SlideShare a Scribd company logo
1 of 20
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/

More Related Content

What's hot (20)

functions of C++
functions of C++functions of C++
functions of C++
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
6-Python-Recursion PPT.pptx
6-Python-Recursion PPT.pptx6-Python-Recursion PPT.pptx
6-Python-Recursion PPT.pptx
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
 
Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Class and object
Class and objectClass and object
Class and object
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Python basic
Python basicPython basic
Python basic
 
C functions
C functionsC functions
C functions
 
16 virtual function
16 virtual function16 virtual function
16 virtual function
 
Characteristics of OOPS
Characteristics of OOPS Characteristics of OOPS
Characteristics of OOPS
 
Function
FunctionFunction
Function
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 
Type conversion
Type conversionType conversion
Type conversion
 
C function
C functionC function
C function
 
Function in C Language
Function in C Language Function in C Language
Function in C Language
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
 

Similar to FUNCTIONS AND RECURSION: KEY CONCEPTS

Similar to FUNCTIONS AND RECURSION: KEY CONCEPTS (20)

Array Cont
Array ContArray Cont
Array Cont
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Functional Programming in Swift
Functional Programming in SwiftFunctional Programming in Swift
Functional Programming in Swift
 
Function in c
Function in cFunction in c
Function in c
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
 
functions
functionsfunctions
functions
 
Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01
 
functions
functionsfunctions
functions
 
New Functional Features of Java 8
New Functional Features of Java 8New Functional Features of Java 8
New Functional Features of Java 8
 
Function recap
Function recapFunction recap
Function recap
 
Function recap
Function recapFunction recap
Function recap
 
Python_Functions_Unit1.pptx
Python_Functions_Unit1.pptxPython_Functions_Unit1.pptx
Python_Functions_Unit1.pptx
 
how to reuse code
how to reuse codehow to reuse code
how to reuse code
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 
Function in c program
Function in c programFunction in c program
Function in c program
 
4th unit full
4th unit full4th unit full
4th unit full
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
 

More from Meghaj Mallick

PORTFOLIO BY USING HTML & CSS
PORTFOLIO BY USING HTML & CSSPORTFOLIO BY USING HTML & CSS
PORTFOLIO BY USING HTML & CSSMeghaj Mallick
 
Introduction to Software Testing
Introduction to Software TestingIntroduction to Software Testing
Introduction to Software TestingMeghaj Mallick
 
Introduction to System Programming
Introduction to System ProgrammingIntroduction to System Programming
Introduction to System ProgrammingMeghaj Mallick
 
Icons, Image & Multimedia
Icons, Image & MultimediaIcons, Image & Multimedia
Icons, Image & MultimediaMeghaj Mallick
 
Project Tracking & SPC
Project Tracking & SPCProject Tracking & SPC
Project Tracking & SPCMeghaj Mallick
 
Architecture and security in Vanet PPT
Architecture and security in Vanet PPTArchitecture and security in Vanet PPT
Architecture and security in Vanet PPTMeghaj Mallick
 
Design Model & User Interface Design in Software Engineering
Design Model & User Interface Design in Software EngineeringDesign Model & User Interface Design in Software Engineering
Design Model & User Interface Design in Software EngineeringMeghaj Mallick
 
Text Mining of Twitter in Data Mining
Text Mining of Twitter in Data MiningText Mining of Twitter in Data Mining
Text Mining of Twitter in Data MiningMeghaj Mallick
 
DFS & BFS in Computer Algorithm
DFS & BFS in Computer AlgorithmDFS & BFS in Computer Algorithm
DFS & BFS in Computer AlgorithmMeghaj Mallick
 
Software Development Method
Software Development MethodSoftware Development Method
Software Development MethodMeghaj Mallick
 
Secant method in Numerical & Statistical Method
Secant method in Numerical & Statistical MethodSecant method in Numerical & Statistical Method
Secant method in Numerical & Statistical MethodMeghaj Mallick
 
Motivation in Organization
Motivation in OrganizationMotivation in Organization
Motivation in OrganizationMeghaj Mallick
 
