Is there a way to reference a function that you are inside of C?

I am writing a function that just searches for values ​​inside a table. Is it possible to call this function internally? I have seen the material about thisand selfand do not understand it.

+5
source share
4 answers

Yes, you can. This is called recursion.

void foo(){
   foo(); //This is legal.
}

Of course, you need to return from it to avoid endless recursive calls. Failure to return will result in a stack overflow. Here is a better example:

void foo(int n){
    if (n == 0)
        return;
    foo(--n);
}
+12
source

See Recursion (computer science) (Wikipedia).

An example of calling a function inside a function:

# include<stdio.h>

int factorial(unsigned int number)
{
    if (number <= 1)
        return 1;
    return number * factorial(number - 1);
}

void main()
{
    int x = 5;
    printf("factorial of %d is %d",x,factorial(x));
}
+5
source

, , , . , , .

, , , . , , .

, - . .

JoshLeaves , , - . , .

+2

, , " ". , . :

, ( , Intel Core i5).

//Iteration
function do_stuff(i)
{
    //BLABLAH
}

for (i = 0; i <5; i++) {
    do_stuff();
}

//Recursion
function do_stuff(int i)
{
    //BLABLAH
    if (i < 5) {
        do_stuff(i + 1);
    }
}
  • , ...
  • ( " " ...), , .
+1

All Articles