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.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889`` ``````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 "<>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
 ``12345678`` ``````#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
 ``12`` ``````#include "norcliff.h" // Here goes your code ``````

main.cpp
 ``1234567`` ``````#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.

 ``123456789`` ``````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:
 ``1234567891011`` ``````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.