Partial-Orderings in Discrete Mathematics
 Partial-Orderings in Discrete Mathematics Partial-Orderings in Discrete Mathematics
Partial-Orderings in Discrete MathematicsMeghaj Mallick
 
Hashing In Data Structure
Hashing In Data Structure Hashing In Data Structure
Hashing In Data Structure Meghaj Mallick
 

More from Meghaj Mallick (20)

24 partial-orderings
24 partial-orderings24 partial-orderings
24 partial-orderings
 
PORTFOLIO BY USING HTML & CSS
PORTFOLIO BY USING HTML & CSSPORTFOLIO BY USING HTML & CSS
PORTFOLIO BY USING HTML & CSS
 
Introduction to Software Testing
Introduction to Software TestingIntroduction to Software Testing
Introduction to Software Testing
 
Introduction to System Programming
Introduction to System ProgrammingIntroduction to System Programming
Introduction to System Programming
 
MACRO ASSEBLER
MACRO ASSEBLERMACRO ASSEBLER
MACRO ASSEBLER
 
Icons, Image & Multimedia
Icons, Image & MultimediaIcons, Image & Multimedia
Icons, Image & Multimedia
 
Project Tracking & SPC
Project Tracking & SPCProject Tracking & SPC
Project Tracking & SPC
 
Peephole Optimization
Peephole OptimizationPeephole Optimization
Peephole Optimization
 
Routing in MANET
Routing in MANETRouting in MANET
Routing in MANET
 
Macro assembler
 Macro assembler Macro assembler
Macro assembler
 
Architecture and security in Vanet PPT
Architecture and security in Vanet PPTArchitecture and security in Vanet PPT
Architecture and security in Vanet PPT
 
Design Model & User Interface Design in Software Engineering
Design Model & User Interface Design in Software EngineeringDesign Model & User Interface Design in Software Engineering
Design Model & User Interface Design in Software Engineering
 
Text Mining of Twitter in Data Mining
Text Mining of Twitter in Data MiningText Mining of Twitter in Data Mining
Text Mining of Twitter in Data Mining
 
DFS & BFS in Computer Algorithm
DFS & BFS in Computer AlgorithmDFS & BFS in Computer Algorithm
DFS & BFS in Computer Algorithm
 
Software Development Method
Software Development MethodSoftware Development Method
Software Development Method
 
Secant method in Numerical & Statistical Method
Secant method in Numerical & Statistical MethodSecant method in Numerical & Statistical Method
Secant method in Numerical & Statistical Method
 
Motivation in Organization
Motivation in OrganizationMotivation in Organization
Motivation in Organization
 
Communication Skill
Communication SkillCommunication Skill
Communication Skill
 
Partial-Orderings in Discrete Mathematics
 Partial-Orderings in Discrete Mathematics Partial-Orderings in Discrete Mathematics
Partial-Orderings in Discrete Mathematics
 
Hashing In Data Structure
Hashing In Data Structure Hashing In Data Structure
Hashing In Data Structure
 

Recently uploaded

SBFT Tool Competition 2024 -- Python Test Case Generation Track
SBFT Tool Competition 2024 -- Python Test Case Generation TrackSBFT Tool Competition 2024 -- Python Test Case Generation Track
SBFT Tool Competition 2024 -- Python Test Case Generation TrackSebastiano Panichella
 
Work Remotely with Confluence ACE 2.pptx
Work Remotely with Confluence ACE 2.pptxWork Remotely with Confluence ACE 2.pptx
Work Remotely with Confluence ACE 2.pptxmavinoikein
 
Genshin Impact PPT Template by EaTemp.pptx
Genshin Impact PPT Template by EaTemp.pptxGenshin Impact PPT Template by EaTemp.pptx
Genshin Impact PPT Template by EaTemp.pptxJohnree4
 
OSCamp Kubernetes 2024 | Zero-Touch OS-Infrastruktur für Container und Kubern...
OSCamp Kubernetes 2024 | Zero-Touch OS-Infrastruktur für Container und Kubern...OSCamp Kubernetes 2024 | Zero-Touch OS-Infrastruktur für Container und Kubern...
OSCamp Kubernetes 2024 | Zero-Touch OS-Infrastruktur für Container und Kubern...NETWAYS
 
