0% found this document useful (0 votes)
14 views5 pages

WWW W3schools Com CPP CPP - Functions - Recursion Asp

Uploaded by

Abenezer Asefa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
14 views5 pages

WWW W3schools Com CPP CPP - Functions - Recursion Asp

Uploaded by

Abenezer Asefa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

 Tutorials  Exercises  Services  

C#
 BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY

C++ Recursion
❮ Previous Next ❯

Recursion
Recursion is the technique of making a function call itself. This technique provides a
way to break complicated problems down into simple problems which are easier to
solve.

Recursion may be a bit difficult to understand. The best way to figure out how it works
is to experiment with it.

Recursion Example
Adding two numbers together is easy to do, but adding a range of numbers is more
complicated. In the following example, recursion is used to add a range of numbers
together by breaking it down into the simple task of adding two numbers:

Example
int sum(int k) {
if (k > 0) {
return k + sum(k ‐ 1);
} else {
return 0;
}
}
int main() {
int result = sum(10);
cout << result;
return 0;
}

Try it Yourself »

Example Explained
When the sum() function is called, it adds parameter k to the sum of all numbers
smaller than k and returns the result. When k becomes 0, the function just returns 0.
When running, the program follows these steps:

10 + sum(9)
10 + ( 9 + sum(8) )
10 + ( 9 + ( 8 + sum(7) ) )
...
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + sum(0)
10 +9+8+7+6+5+4+3+2+1+0

Since the function does not call itself when k is 0, the program stops there and returns
the result.

The developer should be very careful with recursion as it can be quite easy to slip into
writing a function which never terminates, or one that uses excess amounts of memory
or processor power. However, when written correctly recursion can be a very efficient
and mathematically­elegant approach to programming.

❮ Previous Next ❯
COLOR PICKER




SPACES

UPGRADE

NEWSLETTER

GET CERTIFIED

REPORT ERROR
Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial

Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference

Top Examples
HTML Examples
CSS Examples
JavaScript Examples
How To Examples
SQL Examples
Python Examples
W3.CSS Examples
Bootstrap Examples
PHP Examples
Java Examples
XML Examples
jQuery Examples

Get Certified
HTML Certificate
CSS Certificate
JavaScript Certificate
Front End Certificate
SQL Certificate
Python Certificate
PHP Certificate
jQuery Certificate
Java Certificate
C++ Certificate
C# Certificate
XML Certificate

    FORUM ABOUT
W3Schools is optimized for learning and training. Examples might be simplified to improve
reading and learning. Tutorials, references, and examples are constantly reviewed to avoid
errors, but we cannot warrant full correctness of all content. While using W3Schools, you
agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999­2023 by Refsnes Data. All Rights Reserved. W3Schools is Powered by


W3.CSS.

You might also like