How do i goto a new function?

I was wondering how or what command to use to get from the middle of one function and go into a new one from that function, like a goto statement but not within the same function, from one function to another? Examples would be greatly helpful as i am a visual learner. :)
Last edited on
Answer: don't do that. (It is possible, but a very bad idea.)
Care to expand on that, cuz at my school i learned C# and my teacher said there was nothing wrong with doing it, i also found it very helpful.

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
cout << " 1: New Game" << endl;
cout << " 2: Options" << endl;
cout << " 3: Help" << endl;

switch (a) {
	case 1:
		newgame(); //This brings me to the newgame function
		break;
	case 2:
		options(); //This brings me to the options function
		break;
	case 3:
		help(); //This brings me to the help function
		break;
	default:
		cout << "Invalid options, please choose 1, 2, or 3." << endl;
}

}

void newgame ()
{
	return 0;
}

void options ()
{
	return 0;
}

void help ()
{
	return 0;
}


This is how i would write it in C#, if i am not to do it this way in C++, can you explain how to accomplish the same goal in C++?
Last edited on
Um... That's not C#. That's C++. And you're returning values inside functions that are supposed to return nothing.

Also, what exactly is it you want to do?
Do you want to transfer control to a function and have it returned to the original function after it finishes or you want do a jump from one function to another?
For the former you'd do this:
1
2
3
4
5
6
7
8
9
10
11
void A(){
    ...
    B();
    ...
    return;
}

void B(){
    ...
    return;
}

For the latter:
1
2
3
4
5
6
7
8
9
10
11
12
13
void A(){
    ...
    goto B_000;
    ...
    return;
}

void B(){
    ...
B_000:
    ...
    return;
}

I've never actually tried this, so I'm not sure if it will work or even compile. In any case, this is like playing with fire and one of the most dangerous things you could possibly do. If you're not careful, you have a very high chance of smashing the stack. I'd say if your design requires you to do this, you need to reconsider it.
Tell your teacher she shouldn't be teaching if she doesn't know what the hell she's talking about.
Thank you helios, i wanted to do the former... but it does not let me do it how you say. It wont let me transfer control from the main function

I am doing this:
1
2
3
4
5
6
7
8
9
10
11
12
13
using namespace std;

int main()
{
	newgame();

	return 0;
}

void newgame()
{
	return;
}


It claims the "newgame" identifier is not found.

How could i solve this problem? I doubt i will get a response any longer though so i am going to repost this.
Ah. The terminology is "calling a function".

The compiler reads the file one line at a time, just like we do. When it gets to line 5 it finds what looks like a call to a function named "newgame", but it has no idea (yet) what that function looks like.

You can fix it one of two ways:
method one
1
2
3
4
5
6
7
8
9
10
void newgame()  // the compiler knows what this is now... 
  {
  ...
  }

int main()
  {
  newgame();    // ...so when it gets here it knows how to call it 
  return 0;
  }

method two
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void newgame();  // this is a function prototype
                 // A function prototype tells the compiler about a function
                 // that has yet to be defined (or, more accurately, that is
                 // defined elsewhere --in this case, later in this file).

int main()
  {
  newgame();     // the compiler knows all about this because of the prototype above... 
  return 0;
  }

void newgame()   // here we actually define the function.
  {              // The function header must
  }              // match its prototype above. 

Hope this helps.
Last edited on
hi use jump() to go to one function to another function.
So, rather than addressing the OP's actual question you felt it necessary to blithely recommend a non-standard and dangerous function?

Don't encourage beginners to do stupid stuff.
Just a minor addition to Duoas's comments.

While the function prototype and function definitions headers must match in terms of type, name (case sensitive) of function and parameter list types, it is OK to have different parameter names.

So the following prototypes are all equivelent
1
2
3
void newgame(int);             //You should, of course
void newgame(int gameNumber);  //only use one of
void newgame(int level);       //the three!!! 

and would match the definition
1
2
3
void newgame(int x)
{
}

I mention it because it was something that confused me in the examples when I was first learnign C++. The book I was using would swap between no parameter names, matching parameter names or mismatched parameter names without any note to explain this behaviour.
Could someone elaborate on why prototypes where created in the first place, or why they are used. To not sound ignorant I will explain.

Why not just put the function definition before main()? Does it have to do with when you include files?? Thus you can have prototypes is one file and function definitions in another (I thought I read that somewhere) I have never really understood the use.

Thanks
Last edited on
You've got it head-on.

I usually put main() last, so I don't tend to have very many prototypes for functions in the same file.

However, prototypes are essential to link to functions declared in other modules.