Mathan flower ppt.pptx slide orchids ✨🌸
Mathan flower ppt.pptx slide orchids ✨🌸Mathan flower ppt.pptx slide orchids ✨🌸
Mathan flower ppt.pptx slide orchids ✨🌸mathanramanathan2005
 
Event 4 Introduction to Open Source.pptx
Event 4 Introduction to Open Source.pptxEvent 4 Introduction to Open Source.pptx
Event 4 Introduction to Open Source.pptxaryanv1753
 
LANDMARKS AND MONUMENTS IN NIGERIA.pptx
LANDMARKS  AND MONUMENTS IN NIGERIA.pptxLANDMARKS  AND MONUMENTS IN NIGERIA.pptx
LANDMARKS AND MONUMENTS IN NIGERIA.pptxBasil Achie
 
The Ten Facts About People With Autism Presentation
The Ten Facts About People With Autism PresentationThe Ten Facts About People With Autism Presentation
The Ten Facts About People With Autism PresentationNathan Young
 
Simulation-based Testing of Unmanned Aerial Vehicles with Aerialist
Simulation-based Testing of Unmanned Aerial Vehicles with AerialistSimulation-based Testing of Unmanned Aerial Vehicles with Aerialist
Simulation-based Testing of Unmanned Aerial Vehicles with AerialistSebastiano Panichella
 
Open Source Strategy in Logistics 2015_Henrik Hankedvz-d-nl-log-conference.pdf
Open Source Strategy in Logistics 2015_Henrik Hankedvz-d-nl-log-conference.pdfOpen Source Strategy in Logistics 2015_Henrik Hankedvz-d-nl-log-conference.pdf
Open Source Strategy in Logistics 2015_Henrik Hankedvz-d-nl-log-conference.pdfhenrik385807
 
Open Source Camp Kubernetes 2024 | Running WebAssembly on Kubernetes by Alex ...
Open Source Camp Kubernetes 2024 | Running WebAssembly on Kubernetes by Alex ...Open Source Camp Kubernetes 2024 | Running WebAssembly on Kubernetes by Alex ...
Open Source Camp Kubernetes 2024 | Running WebAssembly on Kubernetes by Alex ...NETWAYS
 
Open Source Camp Kubernetes 2024 | Monitoring Kubernetes With Icinga by Eric ...
Open Source Camp Kubernetes 2024 | Monitoring Kubernetes With Icinga by Eric ...Open Source Camp Kubernetes 2024 | Monitoring Kubernetes With Icinga by Eric ...
Open Source Camp Kubernetes 2024 | Monitoring Kubernetes With Icinga by Eric ...NETWAYS
 
Genesis part 2 Isaiah Scudder 04-24-2024.pptx
Genesis part 2 Isaiah Scudder 04-24-2024.pptxGenesis part 2 Isaiah Scudder 04-24-2024.pptx
Genesis part 2 Isaiah Scudder 04-24-2024.pptxFamilyWorshipCenterD
 
Gaps, Issues and Challenges in the Implementation of Mother Tongue Based-Mult...
Gaps, Issues and Challenges in the Implementation of Mother Tongue Based-Mult...Gaps, Issues and Challenges in the Implementation of Mother Tongue Based-Mult...
Gaps, Issues and Challenges in the Implementation of Mother Tongue Based-Mult...marjmae69
 
call girls in delhi malviya nagar @9811711561@
call girls in delhi malviya nagar @9811711561@call girls in delhi malviya nagar @9811711561@
call girls in delhi malviya nagar @9811711561@vikas rana
 
OSCamp Kubernetes 2024 | SRE Challenges in Monolith to Microservices Shift at...
OSCamp Kubernetes 2024 | SRE Challenges in Monolith to Microservices Shift at...OSCamp Kubernetes 2024 | SRE Challenges in Monolith to Microservices Shift at...
OSCamp Kubernetes 2024 | SRE Challenges in Monolith to Microservices Shift at...NETWAYS
 
