+ 4
What is recursion function?
6 Réponses
+ 11
here if n is 3 for example it will decrease to 2 then to 1 , so when it reaches 1 it returns 3×2×1.(n*factorial(n-1)) bcz it knows what to return when n=1 .
+ 8
a recursion function is a function that repeats its self unless it finds a value to stop on and u declare this value ex :
#include<stdio.h>
unsigned factorial (int n)
{ if (n==0 || n==1)
return 1 ;
else
return n*(factorial(n-1) );
}
void main ()
{ int n ;
printf("enter a number");
scanf ("%d", &n);
printf ("answer : %d!= %u ",n,factorial(n));
}
+ 2
in simple terms its just a function calling itself repeatedly
+ 1
This is a function that looks like a loop, but the exit condition of the function always lies in the function. Sample:
#include <iostream>
int rec(int i){
if(i>10)
return i;
i++;
rec(i);
}
int main(){
int i=0;
std::cout<<rec(i);
}
output: 11
for better understanding, you need to remember the theory of the graph.
0
A function that is called by itself😊