2
void viewMenu(){

//code

viewSellMenu();

}

void viewSellMenu(){

//code

viewMenu();

}

How do i have to code those 2 function so they can have a mutual relation?

peterchen
  • 40,917
  • 20
  • 104
  • 186
user432
  • 33
  • 3

2 Answers2

0

To call a function, the compiler needs to know its declaration, i.e. what's it called, that it's a function, and what the parameters and return type are:

void viewSellMenu(void); // declaration of viewSelMenu

// *definition* of viewMenu also serves as declaration
void viewMenu(){

//code

viewSellMenu();  // can be called because compiler knows declaration

}

// *definition* of viewSelMenu
void viewSellMenu(){

//code

viewMenu();

}
peterchen
  • 40,917
  • 20
  • 104
  • 186
0

To use an identifier, it must be "visible" to the compiler. In C, visibility is from top to bottom. So, you have to add a declaration to a function above the function that calls it:

void viewSellMenu();

// rest of code

Since you call the function viewSellMenu() with no arguments, consider changing it's signature to:

void viewSellMenu(void);

and when you define it, write:

void viewSellMenu(void)
{
    viewMenu();
}

Same goes for viewMenu().

Note: If a function definition exists above a function that calls it, then you don't have to add a declaration.

machine_1
  • 4,266
  • 2
  • 21
  • 42
  • I strongly recommend adding a prototype to your declaration, i.e. `void viewSellMenu(void);`. – melpomene Mar 03 '19 at 10:03
  • @melpomene I don't get it. `viewSellMenu` does not take `void`... – machine_1 Mar 03 '19 at 10:05
  • 1
    `viewSellMenu` takes no arguments. The syntax for that is `(void)`. If you just use `()`, it means "this function takes an unspecified number of arguments". – melpomene Mar 03 '19 at 10:06
  • @melpomene but why should I write something that differs from what the OP is showing? – machine_1 Mar 03 '19 at 10:07
  • Because answers are supposed to improve/fix the code in the question. Also because OP's code only contains function definitions, not declarations, so the prototype issue doesn't really come up. – melpomene Mar 03 '19 at 10:08
  • Depends on what you mean by "wrong". The language allows it (for compatibility with pre-standard code from the 1980s), but it wouldn't pass code review today. – melpomene Mar 03 '19 at 10:11
  • @melpomene Now I see it. The OP calls viewSellMenu with no arguments so it should rather be `void viewSellMenu(void)` like you said. – machine_1 Mar 03 '19 at 10:13