Presentation for the Strategic Dialogue on the Future of Agriculture, Brussel...
Presentation for the Strategic Dialogue on the Future of Agriculture, Brussel...Presentation for the Strategic Dialogue on the Future of Agriculture, Brussel...
Presentation for the Strategic Dialogue on the Future of Agriculture, Brussel...Krijn Poppe
 
NATIONAL ANTHEMS OF AFRICA (National Anthems of Africa)
NATIONAL ANTHEMS OF AFRICA (National Anthems of Africa)NATIONAL ANTHEMS OF AFRICA (National Anthems of Africa)
NATIONAL ANTHEMS OF AFRICA (National Anthems of Africa)Basil Achie
 
Exploring protein-protein interactions by Weak Affinity Chromatography (WAC) ...
Exploring protein-protein interactions by Weak Affinity Chromatography (WAC) ...Exploring protein-protein interactions by Weak Affinity Chromatography (WAC) ...
Exploring protein-protein interactions by Weak Affinity Chromatography (WAC) ...Salam Al-Karadaghi
 
Call Girls in Rohini Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Rohini Delhi 💯Call Us 🔝8264348440🔝Call Girls in Rohini Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Rohini Delhi 💯Call Us 🔝8264348440🔝soniya singh
 

Recently uploaded (20)

SBFT Tool Competition 2024 -- Python Test Case Generation Track
SBFT Tool Competition 2024 -- Python Test Case Generation TrackSBFT Tool Competition 2024 -- Python Test Case Generation Track
SBFT Tool Competition 2024 -- Python Test Case Generation Track
 
Work Remotely with Confluence ACE 2.pptx
Work Remotely with Confluence ACE 2.pptxWork Remotely with Confluence ACE 2.pptx
Work Remotely with Confluence ACE 2.pptx
 
Genshin Impact PPT Template by EaTemp.pptx
Genshin Impact PPT Template by EaTemp.pptxGenshin Impact PPT Template by EaTemp.pptx
Genshin Impact PPT Template by EaTemp.pptx
 
OSCamp Kubernetes 2024 | Zero-Touch OS-Infrastruktur für Container und Kubern...
OSCamp Kubernetes 2024 | Zero-Touch OS-Infrastruktur für Container und Kubern...OSCamp Kubernetes 2024 | Zero-Touch OS-Infrastruktur für Container und Kubern...
OSCamp Kubernetes 2024 | Zero-Touch OS-Infrastruktur für Container und Kubern...
 
Mathan flower ppt.pptx slide orchids ✨🌸
Mathan flower ppt.pptx slide orchids ✨🌸Mathan flower ppt.pptx slide orchids ✨🌸
Mathan flower ppt.pptx slide orchids ✨🌸
 
Event 4 Introduction to Open Source.pptx
Event 4 Introduction to Open Source.pptxEvent 4 Introduction to Open Source.pptx
Event 4 Introduction to Open Source.pptx
 
LANDMARKS AND MONUMENTS IN NIGERIA.pptx
LANDMARKS  AND MONUMENTS IN NIGERIA.pptxLANDMARKS  AND MONUMENTS IN NIGERIA.pptx
LANDMARKS AND MONUMENTS IN NIGERIA.pptx
 
The Ten Facts About People With Autism Presentation
The Ten Facts About People With Autism PresentationThe Ten Facts About People With Autism Presentation
The Ten Facts About People With Autism Presentation
 
Simulation-based Testing of Unmanned Aerial Vehicles with Aerialist
Simulation-based Testing of Unmanned Aerial Vehicles with AerialistSimulation-based Testing of Unmanned Aerial Vehicles with Aerialist
Simulation-based Testing of Unmanned Aerial Vehicles with Aerialist
 
Open Source Strategy in Logistics 2015_Henrik Hankedvz-d-nl-log-conference.pdf
Open Source Strategy in Logistics 2015_Henrik Hankedvz-d-nl-log-conference.pdfOpen Source Strategy in Logistics 2015_Henrik Hankedvz-d-nl-log-conference.pdf
Open Source Strategy in Logistics 2015_Henrik Hankedvz-d-nl-log-conference.pdf
 
