FUNCTIONS AND RECURSION
Gaurav Prakash
(MCA/25006/18)
Roopak Bhama
(MCA/25003/18)
BIRLA INSTITUTE OF TECHNOLOGY
MESRA, JAIPUR CAMPUS
FUNCTIONS
• A function is a set of statements that take inputs, do
some specific computation and produces output.
OR
A self contained block of statement performing a
specific task.
• Components
• Prototype
• Call
• Body
PROTOTYPE
• Tells compiler about
• return type of function
• function name
• number of parameters
• data types of parameters
rtype fnct_name(dtype para1,…);
rtype = return type of function
fnct_name = name of the function
dtype = data type of parameter
para1 = parameter one… to ‘n’
PROTOTYPE
FUNCTION CALL
1 -
#include<iostream.h>
2 - void prnt();
3 - void main(){
4 - prnt();
5 - }
6 - void prnt(){
7 - cout<<“Hello”;
8 - }
1 -
#include<iostream.h>
2 - void prnt();
3 - void main(){
4 - prnt();
5 - }
6 - void prnt(){
7 - cout<<“Hello”;
8 - }
1 -
#include<iostream.h>
2 - void prnt();
3 - void main(){
4 - prnt();
5 - }
6 - void prnt(){
7 - cout<<“Hello”;
8 - }
1 – Include header files as per requirements of program.
2 – Function declaration. Tells the program that a function named prnt() has been
used.
3 – Main function starts. Program execution starts here.
4 – Function Call. Calls prnt() which returns nothing and have no parameters,
program moves to line 6.
6 – Starts executing statements of prnt(). Returns back to main at line 5. Program
FUNCTION BODY
1. #include<iostream.h>
2. void times(int);
3. int main(){
1. void times(5);
4. }
5. void times(int a){
1. for(int i=0;i<a;i++){
1. cout<<“Hellon”;
2. }
6. }
Function
Body
OUTPUT:
Hello
Hello
Hello
Hello
Hello
FUNCTIONS TYPES
LIBRARY USER DEFINED
Header Functions
stdio printf(),scanf()
math pow(), sqrt()
string strrev(),strcpy()
Without Argument, Without Return-
type
Without Argument, With Return-type
With Argument, Without Return-type
With Argument, With Return-type
NO ARGUEMENT AND NO
RETURN VALUES
#include<iostream>
using namespace std;
void printgreet(){
cout<<"Hellon";
}
int main(){
printgreet();
return 0;
}
• The empty
parentheses in
printgreet();
indicates that no
argument is passed
to the function.
• The return type of
the function is void.
Hence, no value is
returned from the
function.
NO ARGUEMENT AND RETURN
VALUES
#include<iostream.h>
int printgreet(){
int i,a;
cout<<"Enter times to
greetn";
cin>>a;
for(i=0;i<a;i++)
cout<<"Hellon";
return i;
}
void main(){
int a;
cout<<"Greeted
"<<printgreet()<<" time"; }
• The empty
parentheses in
printgreet();
statement indicates
that no argument is
passed to the
function.
• Here, the
printgreet()
function takes
input from the user
and returns i to the
main().
ARGUEMENT AND NO RETURN
VALUES
#include<iostream.h>
void printgreet(int a){
for(int i=0;i<a;i++)
cout<<"Hellon";
}
int main(){
printgreet(5);
return 0;
}
• The integer value is
passed to
printgreet()
function.
• The program will
print hello the
number of times
which is passed to
it.
• that is ‘5’ in
the above
program.
ARGUEMENT AND RETURN
VALUES
#include<iostream.h>
int printgreet(int x){
for(i=0;i<x;i++)
cout<<"Hellon";
return x;
}
int main(){
int a;
cout<<"Enter numbern";
cin>>a;
cout<<"Greeted
"<<printgreet(a)<<" times";
return 0;
}
• The input from the
user is passed to
printgreet()
function.
• The printgreet()
function returns
the number of
times the greeting
message has been
printed.
• Then the program
returns ‘x’ to the
main().
ACTUAL VS FORMAL
PARAMETERS
#include<iostream.h>
int sum(int a, int b) //Formal Parameters
{
return a+b;
}
int main()
{
int x=10,y=20;
int s = sum(x,y); //Actual Parameters
return 0;
)
PARAMETER PASSING
Pass by Value Pass by Reference
• Values of actual parameters
are copied to formal
parameters.
• Actual and Formal refer to
different memory locations.
• Address of actual parameters
is passed.
• Actual and Formal refer to
same memory location.
#include <iostream.h>
void fun(int x)
{ x = 30; }
void main()
{ int x = 20; fun(x);
printf("x = %d", x); }
# include <iostream.h>
void fun(int *ptr)
{ *ptr = 30; }
void main()
{ int x = 20; fun(&x);
printf("x = %d", x); }
OUTPUT OUTPUT
x = 20 x = 30
RECURSION
• Recursion breaks a problem into smaller identical
problems.
• Technique in which a method can call itself to solve a
problem.
• The recursive function is
• a kind of function that calls itself, or
• a function that is part of a cycle in the sequence of function
calls.
ITERATION VS RECURSION
Iteration Recursion
iterative function is one that
loops to repeat some part
of the code
recursive function is one
that calls itself again to
repeat the code.
FACTORIAL FACTORIAL
factorial(n) = n*(n-1)*….*1
factorial(0) = 1
factorial(n) =
1 //(if n=0)
n * (factorial(n)) //(if n>0)
HOW RECURSION WORKS?
• Each call creates a new instance of all the parameters and
the local variables
• As always, when the method completes, control returns to
the method that invoked it (which might be another
invocation of the same method)
• factorial(3) = 3 * factorial(3-1)
• = 3 * 2 * factorial(2-1)
• = 3 * 2 * 1 * factorial(1-1)
• = 3 * 2 * 1 * 1
• = 6
MEMORY
Instructions of a
program are kept
here.
Variables/Data of
the function are
stored here.
Dynamic Allocation
of memory
Used to store
Global/Static
variables
TAIL RECURSION
• A recursive function is tail recursive when recursive call
is the last thing executed by the function.
void A(int n)
{
if(n>0)
{
cout<<n<<“n”;
A(n-1);
}
}
void main()
{
int x=3;
A(x);
}
1
3
5
76
4
2
HEAD RECURSION
• A call is head-recursive when the first statement of the
function is the recursive call.
void A(int n)
{
if(n>0)
{
A(n-1);
printf(“%dn”,n);
}
}
void main()
{
int x=3;
A(x);
}
1
2 7
63
4 5
DIFFERENCE
1
3
5
76
4
2
1
2 7
63
4 5
TAIL OUTPUT:
3
2
1
HEAD OUTPUT:
1
2
3
REFERENCES
• https://www.advanced-
ict.info/programming/recursion.html
• https://www.youtube.com/watch?v=ygK0YON10sQ
• https://stackoverflow.com/questions/105838/real-
world-examples-of-recursion
• https://stackoverflow.com/questions/21426688/the-
difference-between-head-tail-recursion
• https://www.geeksforgeeks.org/recursion/
• https://www.geeksforgeeks.org/tail-recursion/

Function & Recursion

  • 1.
    FUNCTIONS AND RECURSION GauravPrakash (MCA/25006/18) Roopak Bhama (MCA/25003/18) BIRLA INSTITUTE OF TECHNOLOGY MESRA, JAIPUR CAMPUS
  • 2.
    FUNCTIONS • A functionis a set of statements that take inputs, do some specific computation and produces output. OR A self contained block of statement performing a specific task. • Components • Prototype • Call • Body
  • 3.
    PROTOTYPE • Tells compilerabout • return type of function • function name • number of parameters • data types of parameters rtype fnct_name(dtype para1,…); rtype = return type of function fnct_name = name of the function dtype = data type of parameter para1 = parameter one… to ‘n’ PROTOTYPE
  • 4.
    FUNCTION CALL 1 - #include<iostream.h> 2- void prnt(); 3 - void main(){ 4 - prnt(); 5 - } 6 - void prnt(){ 7 - cout<<“Hello”; 8 - } 1 - #include<iostream.h> 2 - void prnt(); 3 - void main(){ 4 - prnt(); 5 - } 6 - void prnt(){ 7 - cout<<“Hello”; 8 - } 1 - #include<iostream.h> 2 - void prnt(); 3 - void main(){ 4 - prnt(); 5 - } 6 - void prnt(){ 7 - cout<<“Hello”; 8 - } 1 – Include header files as per requirements of program. 2 – Function declaration. Tells the program that a function named prnt() has been used. 3 – Main function starts. Program execution starts here. 4 – Function Call. Calls prnt() which returns nothing and have no parameters, program moves to line 6. 6 – Starts executing statements of prnt(). Returns back to main at line 5. Program
  • 5.
    FUNCTION BODY 1. #include<iostream.h> 2.void times(int); 3. int main(){ 1. void times(5); 4. } 5. void times(int a){ 1. for(int i=0;i<a;i++){ 1. cout<<“Hellon”; 2. } 6. } Function Body OUTPUT: Hello Hello Hello Hello Hello
  • 6.
    FUNCTIONS TYPES LIBRARY USERDEFINED Header Functions stdio printf(),scanf() math pow(), sqrt() string strrev(),strcpy() Without Argument, Without Return- type Without Argument, With Return-type With Argument, Without Return-type With Argument, With Return-type
  • 7.
    NO ARGUEMENT ANDNO RETURN VALUES #include<iostream> using namespace std; void printgreet(){ cout<<"Hellon"; } int main(){ printgreet(); return 0; } • The empty parentheses in printgreet(); indicates that no argument is passed to the function. • The return type of the function is void. Hence, no value is returned from the function.
  • 8.
    NO ARGUEMENT ANDRETURN VALUES #include<iostream.h> int printgreet(){ int i,a; cout<<"Enter times to greetn"; cin>>a; for(i=0;i<a;i++) cout<<"Hellon"; return i; } void main(){ int a; cout<<"Greeted "<<printgreet()<<" time"; } • The empty parentheses in printgreet(); statement indicates that no argument is passed to the function. • Here, the printgreet() function takes input from the user and returns i to the main().
  • 9.
    ARGUEMENT AND NORETURN VALUES #include<iostream.h> void printgreet(int a){ for(int i=0;i<a;i++) cout<<"Hellon"; } int main(){ printgreet(5); return 0; } • The integer value is passed to printgreet() function. • The program will print hello the number of times which is passed to it. • that is ‘5’ in the above program.
  • 10.
    ARGUEMENT AND RETURN VALUES #include<iostream.h> intprintgreet(int x){ for(i=0;i<x;i++) cout<<"Hellon"; return x; } int main(){ int a; cout<<"Enter numbern"; cin>>a; cout<<"Greeted "<<printgreet(a)<<" times"; return 0; } • The input from the user is passed to printgreet() function. • The printgreet() function returns the number of times the greeting message has been printed. • Then the program returns ‘x’ to the main().
  • 11.
    ACTUAL VS FORMAL PARAMETERS #include<iostream.h> intsum(int a, int b) //Formal Parameters { return a+b; } int main() { int x=10,y=20; int s = sum(x,y); //Actual Parameters return 0; )
  • 12.
    PARAMETER PASSING Pass byValue Pass by Reference • Values of actual parameters are copied to formal parameters. • Actual and Formal refer to different memory locations. • Address of actual parameters is passed. • Actual and Formal refer to same memory location. #include <iostream.h> void fun(int x) { x = 30; } void main() { int x = 20; fun(x); printf("x = %d", x); } # include <iostream.h> void fun(int *ptr) { *ptr = 30; } void main() { int x = 20; fun(&x); printf("x = %d", x); } OUTPUT OUTPUT x = 20 x = 30
  • 13.
    RECURSION • Recursion breaksa problem into smaller identical problems. • Technique in which a method can call itself to solve a problem. • The recursive function is • a kind of function that calls itself, or • a function that is part of a cycle in the sequence of function calls.
  • 14.
    ITERATION VS RECURSION IterationRecursion iterative function is one that loops to repeat some part of the code recursive function is one that calls itself again to repeat the code. FACTORIAL FACTORIAL factorial(n) = n*(n-1)*….*1 factorial(0) = 1 factorial(n) = 1 //(if n=0) n * (factorial(n)) //(if n>0)
  • 15.
    HOW RECURSION WORKS? •Each call creates a new instance of all the parameters and the local variables • As always, when the method completes, control returns to the method that invoked it (which might be another invocation of the same method) • factorial(3) = 3 * factorial(3-1) • = 3 * 2 * factorial(2-1) • = 3 * 2 * 1 * factorial(1-1) • = 3 * 2 * 1 * 1 • = 6
  • 16.
    MEMORY Instructions of a programare kept here. Variables/Data of the function are stored here. Dynamic Allocation of memory Used to store Global/Static variables
  • 17.
    TAIL RECURSION • Arecursive function is tail recursive when recursive call is the last thing executed by the function. void A(int n) { if(n>0) { cout<<n<<“n”; A(n-1); } } void main() { int x=3; A(x); } 1 3 5 76 4 2
  • 18.
    HEAD RECURSION • Acall is head-recursive when the first statement of the function is the recursive call. void A(int n) { if(n>0) { A(n-1); printf(“%dn”,n); } } void main() { int x=3; A(x); } 1 2 7 63 4 5
  • 19.
    DIFFERENCE 1 3 5 76 4 2 1 2 7 63 4 5 TAILOUTPUT: 3 2 1 HEAD OUTPUT: 1 2 3
  • 20.
    REFERENCES • https://www.advanced- ict.info/programming/recursion.html • https://www.youtube.com/watch?v=ygK0YON10sQ •https://stackoverflow.com/questions/105838/real- world-examples-of-recursion • https://stackoverflow.com/questions/21426688/the- difference-between-head-tail-recursion • https://www.geeksforgeeks.org/recursion/ • https://www.geeksforgeeks.org/tail-recursion/