Make functions call each other - C

1.8k Views Asked by At

A noob question: I created a library called funcoes.h that have a menu() and other functions that can call menu(). An example:

void cifrar(){

 printf("\n\nDeseja cifrar outra mensagem? Digite 1 para Sim ou 2 para sair: ");
 scanf("%d", &exit);

 if(exit == 1){
      cifrar();
         }
 else{
      menu();
      }

}

void menu(){
     printf("Escolha uma das opcoes: ");         
     scanf("%d", &varMenu);
     switch(varMenu){
                     case 1:
                          system("cls");
                          cifrar();
                          break;
                     case 2:
                          system("cls");
                          decifrar();
                          break;
                     case 3:
                          system("cls");
                          sair();
                          break;
                     default:
                          system("cls");   
                          printf("Escolha uma opcao valida!\n\n");
                          menu();
                          break;
     }         
}

But when I compile, I have this error:

In function 'void cifrar()'
'menu' undeclared(first use this function)"

'void menu()' used prior to declaration

How to make them call each other without this error?

Thanks!

3

There are 3 best solutions below

0
On

every function that you call has to be declared BEFORE that call. you can do this by using a prototype of that function:

void menu();

void cifrar() {
  ...
}

void menu() {

  ..
}

or simply by putting the whole main function (with it's body) on top of cifrar.

0
On

Well, maybe it would be nice to sum up what is in comments.

The compiler wants to know any function's prototype before this function is used somewhere else. 'Before' here means something like 'earlier in the source file'. You can, although, place all the prototypes in a separate .h file, include it in the .c file with actual code, and then place function implementations in whatever order you like - the compiler will not complain.

0
On

What you should do, is to create a header file, which will have the signatures of all your functions and then you do not need to worry about where each function is located at the code, you will be able to use all of the functions all over the code.

Your code should look like this:

funcoes.h

void cifrar(void);
void menu(void);

funcoes.c

#include "funcoes.h"

void cifrar(void){

 printf("\n\nDeseja cifrar outra mensagem? Digite 1 para Sim ou 2 para sair: ");
 scanf("%d", &exit);

 if(exit == 1){
      cifrar();
         }
 else{
      menu();
      }
}

void menu(void){
     printf("Escolha uma das opcoes: ");         
     scanf("%d", &varMenu);
     switch(varMenu){
                     case 1:
                          system("cls");
                          cifrar();
                          break;
                     case 2:
                          system("cls");
                          decifrar();
                          break;
                     case 3:
                          system("cls");
                          sair();
                          break;
                     default:
                          system("cls");   
                          printf("Escolha uma opcao valida!\n\n");
                          menu();
                          break;
     }         
}

Another small tip, do not create functions without any arguments, such as:

void menu();

Always insert the arguments you want to pass. If you want the functions to get not arguments, just pass void.

void menu (void);