0

I am a beginner in C and there is one problem i can't seem to solve. I want to go from main() for example, to another function with a line or two of code. I have searched the Internet and nothing has answered this specific question, or so it seems to my amateur eyes. For example, i want to do this:

int main(void){
/*here i need a line of code that will make the program run FunctionToCall*/

}
int FunctiontoCall(void){
printf ("you went to this function! congratulations!\n");

}

I hope that helps sorry for the unclear question!

user2651382
  • 101
  • 1
  • 3
  • 8

2 Answers2

2

Just call the function you want to go to.

For example:

void functionToCall(int x, int y, int z);

int main() {
   //some code...
   functionToCall(x, y, z);
   //more code...
}

void functionToCall(int x, int y, int z) {
   //more code...
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Undefined
  • 655
  • 1
  • 4
  • 13
  • 2
    `function(x,y,z);` is not valid C; the types are missing. Don't confuse a beginner with sloppy snippets... – Jens Aug 07 '13 at 15:00
  • Yes, both the return type and the parameter types are missing. – Crowman Aug 07 '13 at 15:01
  • I know. The types are arbitrary. I just put them in to give an example of parameters. I didn't want to add types to avoid confusion. – Undefined Aug 07 '13 at 15:01
  • Answering a question with incorrect code which won't compile is more likely to lead to confusion than avoid it, especially for someone who apparently doesn't know how to call functions at all. – Crowman Aug 07 '13 at 15:02
  • @DrewMcGowen: Even K&R C required you to declare the types of your parameters. – Crowman Aug 07 '13 at 15:02
  • According to [this](http://stackoverflow.com/questions/1630631/alternate-c-syntax-for-function-declaration-use-cases), the parameter type specifiers are optional in pre-C99 code, and are assumed `int` – Drew McGowen Aug 07 '13 at 15:04
  • 1
    @PaulGriffiths: `functionToCall(x, y, z) { /* code ... */ }` is a valid function definition in (pre-ANSI) K&R C; the parameters and the function result are implicitly of type `int`. – Keith Thompson Aug 07 '13 at 15:05
  • @KeithThompson: If you want them to be `int`s, then yes, you're right. – Crowman Aug 07 '13 at 15:06
  • @DrewMcGowen While K&R allows implicit int, it doesn't allow non-empty parameter lists in a function declaration, IIRC. – Jens Aug 07 '13 at 15:06
  • @Jens: Not in a declaration, but a K&R function definition includes a list of parameter names. – Keith Thompson Aug 07 '13 at 15:09
  • 1
    A simpler solution is to put the definition of `functionToCall` above the definition of `main`; then no separate declaration is needed. – Keith Thompson Aug 07 '13 at 15:11
1

I think this will help you in understanding how a method call worksenter image description here

So when you call a method x from method y control from x passes to y and return back to x when a return statement/last statement of the called method is encountered

Sanyam Goel
  • 2,138
  • 22
  • 40