0% found this document useful (0 votes)
7 views

EXP-1 Functions and Recursion in C++

The document provides examples of functions and recursion in C++, illustrating how to declare and call functions with and without parameters. It explains recursion, including its advantages and disadvantages, and presents examples such as calculating the factorial of a number and the sum of the first N natural numbers using recursion. Overall, it serves as a basic guide to understanding functions and recursion in C++ programming.

Uploaded by

23shiv.prasad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

EXP-1 Functions and Recursion in C++

The document provides examples of functions and recursion in C++, illustrating how to declare and call functions with and without parameters. It explains recursion, including its advantages and disadvantages, and presents examples such as calculating the factorial of a number and the sum of the first N natural numbers using recursion. Overall, it serves as a basic guide to understanding functions and recursion in C++ programming.

Uploaded by

23shiv.prasad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Functions and recursion in c++


Example 1: Display a Text

#include <iostream>
using namespace std;

// declaring a function
void greet() {
cout << "Hello there!";
}

int main()
{

// calling the function


greet();

return 0;
}

Output
Hello there!

Example 2: Function with Parameters

// program to print a text

#include <iostream>
using namespace std;

// display a number
void displayNum(int n1, float n2) {
cout << "The int number is " << n1;
cout << "The double number is " << n2;
}

int main() {

int num1 = 5;
double num2 = 5.5;

// calling the function


displayNum(num1, num2);
return 0;
}
Output
The int number is 5
The double number is 5.5

Example 3: Add Two Numbers


// program to add two numbers using a function

#include <iostream>

using namespace std;

// declaring a function
int add(int a, int b) {
return (a + b);
}

int main() {

int sum;

// calling the function and storing


// the returned value in sum
sum = add(100, 78);

cout << "100 + 78 = " << sum << endl;

return 0;
}

Output
100 + 78 = 178

 A function that calls itself is known as a recursive function. And, this technique is known as
recursion.

Advantages of C++ Recursion


 It makes our code shorter and cleaner.
Disadvantages of C++ Recursion
 It takes a lot of stack space compared to an iterative program.
 It uses more processor time.
 It can be more difficult to debug compared to an equivalent iterative program.

Example 1: Factorial of a Number Using Recursion


// Factorial of n = 1*2*3*...*n
#include <iostream>
using namespace std;

int factorial(int);

int main() {
int n, result;

cout << "Enter a non-negative number: ";


cin >> n;

result = factorial(n);
cout << "Factorial of " << n << " = " << result;
return 0;
}

int factorial(int n) {
if (n > 1) {
return n * factorial(n - 1);
} else {
return 1;
}
}

Output
Enter a non-negative number: 4
Factorial of 4 = 24

Example2: C++ Program to calculate the sum of first N natural numbers using recursion
#include <iostream>
using namespace std;

int nSum(int n)
{
// base condition to terminate the recursion when N = 0
if (n == 0) {
return 0;
}

// recursive case / recursive call


int res = n + nSum(n - 1);

return res;
}

int main()
{
int n = 5;
// calling the function
int sum = nSum(n);

cout << "Sum = " << sum;


return 0;
}

Output
Sum = 15

You might also like