Recursion,Storage Classes,Function
overloading in C++
KALINGA INSTITUTE OF INDUSTRIAL
TECHNOLOGY
School Of Computer Engineering
Dr. Pradeep Kumar Mallick
Associate Professor [II]
School of Computer Engineering,
Kalinga Institute of Industrial Technology (KIIT),
Deemed to be University,Odisha
3 Credit Lecture Note 05
Chapter Contents
2
Recursion
C++ Storage Classes
Function overloading in C++
Rules for Function Overloading
Recursion
3
When function is called within the same function, it is known as recursion in C+
+.
The function which calls the same function, is known as recursive function.
A function that calls itself, and doesn't perform any task after function call, is
known as tail recursion.
In tail recursion, we generally call the same function with return statement.
Example
4
#include<iostream>
using namespace std; int factorial(int n)
int main() {
{ if(n<0)
int factorial(int);
return(-1); /*Wrong value*/
int fact,value;
cout<<"Enter any number: ";
if(n==0)
cin>>value;
return(1); /*Terminating
fact=factorial(value);
condition*/
cout<<"Factorial of a number is: "<<fact<<endl;
return 0;
else
} {
return(n*factorial(n-1));
}
}
C++ Storage Classes
5
Storage class is used to define the lifetime and visibility of a variable and/or
function within a C++ program.
Lifetime refers to the period during which the variable remains active and
visibility refers to the module of a program in which the variable is accessible.
There are four types of storage classes, which can be used in a C++ program
1. Automatic
2. Register
3. Static
4. External
Storage Classes
6
Storage Class Keyword Lifetime Visibility Initial Value
Automatic auto Function Block Local Garbage
Register register Function Block Local Garbage
External extern Whole Program Global Zero
Static static Whole Program Local Zero
Function overloading in C++
7
Function overloading is a C++ programming feature that allows us to have more than one
function having same name but different parameter list,
Parameter list means the data type and sequence of the parameters,
Example the parameters list of a function myfuncn(int a, float b) is (int, float) which is
different from the function myfuncn(float a, int b) parameter list (float, int).
Function overloading is a compile-time polymorphism.
Now that we know what is parameter list lets see the rules of overloading: we can have
following functions in the same scope.
Example:
sum(int num1, int num2)
sum(int num1, int num2, int num3)
sum(int num1, double num2)
Examples:
8
These two have different number of parameters:
sum(int num1, int num2)
sum(int num1, int num2, int num3)
These two functions have different parameter type:
sum(int num1, int num2)
sum(double num1, double num2)
These two have different sequence of parameters:
sum(int num1, double num2)
sum(double num1, int num2)
All of the above three cases are valid case of overloading. We can have any number
of functions, just remember that the parameter list should be different. For example:
int sum(int, int)
double sum(int, int)
This is not allowed as the parameter list is same. Even though they have different return
types, its not valid.
Example
9
#include <iostream>
using namespace std; Output:
void print(int i) {
cout << " Here is int " << i << endl; Here is int 10
} Here is float 10.1
void print(double f) { Here is char* ten
cout << " Here is float " << f << endl;
}
void print(char const *c) {
cout << " Here is char* " << c << endl;
}
int main() {
print(10);
print(10.10);
print("ten");
return 0;
}
Rules for Function overloading
10
11