Search code examples
crecursionmatrixvoid

is it possible for two different recursive voids to call each other in C?


i am trying to use two recursive that depend on each other in C but when i compile i get this error :

In function 'void Horizontal(int* (*)[1000], int, int, int*)':

[Error] 'Vertical' was not declared in this scope

and if i swap vertical and horizontal , horizontal becomes undeclared in this scope

here is the code i wrote :

#include <stdio.h>
#include <stdlib.h> 
 void Horizontal(int*m[][1000],int i,int j,int*cpt)
 {
    if(*m[i][j]!=0)
    {
        *m[i][j]=0;
        *cpt++;
        Vertical(&*m,i,j,&*cpt);
        Horizontal(&*m,i,j-1,&*cpt);
        Horizontal(&*m,i,j+1,&*cpt);
    }
 }
 void Vertical(int*m[][1000],int i,int j,int*cpt)
 {
    if(*m[i][j]!=0)
    {
        *m[i][j]=0;
        *cpt++;
        Horizontal(&*m,i,j,&*cpt);
        Vertical(&*m,i-1,j,&*cpt);
        Vertical(&*m,i+1,j,&*cpt);
    }
 }
 int main()
 {

 }

the int main() is empty because i just wanted to check my syntax


Solution

  • They can, but you need to forward declare the function.

    Like so:

    // Forward declaration:
    
    void Horizontal( int* m[][1000], int i, int j, int* cpt );
    
    void Vertical( int* m[][1000], int i, int j, int* cpt );
    
    // Function implementation:
    
    void Horizontal(int*m[][1000],int i,int j,int*cpt)
     {
        if(*m[i][j]!=0)
        {
            *m[i][j]=0;
            *cpt++;
            Vertical(&*m,i,j,&*cpt);
            Horizontal(&*m,i,j-1,&*cpt);
            Horizontal(&*m,i,j+1,&*cpt);
        }
     }
     void Vertical(int*m[][1000],int i,int j,int*cpt)
     {
        if(*m[i][j]!=0)
        {
            *m[i][j]=0;
            *cpt++;
            Horizontal(&*m,i,j,&*cpt);
            Vertical(&*m,i-1,j,&*cpt);
            Vertical(&*m,i+1,j,&*cpt);
        }
     }
    

    To allow other *.c files to use these functions you place these forward-declarations in a separate file (the header file) and let your other *.c files use that header file and the linker takes care of the rest.