# Tutorial dan Contoh Recursion Pemrograman C++

##### 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:
• 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.
Consider this example to find factorial of a number using recursion.

## 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: 4Factorial 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 statement `return num*factorial(num-1);` is executed, which calls second `factorial()` function and argument passed is `num-1` which is 3.
• In second `factorial()` function, test expression inside if statement is true. The statement `return num*factorial(num-1);` is executed, which calls third `factorial()` function and argument passed is `num-1` which is 2.
• In third `factorial()` function, test expression inside if statement is true. The statement `return num*factorial(num-1);` is executed, which calls fourth `factorial()` function and argument passed is `num-1` which is 1.
• The fourth `factorial()` function, test expression inside if statement is false. The statement `return 1;` is executed, which returns 1 to third `factorial()` function.
• The thrid `factorial()` function returns 2 to second `factorial()` function.
• The second `factorial()` function returns 6 to first `factorial()` function.
• Finally, first `factorial()` function returns 24 to the `main()` 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 :
{"ft_lang_label":"__label__en","ft_lang_prob":0.6880235,"math_prob":0.95365906,"size":2929,"snap":"2022-05-2022-21","text_gpt3_token_len":700,"char_repetition_ratio":0.19897436,"word_repetition_ratio":0.1452381,"special_character_ratio":0.22226016,"punctuation_ratio":0.14035088,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9958348,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-26T14:03:15Z\",\"WARC-Record-ID\":\"<urn:uuid:5f487881-7285-4c68-8cc4-3af5afff19a9>\",\"Content-Length\":\"272566\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:c50e76a5-e35a-4eb2-af2a-2340b72bd9d0>\",\"WARC-Concurrent-To\":\"<urn:uuid:18eecc2e-ea2e-4553-961f-4dae3e137cc5>\",\"WARC-IP-Address\":\"142.250.31.121\",\"WARC-Target-URI\":\"https://blog.mycoding.id/2021/05/tutorial-dan-contoh-recursion.html\",\"WARC-Payload-Digest\":\"sha1:QYRNLNWEE3FV646XJUYH6J2GMQAAFOSF\",\"WARC-Block-Digest\":\"sha1:OJDIJZD46SCBXFHNY2YBCKRTQTUEY3WO\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652662606992.69_warc_CC-MAIN-20220526131456-20220526161456-00318.warc.gz\"}"}