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

c++ func 2

Uploaded by

alswyah17
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

c++ func 2

Uploaded by

alswyah17
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

FACULTY OF ELECTONICS TECHNOLOGY

Subject: CP II SEMESTER 4th

Experiment 2-B Recursive Function and Overload Function

Recursivity
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
shown below.

For example, in order to obtain the factorial of a number (n!) the mathematical formula would
be: n! = n * (n-1) * (n-2) * (n-3) ... * 1
More concretely, 5! (factorial of 5) would be: 5! = 5 * 4 * 3 * 2 * 1 = 120
And a recursive function to calculate this in C++ as shown below :

// factorial calculator
#include <iostream>
using namespace std;
long factorial (long a)
{
if (a > 1)
return (a * factorial (a-1));
else
return 1;}
int main ()
{ long number = 9;
cout << number << "! = " << factorial (number)<<endl;
system(“pause”);
return 0;
}
1
Eng:Alhadi
FACULTY OF ELECTONICS TECHNOLOGY

Subject: CP II SEMESTER 4th

Experiment 2-B Recursive Function and Overload Function

Simple program to understand recursive function


#include<iostream>
using namespace std;
void recursive_function(int n){

if(n>10) //base case


return;
else{
cout<<"Recursive Function call number "<<n<<endl;
recursive_function(n=n+1); // here function is calling it self
}
}
int main(){
int n=1;
recursive_function(n); // function call
System(“pause”);
return 0;
}
Example 1: Fibonacci Sequence

#include <iostream>
#include <stdlib.h>

using namespace std;


int Fibonacci(int x) {
if (x == 0) return 0; // Stopping conditions
if (x == 1) return 1;
return Fibonacci(x - 1) + Fibonacci(x - 2);
}
int main() {
int num;
cin >> num;
cout << Fibonacci(num) << endl;

system("PAUSE");
return 0;
2
Eng:Alhadi
FACULTY OF ELECTONICS TECHNOLOGY

Subject: CP II SEMESTER 4th

Experiment 2-B Recursive Function and Overload Function

Example of complete program of recursive function Power function as shown


#include <iostream>
#include <stdlib.h>

using namespace std;

int power(int x,int n){


if(n==0)
return 1;
else
return x*power(x,n-1);
}
int main()
{int x,y;
cin>>x>>y;
cout<<"insert base number is x = "<<x<<"the power number is y = "<<y<<endl;
cout<<power(x,y)<<endl;
system("PAUSE");
return 0;
}
Overload functions

In C++ programming, two functions can have same identifier(name) if either number of
arguments or type of arguments passed to functions are different. These types of functions
having similar name are called overloaded functions.

/* Example of function overloading */


int test() { }
int test(int a){ }
int test(double a){ }
int test(int a, double b){ }
All 4 functions mentioned above are overloaded function.
It should be noticed that, the return type of all 4 functions is same,i.e, int. Overloaded function
may or may not have different return type but it should have different argument(either
3
Eng:Alhadi
FACULTY OF ELECTONICS TECHNOLOGY

Subject: CP II SEMESTER 4th

Experiment 2-B Recursive Function and Overload Function

type of argument or numbers of argument passed). Two functions shown below are not
overloaded functions because they only have same number of arguments and arguments in both
functions are of type int.
/* Both functions has same number of argument and same type of argument*/
/* Hence, functions mentioned below are not overloaded functions. */
/* Compiler shows error in this case. */

int test(int a){ }


double test(int b){ }
Following is the example where samd function print() is being used to print different data
types.

#include <iostream>
#include <stdlib.h>
#include<conio.h>
using namespace std;

void print(int i){


cout<<" Printing integer value I = "<< i<<endl;
}
void print(double f){
cout<<" Printing float value F = "<<f<<endl;
}
void print(char* c){
cout<<" Printing character c = "<< c<<endl;
}

int main(void)
{
print(5);
print(500.689);
print("Hello libya");
getch();
return 0;
}

4
Eng:Alhadi
FACULTY OF ELECTONICS TECHNOLOGY

Subject: CP II SEMESTER 4th

Experiment 2-B Recursive Function and Overload Function

Example of overload function

#include <iostream>
using namespace std;
void test(int);
void test(float);
void test(int, float);
int main() {
int a = 5;
float b = 5.5;
test(a); //call function test with int argument
test(b); //call function test with float argument
test(a, b); //call function test with int and float argument
system("pause");
return 0;
}

void test(int var) {


cout<<"Integer number: "<<var<<endl;
}

void test(float var){


cout<<"Float number: "<<var<<endl;
}

void test(int var1, float var2) {


cout<<"Integer number: "<<var1<<endl;
cout<<"And float number:"<<var2<<endl;
}

Example :using overload Function arguments are of different data type

#include <iostream>
using namespace std;

/* Function arguments are of different data type */

5
Eng:Alhadi
FACULTY OF ELECTONICS TECHNOLOGY

Subject: CP II SEMESTER 4th

Experiment 2-B Recursive Function and Overload Function

long add(long, long);


float add(float, float);
int main()
{
long a, b, x;
float c, d, y;
cout << "Enter two integers\n";
cin >> a >> b;
x = add(a, b);
cout << "Sum of integers: " << x << endl;
cout << "Enter two floating point numbers\n";
cin >> c >> d;
y = add(c, d);
cout << "Sum of floats: " << y << endl;
system("pause");
return 0;
}

long add(long x, long y)


{ long sum;
sum = x + y;
return sum;
}
float add(float x, float y)
{
float sum;
sum = x + y;
return sum;
}

6
Eng:Alhadi

You might also like