This is particularly handy when you have a lot of code and you don't want to have to recompile all of it every time you make a change to just one module.

Hope this helps.
Additionally, you may need to call function b inside function a and viceversa. You can't do that without first declaring the functions and then defining them.
Duoas,

Would this be where you would use prototypes:

File A:
1
2
//function definitions
//Main does not exist in this file 


File B:
1
2
3
#include <filea>
//prototype for functions within  file A
main();


Last edited on
Simple example one: linkage

hello.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
using namespace std;

#include "greeting.hpp"

int main()
  {
  string myname;
  cout << "What is your name? ";
  getline( cin, myname );

  salute( myname );
  salute( "Séanaéd" );

  return 0;
  }

greeting.hpp
1
2
3
4
5
6
7
8
9
#ifndef GREETING_HPP
#define GREETING_HPP

#include <string>

void salute( const std::string& name );
  // Gives a timely greeting to the named individual.

#endif  

greeting.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <ctime>
#include <iostream>
#include <string>

#include "greeting.hpp"

void salute( const std::string& name )
  {
  std::time_t     timevalue = std::time( NULL );
  struct std::tm* timedata = std::localtime( &timevalue );
  if (timedata->tm_hour < 12)
    std::cout << "Good morning ";
  else if (timedata->tm_hour < 18)
    std::cout << "Good afternoon ";
  else if (timedata->tm_hour < 22)
    std::cout << "Good evening ";
  else
    std::cout << "Good night ";
  std::cout << name << "!\n";
  }

Compile and link hello.cpp and greeting.cpp. Using the GCC, that is done with
g++ -Wall -o hello hello.cpp greeting.cpp

Technically, you don't have to compile them both at the same time. You could do it at different times:
On Monday: compile the 'greeting' module only
g++ -Wall -c greeting.cpp
On Friday: compile the main program only
g++ -Wall -c hello.cpp
On Saturday: link the two together
g++ -o hello hello.o greeting.o

The reason that hello.cpp can use things in greeting.o is because greeting.hpp tells it what is in greeting.o.

When you run the program it will say hello to whomever you identify yourself as as is appropriate to the time of day. So if it were 3:00 pm, and I ran the program, it might look like:
D:\prog\hello> hello
What is your name? Dúthomhas
Good afternoon Dúthomhas!
Good afternoon Séanaéd!
D:\prog\hello>


Simple example two: recursion (Thanks helios!)

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
#include <iostream>
using namespace std;

bool odd( unsigned n );

int main()
  {
  int n;
  cout << "Please enter a positive number> ";
  cin >> n;

  if (odd( n ))
    cout << "That sure is an odd number!\n";
  else
    cout << "What a well-balanced number!\n";

  return 0;
  }

bool odd( unsigned n )
  {
  bool even( unsigned n );

  switch (n)
    {
    case 0:  return false;
    case 1:  return true;
    default: return even( n -1 );
    }
  }

bool even( unsigned n )
  {
  switch (n)
    {
    case 0:  return false;
    case 2:  return true;
    default: return odd( n -1 );
    }
  }

In this example there is only one file, but the relative position of the functions are a bit wonky to illustrate a point.

odd() is declared (aka prototyped) before main(), so main() can use it there on line 12 without any grief. Notice how the prototype is in the global space. (Any function appearing after the prototype can use odd().)

The odd() function is mutually recursive with even(), but even() is neither declared nor defined before odd() is defined. Therefore, we prototype even() so that odd() can use it. This time, though, the prototype is in odd()'s local space, so any functions following odd() but preceding even() cannot use even() [without also prototyping it]. So on line 28 there odd() can use even() without any trouble.

Finally, even() can use odd() on line 38 because odd() is both declared and defined (definition implies declaration) before even().

When you compile and run it, try not to blow the stack with really huge numbers.


Hope this helps.
I get it, thanks!

In example one I understand the use, but why not just put even() before odd(), and both before main()?

Maybe I just need to get over it!
Try it and see what happens.
As a project, I am writing a tic tac toe game, for the console and I have a slew of diffrent functions, I now see why it is important when you have several different functions!

Duoas could you point me in the direction of a good article on linking diffrent files together. Like if I want to make a file of all the functions I use in my TTT program. I know I need to use an #include <...> , but how does the compiler know where that file is. Does it just assume that it is in the directory with all the other library's (Like iostream and string etc) or is there another command I should/can use?
Hmm, I'm sure I posted a response to this very question recently, but I can't find it. This link has a pretty good example:
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044842972&id=1043284392

Maybe I'll make an Article or FAQ on this.
Thanks, The article answered my question.
Topic archived. No new replies allowed.