Artikel Terkait Tutorial C
In many programming languages including C++, it is possible to call a function from a same function. This function is known as recursive function and this programming technique is known as recursion.
To understand recursion, you should have knowledge of two important aspects:
To understand recursion, you should have knowledge of two important aspects:
- In recursion, a function calls itself but you shouldn't assume these two functions are same function. They are different functions although they have same name.
- Local variables : Local variables are variables defined inside a function and has scope only inside that function. In recursion, a function call itself but these two functions are different functions (You can imagine these functions are function1 and function 2. The local variables inside function1 and function2 are also different and can only be accessed within that function.
Example 1: C++ Recursion
#include <iostream>
using namespace std;
int factorial(int);
int main() {
int n;
cout<<"Enter a number to find factorial: ";
cin>>n;
cout<<"Factorial of "<<n<<" = "<<factorial(n);
return 0;
}
int factorial(int n) {
if (n>1) {
return n*factorial(n-1);
}
else {
return 1;
}
}
Output
Enter a number to find factorial: 4
Factorial of 4 = 24
Explanation: How recursion works?
Suppose user enters 4 which is passed to function factorial()
. Here are the steps involved:- In first
factorial()
function, test expression inside if statement is true. The statementreturn num*factorial(num-1);
is executed, which calls secondfactorial()
function and argument passed isnum-1
which is 3. - In second
factorial()
function, test expression inside if statement is true. The statementreturn num*factorial(num-1);
is executed, which calls thirdfactorial()
function and argument passed isnum-1
which is 2. - In third
factorial()
function, test expression inside if statement is true. The statementreturn num*factorial(num-1);
is executed, which calls fourthfactorial()
function and argument passed isnum-1
which is 1. - The fourth
factorial()
function, test expression inside if statement is false. The statementreturn 1;
is executed, which returns 1 to thirdfactorial()
function. - The thrid
factorial()
function returns 2 to secondfactorial()
function. - The second
factorial()
function returns 6 to firstfactorial()
function. - Finally, first
factorial()
function returns 24 to themain()
function and is displayed.
Tag : C++ Functions, C++ Function Types, C++ Function Overloading, C++ Default Argument, C++ Storage Class, C++ Recursion, C++ Return Reference.
Selain Sebagai Penyedia Panduan Belajar Database dan Tutorial Pemrograman, Kami Juga Membagikan Kumpulan Source Code Program Aplikasi dan Ebook Pemrograman Terlengkap yang Bisa Anda Dapatkan Secara Gratis di Halaman :
Rekomendasi Web Hosting
- 20rb perbulan. Diskon hingga 40% kode kupon: MCP Daftar disini (apache).
- 10rb perbulan. Diskon hingga 75% kode kupon: MCP Daftar disini (litespeed).
- 10rb perbulan. Diskon hingga 70% kode kupon: aff-MCP Daftar disini (apache).