Open Source Camp Kubernetes 2024 | Running WebAssembly on Kubernetes by Alex ...
Open Source Camp Kubernetes 2024 | Running WebAssembly on Kubernetes by Alex ...Open Source Camp Kubernetes 2024 | Running WebAssembly on Kubernetes by Alex ...
Open Source Camp Kubernetes 2024 | Running WebAssembly on Kubernetes by Alex ...
 
Open Source Camp Kubernetes 2024 | Monitoring Kubernetes With Icinga by Eric ...
Open Source Camp Kubernetes 2024 | Monitoring Kubernetes With Icinga by Eric ...Open Source Camp Kubernetes 2024 | Monitoring Kubernetes With Icinga by Eric ...
Open Source Camp Kubernetes 2024 | Monitoring Kubernetes With Icinga by Eric ...
 
Genesis part 2 Isaiah Scudder 04-24-2024.pptx
Genesis part 2 Isaiah Scudder 04-24-2024.pptxGenesis part 2 Isaiah Scudder 04-24-2024.pptx
Genesis part 2 Isaiah Scudder 04-24-2024.pptx
 
Gaps, Issues and Challenges in the Implementation of Mother Tongue Based-Mult...
Gaps, Issues and Challenges in the Implementation of Mother Tongue Based-Mult...Gaps, Issues and Challenges in the Implementation of Mother Tongue Based-Mult...
Gaps, Issues and Challenges in the Implementation of Mother Tongue Based-Mult...
 
call girls in delhi malviya nagar @9811711561@
call girls in delhi malviya nagar @9811711561@call girls in delhi malviya nagar @9811711561@
call girls in delhi malviya nagar @9811711561@
 
OSCamp Kubernetes 2024 | SRE Challenges in Monolith to Microservices Shift at...
OSCamp Kubernetes 2024 | SRE Challenges in Monolith to Microservices Shift at...OSCamp Kubernetes 2024 | SRE Challenges in Monolith to Microservices Shift at...
OSCamp Kubernetes 2024 | SRE Challenges in Monolith to Microservices Shift at...
 
Presentation for the Strategic Dialogue on the Future of Agriculture, Brussel...
Presentation for the Strategic Dialogue on the Future of Agriculture, Brussel...Presentation for the Strategic Dialogue on the Future of Agriculture, Brussel...
Presentation for the Strategic Dialogue on the Future of Agriculture, Brussel...
 
NATIONAL ANTHEMS OF AFRICA (National Anthems of Africa)
NATIONAL ANTHEMS OF AFRICA (National Anthems of Africa)NATIONAL ANTHEMS OF AFRICA (National Anthems of Africa)
NATIONAL ANTHEMS OF AFRICA (National Anthems of Africa)
 
Exploring protein-protein interactions by Weak Affinity Chromatography (WAC) ...
Exploring protein-protein interactions by Weak Affinity Chromatography (WAC) ...Exploring protein-protein interactions by Weak Affinity Chromatography (WAC) ...
Exploring protein-protein interactions by Weak Affinity Chromatography (WAC) ...
 
Call Girls in Rohini Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Rohini Delhi 💯Call Us 🔝8264348440🔝Call Girls in Rohini Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Rohini Delhi 💯Call Us 🔝8264348440🔝
 

FUNCTIONS AND RECURSION: KEY CONCEPTS

  • 1. FUNCTIONS AND RECURSION Gaurav Prakash (MCA/25006/18) Roopak Bhama (MCA/25003/18) BIRLA INSTITUTE OF TECHNOLOGY MESRA, JAIPUR CAMPUS
  • 2. 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
  • 3. 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
  • 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 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
  • 7. 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.
  • 8. 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().
  • 9. 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.
  • 10. 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().
  • 11. 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; )
  • 12. 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
  • 13. 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.
  • 14. 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)
  • 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 program are kept here. Variables/Data of the function are stored here. Dynamic Allocation of memory Used to store Global/Static variables
  • 17. 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
  • 18. 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
  • 19. DIFFERENCE 1 3 5 76 4 2 1 2 7 63 4 5 TAIL OUTPUT: 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/