Function Problem

So im having some trouble with the fact functions have to be declared before you can use them, and that really screws up my game concept I am trying to use to get the hang of C++.

It works so you can move between 2 areas of the game, in this case the market and city center.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
int norcliffCentre() {
	cout<<"The city centre is a busy place, with lots of people moving about place to place.\n\n";
	cout<<" 1)Leave the city\n\n 2)Go to the market\n\n";


	cin>>choiceNum;
	
	switch (choiceNum) {
	case 1:
		cout<<"You leave the city through the large gates";
		//Add Code
		break;
	case 2:
		cout<<"You head towards the market";
		cout<<"The market is noisy and busy, with people shouting about what they are selling.\n\n";
		cout<<"A general trader catches your eye, and you walk up to his stall, and you read   what he has to offer\n\n";
		norcliffMarket();
		break;
	}
}

int norcliffMarket(){
	cout<<" You have "<<gold<<" gold.\n\n";
	cout<<" 1)Buy Rusty Sword - 50 Gold\n\n 2)Buy Withering Wand - 50 Gold\n\n 3)Buy Withering Bow - 50 Gold\n\n 4)Leave the Market\n\n";
	
	cin>>choiceNum;

	switch (choiceNum) {
	case 1:
		if (rustySword == false) {
			if (gold >= 50) {
				gold = gold - 50;
				rustySword = true;
				cout<<"You have brought a Rusty Sword for 50 Gold";
				norcliffMarket();
				break;
			} else {
				cout<<"You do not have enough Gold!!";
				norcliffMarket();
				break;
			}
		}else{
			cout<<"You already have this item!";
			norcliffMarket();
			break;
		}
	case 2:
		if (witheringWand == false) {
			if (gold >= 50) {
				gold = gold - 50;
				witheringWand = true;
				cout<<"You have brought a Withering Wand for 50 Gold";
				norcliffMarket();
				break;
			} else {
				cout<<"You do not have enough Gold!!";
				norcliffMarket();
				break;
			}
		}else{
			cout<<"You already have this item!";
			norcliffMarket();
			break;
		}
	case 3:
		if (witheringBow == false) {
			if (gold >= 50) {
				gold = gold - 50;
				witheringBow = true;
				cout<<"You have brought a Withering Bow for 50 Gold";
				norcliffMarket();
				break;
			} else {
				cout<<"You do not have enough Gold!!";
				norcliffMarket();
				break;
			}
		}else{
			cout<<"You already have this item!";
			norcliffMarket();
			break;
		}
	case 4:
		cout<<"\nYou head towards the City Centre\n\n";
		norcliffCentre();
		break;
	}
	return 0;
}


Any help apreciated
Those are function definitions. A function declaration is made up of the return type, fuction name and parameters. The declaration for norcliffMarket would be int norcliffMarket();
You put declarations in header files and (usually) implementations in source files, then #include the header with the declaration of a function in any source file where you wish to use it. Implementation files also need to have the header with the declarations included.
Can you give me a guide through that? I am fairly new to C++ - Im using VS2012
Let's call the file where you defined those functions norcliff.cpp.

norcliff.h
1
2
3
4
5
6
7
8
#ifndef NORCLIFF_H // Google "include guards" to understand what this is
#define NORCLIFF_H // Alternatively, Visual Studio should support #pragma once

// Declarations
int norcliffCentre();
int norcliffMarket();

#endif // NORCLIFF_H 


norcliff.cpp
1
2
#include "norcliff.h"
// Here goes your code 


main.cpp
1
2
3
4
5
6
7
#include "norcliff.h"

int main()
{
    norcliffCentre();
    return 0;
}


To compile correctly a source file the compiler needs to know only that a function exists. That is the purpose of a declaration: telling the compiler that something exists.
Later, during linking, the linker will match a function name with actual code.
Here, each function calls the other - I removed all the controlling logic to focus on the issue.

1
2
3
4
5
6
7
8
9
int norcliffCentre()
{
    norcliffMarket();
}

int norcliffMarket()
{
    norcliffCentre();
}

At line 8, the call to norcliffCentre() is ok, as it is defined previously.
On the other hand, at line 3, the call to norcliffMarket() doesn't work as the compiler at that stage is not aware of the existence of that function, since it is defined later.

The solution is to declare a prototype:
1
2
3
4
5
6
7
8
9
10
11
int norcliffMarket(); // prototype declaration

int norcliffCentre()
{
    norcliffMarket();
}

int norcliffMarket()
{
    norcliffCentre();
}


The declaration at line 1 gives the compiler the basic information it requires about the function, that is the name, return type (int) and parameter list (none).

The actual code for the function is defined lower down, but that's ok now. (As was mentioned, the actual function definition could even be in a separate file).
Also you may find an error in your function if it does not return the same value as the function type. An int function must return an int, but a void function does not need to return anything. For this type of function I would use void functions.

EDIT

also in order for these functions to alter variables such as gold and witheringBow. these need to be global variables.
Last edited on
They are global variables - and thanks all of you - im testing these out!
Thanks Chervil - Your explanation helped a lot - I tried them all out - They all done what i wanted - But I might stick with Chervil's one - and I changed them to void functions!

I learnt a lot from reading this!!
Topic archived. No new replies allowed.