SlideShare a Scribd company logo
Unit IV
Function & Recursion
Functions
 A function is a self-contained block of
statements that perform a coherent
task of some kind. If you have a task
that is always performed in the same
way than instead of putting the same
functionality at every point we should
make function and can call by just
declaring function name.
#include<stdio.h>
void func1();
int main()
{
func1();
printf("nthis is first program with funtion");
return 0;
}
void func1()
{
printf("nthis is func1");
}
Output:
This is func1
This is the first program with function
Important points about function
 A function gets called when the function name is followed by a semincolon.
 A function is defined when function name is followed by a pair of braces in
which one or more statements may be present.
 Any function can be called from any other function. Even main can be called
from other function.
 A function calls itself such process known as recursion.
Elements of Functions
 Function Definition
 Function Call
 Function Declaration
Definition of Function
int func1()
{
int a;
printf("nthis is func1");
return 0;
}
Function Definition-
1. Function name
2. Function type
3. List of parameters
4. Local variable declaration
5. Function statement
6. A return statement
Return values and their type
A function may or may not send back any value to the calling function. If it does it is done through return
statement. Called function can only return only one value per call.
return;
or
return(expression)
return: will not return any value but will return the control back to the calling function.
If(error)
{
return(0);
}
Return expression: will return some value of the function back to the called function.
return(p);
return(x*y);
…
So basically, return serves two purposes:
 On executing the return statement, it immediately transfers the control back to
the calling function.
 It returns the value present in the parentheses after return, to the calling function.
Passing values between functions-
 Passing the vales to the function allows us to communicate with the function. Using this
we can pass our values in order to get the results, for that we need to specify the
function name with arguments.
 Example:
void main()
{
Funtion1();
Function2(a);
}
Function1()
{ }
Function2(int a)
{ }
Categories of Function-
 Functions with no arguments and no return value.
 Functions with arguments and no return value.
 Functions with no arguments and return a value.
 Functions that return multiple return values.
Swapping a Number – General
#include <stdio.h>
int main()
{
int x, y, t;
printf("Enter two integersn");
scanf("%d%d", &x, &y);
printf("Before SwappingnFirst integer = %dnSecond integer = %dn", x, y);
t = x;
x = y;
y = t;
printf("After SwappingnFirst integer = %dnSecond integer = %dn", x, y);
return 0;
}
#include <stdio.h>
int main()
{
int x, y, t;
printf("Enter two integersn");
scanf("%d%d", &x, &y);
printf("Before SwappingnFirst integer = %dnSecond integer = %dn", x, y);
swap(x,y);
printf("After SwappingnFirst integer = %dnSecond integer = %dn", x, y);
return 0;
}
void swap(int x, int y) //Swap function definition
{
int t;
t = x;
x = y;
y = t;
printf("After SwappingnFirst integer = %dnSecond integer = %dn", x, y);
}
Swapping a number
#include <stdio.h>
void swap(int*, int*); //Swap function declaration
int main()
{
int x, y;
printf("Enter the value of x and yn");
scanf("%d%d",&x,&y);
printf("Before Swappingnx = %dny = %dn", x, y);
swap(&x, &y);
printf("After Swappingnx = %dny = %dn", x, y);
return 0;
}
void swap(int *a, int *b) //Swap function definition
{
int t;
t = *b;
*b = *a;
*a = t;
}
Recursion
 Recursion is a special case where
function calls itself again and
again.
main()
{
printf(“this is the example of recursion”);
main();
}
…
Another example is factorial:
Factorial of n=n(n-1)(n-2)…….1
Factorial of 4=4*3*2*1 =24
factorial(int n)
{
int fact;
if(n==1)
return 1;
else
fact=n*factorial(n-1);
return(fact);
}
fact=3*factorial(2)
fact=3*2*factorial(1)
fact=3*2*1
fact=6
Tower of Hanoi
 Tower of Hanoi is a mathematical puzzle where we have three rods and n
disks. The objective of the puzzle is to move the entire stack to another
rod, obeying the following simple rules:
1. Only one disk can be moved at a time.
2. Each move consists of taking the upper disk from one of the stacks and
placing it on top of another stack i.e. a disk can only be moved if it is the
uppermost disk on a stack.
3. No disk may be placed on top of a smaller disk.
…
…
Desired Input/output
 Input : 2
Output : Disk 1 moved from A to B
Disk 2 moved from A to C
Disk 1 moved from B to C
 Input : 3
Output : Disk 1 moved from A to C
Disk 2 moved from A to B
Disk 1 moved from C to B
Disk 3 moved from A to C
Disk 1 moved from B to A
Disk 2 moved from B to C
Disk 1 moved from A to C
#include <stdio.h>
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod)
{
if (n==1)
{
printf("nMove disk 1 from rod : %d",from_rod);
printf("nMove disk 1 to rod : %d",to_rod);
return;
}
towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);
printf("nMove disk %d from rod : %d",n,from_rod);
printf("nMove disk %d to rod : %d",n,to_rod);
towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);
}
int main()
{
int n = 3; // Number of disks
towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods
return 0;
}
Ad

More Related Content

What's hot (20)

Linked list implementation of Queue
Linked list implementation of QueueLinked list implementation of Queue
Linked list implementation of Queue
Dr. Sindhia Lingaswamy
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
أحمد محمد
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
Janki Shah
 
Stack using Array
Stack using ArrayStack using Array
Stack using Array
Sayantan Sur
 
Selection sort
Selection sortSelection sort
Selection sort
smlagustin
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
Trupti Agrawal
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
Expression trees
Expression treesExpression trees
Expression trees
Salman Vadsarya
 
Infix to postfix conversion
Infix to postfix conversionInfix to postfix conversion
Infix to postfix conversion
Then Murugeshwari
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
FarihaHabib123
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
Sowmya Jyothi
 
Searching in c language
Searching in c languageSearching in c language
Searching in c language
CHANDAN KUMAR
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
Tirthika Bandi
 
Computer notes - Expression Tree
Computer notes - Expression TreeComputer notes - Expression Tree
Computer notes - Expression Tree
ecomputernotes
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
Tech_MX
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Muhammad Hammad Waseem
 
Java Tokens
Java  TokensJava  Tokens
Java Tokens
Madishetty Prathibha
 
Java Linked List Tutorial | Edureka
Java Linked List Tutorial |  EdurekaJava Linked List Tutorial |  Edureka
Java Linked List Tutorial | Edureka
Edureka!
 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm
KristinaBorooah
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
Rokonuzzaman Rony
 

Similar to Functions & Recursion (20)

TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class Functions
Eelco Visser
 
functions of C++
functions of C++functions of C++
functions of C++
tarandeep_kaur
 
Stack implementation using linked list ppt
Stack implementation using linked list pptStack implementation using linked list ppt
Stack implementation using linked list ppt
JayasankarShyam
 
C and C++ functions
C and C++ functionsC and C++ functions
C and C++ functions
kavitha muneeshwaran
 
Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )
Ziyauddin Shaik
 
The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202
Mahmoud Samir Fayed
 
functionsamplejfjfjfjfjfhjfjfhjfgjfg_v1.ppt
functionsamplejfjfjfjfjfhjfjfhjfgjfg_v1.pptfunctionsamplejfjfjfjfjfhjfjfhjfgjfg_v1.ppt
functionsamplejfjfjfjfjfhjfjfhjfgjfg_v1.ppt
RoselinLourd
 
functions
functionsfunctions
functions
Makwana Bhavesh
 
Recursion in C
Recursion in CRecursion in C
Recursion in C
v_jk
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
v_jk
 
function_v1fgdfdf5645ythyth6ythythgbg.ppt
function_v1fgdfdf5645ythyth6ythythgbg.pptfunction_v1fgdfdf5645ythyth6ythythgbg.ppt
function_v1fgdfdf5645ythyth6ythythgbg.ppt
RoselinLourd
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
ssuser823678
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
ssuser2076d9
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
alish sha
 
Pro
ProPro
Pro
TeshaleSiyum
 
Unit-I Recursion.pptx
Unit-I Recursion.pptxUnit-I Recursion.pptx
Unit-I Recursion.pptx
ajajkhan16
 
Scala functions
Scala functionsScala functions
Scala functions
Knoldus Inc.
 
Function in c
Function in cFunction in c
Function in c
Raj Tandukar
 
Recursion concepts by Divya
Recursion concepts by DivyaRecursion concepts by Divya
Recursion concepts by Divya
Divya Kumari
 
The Ring programming language version 1.5.2 book - Part 31 of 181
The Ring programming language version 1.5.2 book - Part 31 of 181The Ring programming language version 1.5.2 book - Part 31 of 181
The Ring programming language version 1.5.2 book - Part 31 of 181
Mahmoud Samir Fayed
 
TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class Functions
Eelco Visser
 
Stack implementation using linked list ppt
Stack implementation using linked list pptStack implementation using linked list ppt
Stack implementation using linked list ppt
JayasankarShyam
 
Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )
Ziyauddin Shaik
 
The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202
Mahmoud Samir Fayed
 
functionsamplejfjfjfjfjfhjfjfhjfgjfg_v1.ppt
functionsamplejfjfjfjfjfhjfjfhjfgjfg_v1.pptfunctionsamplejfjfjfjfjfhjfjfhjfgjfg_v1.ppt
functionsamplejfjfjfjfjfhjfjfhjfgjfg_v1.ppt
RoselinLourd
 
Recursion in C
Recursion in CRecursion in C
Recursion in C
v_jk
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
v_jk
 
function_v1fgdfdf5645ythyth6ythythgbg.ppt
function_v1fgdfdf5645ythyth6ythythgbg.pptfunction_v1fgdfdf5645ythyth6ythythgbg.ppt
function_v1fgdfdf5645ythyth6ythythgbg.ppt
RoselinLourd
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
alish sha
 
Unit-I Recursion.pptx
Unit-I Recursion.pptxUnit-I Recursion.pptx
Unit-I Recursion.pptx
ajajkhan16
 
Recursion concepts by Divya
Recursion concepts by DivyaRecursion concepts by Divya
Recursion concepts by Divya
Divya Kumari
 
The Ring programming language version 1.5.2 book - Part 31 of 181
The Ring programming language version 1.5.2 book - Part 31 of 181The Ring programming language version 1.5.2 book - Part 31 of 181
The Ring programming language version 1.5.2 book - Part 31 of 181
Mahmoud Samir Fayed
 
Ad

More from Nishant Munjal (20)

Database Management System
Database Management SystemDatabase Management System
Database Management System
Nishant Munjal
 
Array, string and pointer
Array, string and pointerArray, string and pointer
Array, string and pointer
Nishant Munjal
 
Programming in C
Programming in CProgramming in C
Programming in C
Nishant Munjal
 
Introduction to computers
Introduction to computersIntroduction to computers
Introduction to computers
Nishant Munjal
 
Unix Administration
Unix AdministrationUnix Administration
Unix Administration
Nishant Munjal
 
Shell Programming Concept
Shell Programming ConceptShell Programming Concept
Shell Programming Concept
Nishant Munjal
 
VI Editor
VI EditorVI Editor
VI Editor
Nishant Munjal
 
Introduction to Unix
Introduction to UnixIntroduction to Unix
Introduction to Unix
Nishant Munjal
 
Routing Techniques
Routing TechniquesRouting Techniques
Routing Techniques
Nishant Munjal
 
Asynchronous Transfer Mode
Asynchronous Transfer ModeAsynchronous Transfer Mode
Asynchronous Transfer Mode
Nishant Munjal
 
Overview of Cloud Computing
Overview of Cloud ComputingOverview of Cloud Computing
Overview of Cloud Computing
Nishant Munjal
 
SQL Queries Information
SQL Queries InformationSQL Queries Information
SQL Queries Information
Nishant Munjal
 
Database Design and Normalization Techniques
Database Design and Normalization TechniquesDatabase Design and Normalization Techniques
Database Design and Normalization Techniques
Nishant Munjal
 
Concurrency Control
Concurrency ControlConcurrency Control
Concurrency Control
Nishant Munjal
 
Transaction Processing Concept
Transaction Processing ConceptTransaction Processing Concept
Transaction Processing Concept
Nishant Munjal
 
Database Management System
Database Management SystemDatabase Management System
Database Management System
Nishant Munjal
 
Relational Data Model Introduction
Relational Data Model IntroductionRelational Data Model Introduction
Relational Data Model Introduction
Nishant Munjal
 
Virtualization, A Concept Implementation of Cloud
Virtualization, A Concept Implementation of CloudVirtualization, A Concept Implementation of Cloud
Virtualization, A Concept Implementation of Cloud
Nishant Munjal
 
Technical education benchmarks
Technical education benchmarksTechnical education benchmarks
Technical education benchmarks
Nishant Munjal
 
Bluemix Introduction
Bluemix IntroductionBluemix Introduction
Bluemix Introduction
Nishant Munjal
 
Database Management System
Database Management SystemDatabase Management System
Database Management System
Nishant Munjal
 
Array, string and pointer
Array, string and pointerArray, string and pointer
Array, string and pointer
Nishant Munjal
 
Introduction to computers
Introduction to computersIntroduction to computers
Introduction to computers
Nishant Munjal
 
Shell Programming Concept
Shell Programming ConceptShell Programming Concept
Shell Programming Concept
Nishant Munjal
 
Asynchronous Transfer Mode
Asynchronous Transfer ModeAsynchronous Transfer Mode
Asynchronous Transfer Mode
Nishant Munjal
 
Overview of Cloud Computing
Overview of Cloud ComputingOverview of Cloud Computing
Overview of Cloud Computing
Nishant Munjal
 
SQL Queries Information
SQL Queries InformationSQL Queries Information
SQL Queries Information
Nishant Munjal
 
Database Design and Normalization Techniques
Database Design and Normalization TechniquesDatabase Design and Normalization Techniques
Database Design and Normalization Techniques
Nishant Munjal
 
Transaction Processing Concept
Transaction Processing ConceptTransaction Processing Concept
Transaction Processing Concept
Nishant Munjal
 
Database Management System
Database Management SystemDatabase Management System
Database Management System
Nishant Munjal
 
Relational Data Model Introduction
Relational Data Model IntroductionRelational Data Model Introduction
Relational Data Model Introduction
Nishant Munjal
 
Virtualization, A Concept Implementation of Cloud
Virtualization, A Concept Implementation of CloudVirtualization, A Concept Implementation of Cloud
Virtualization, A Concept Implementation of Cloud
Nishant Munjal
 
Technical education benchmarks
Technical education benchmarksTechnical education benchmarks
Technical education benchmarks
Nishant Munjal
 
Ad

Recently uploaded (20)

22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
Guru Nanak Technical Institutions
 
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdf
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdfSmart City is the Future EN - 2024 Thailand Modify V1.0.pdf
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdf
PawachMetharattanara
 
Frontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend EngineersFrontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend Engineers
Michael Hertzberg
 
twin tower attack 2001 new york city
twin  tower  attack  2001 new  york citytwin  tower  attack  2001 new  york city
twin tower attack 2001 new york city
harishreemavs
 
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic AlgorithmDesign Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Journal of Soft Computing in Civil Engineering
 
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdfLittle Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
gori42199
 
AI Chatbots & Software Development Teams
AI Chatbots & Software Development TeamsAI Chatbots & Software Development Teams
AI Chatbots & Software Development Teams
Joe Krall
 
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdfML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
rameshwarchintamani
 
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdfATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ssuserda39791
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
Machine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATIONMachine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATION
DarrinBright1
 
OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...
OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...
OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...
ijdmsjournal
 
Deepfake Phishing: A New Frontier in Cyber Threats
Deepfake Phishing: A New Frontier in Cyber ThreatsDeepfake Phishing: A New Frontier in Cyber Threats
Deepfake Phishing: A New Frontier in Cyber Threats
RaviKumar256934
 
vtc2018fall_otfs_tutorial_presentation_1.pdf
vtc2018fall_otfs_tutorial_presentation_1.pdfvtc2018fall_otfs_tutorial_presentation_1.pdf
vtc2018fall_otfs_tutorial_presentation_1.pdf
RaghavaGD1
 
Physical and Physic-Chemical Based Optimization Methods: A Review
Physical and Physic-Chemical Based Optimization Methods: A ReviewPhysical and Physic-Chemical Based Optimization Methods: A Review
Physical and Physic-Chemical Based Optimization Methods: A Review
Journal of Soft Computing in Civil Engineering
 
Personal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.pptPersonal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.ppt
ganjangbegu579
 
Environment .................................
Environment .................................Environment .................................
Environment .................................
shadyozq9
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
acid base ppt and their specific application in food
acid base ppt and their specific application in foodacid base ppt and their specific application in food
acid base ppt and their specific application in food
Fatehatun Noor
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdf
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdfSmart City is the Future EN - 2024 Thailand Modify V1.0.pdf
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdf
PawachMetharattanara
 
Frontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend EngineersFrontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend Engineers
Michael Hertzberg
 
twin tower attack 2001 new york city
twin  tower  attack  2001 new  york citytwin  tower  attack  2001 new  york city
twin tower attack 2001 new york city
harishreemavs
 
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdfLittle Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
gori42199
 
AI Chatbots & Software Development Teams
AI Chatbots & Software Development TeamsAI Chatbots & Software Development Teams
AI Chatbots & Software Development Teams
Joe Krall
 
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdfML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
rameshwarchintamani
 
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdfATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ssuserda39791
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
Machine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATIONMachine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATION
DarrinBright1
 
OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...
OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...
OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...
ijdmsjournal
 
Deepfake Phishing: A New Frontier in Cyber Threats
Deepfake Phishing: A New Frontier in Cyber ThreatsDeepfake Phishing: A New Frontier in Cyber Threats
Deepfake Phishing: A New Frontier in Cyber Threats
RaviKumar256934
 
vtc2018fall_otfs_tutorial_presentation_1.pdf
vtc2018fall_otfs_tutorial_presentation_1.pdfvtc2018fall_otfs_tutorial_presentation_1.pdf
vtc2018fall_otfs_tutorial_presentation_1.pdf
RaghavaGD1
 
Personal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.pptPersonal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.ppt
ganjangbegu579
 
Environment .................................
Environment .................................Environment .................................
Environment .................................
shadyozq9
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
acid base ppt and their specific application in food
acid base ppt and their specific application in foodacid base ppt and their specific application in food
acid base ppt and their specific application in food
Fatehatun Noor
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 

Functions & Recursion

  • 1. Unit IV Function & Recursion
  • 2. Functions  A function is a self-contained block of statements that perform a coherent task of some kind. If you have a task that is always performed in the same way than instead of putting the same functionality at every point we should make function and can call by just declaring function name. #include<stdio.h> void func1(); int main() { func1(); printf("nthis is first program with funtion"); return 0; } void func1() { printf("nthis is func1"); } Output: This is func1 This is the first program with function
  • 3. Important points about function  A function gets called when the function name is followed by a semincolon.  A function is defined when function name is followed by a pair of braces in which one or more statements may be present.  Any function can be called from any other function. Even main can be called from other function.  A function calls itself such process known as recursion.
  • 4. Elements of Functions  Function Definition  Function Call  Function Declaration
  • 5. Definition of Function int func1() { int a; printf("nthis is func1"); return 0; } Function Definition- 1. Function name 2. Function type 3. List of parameters 4. Local variable declaration 5. Function statement 6. A return statement
  • 6. Return values and their type A function may or may not send back any value to the calling function. If it does it is done through return statement. Called function can only return only one value per call. return; or return(expression) return: will not return any value but will return the control back to the calling function. If(error) { return(0); } Return expression: will return some value of the function back to the called function. return(p); return(x*y);
  • 7. … So basically, return serves two purposes:  On executing the return statement, it immediately transfers the control back to the calling function.  It returns the value present in the parentheses after return, to the calling function.
  • 8. Passing values between functions-  Passing the vales to the function allows us to communicate with the function. Using this we can pass our values in order to get the results, for that we need to specify the function name with arguments.  Example: void main() { Funtion1(); Function2(a); } Function1() { } Function2(int a) { }
  • 9. Categories of Function-  Functions with no arguments and no return value.  Functions with arguments and no return value.  Functions with no arguments and return a value.  Functions that return multiple return values.
  • 10. Swapping a Number – General #include <stdio.h> int main() { int x, y, t; printf("Enter two integersn"); scanf("%d%d", &x, &y); printf("Before SwappingnFirst integer = %dnSecond integer = %dn", x, y); t = x; x = y; y = t; printf("After SwappingnFirst integer = %dnSecond integer = %dn", x, y); return 0; }
  • 11. #include <stdio.h> int main() { int x, y, t; printf("Enter two integersn"); scanf("%d%d", &x, &y); printf("Before SwappingnFirst integer = %dnSecond integer = %dn", x, y); swap(x,y); printf("After SwappingnFirst integer = %dnSecond integer = %dn", x, y); return 0; } void swap(int x, int y) //Swap function definition { int t; t = x; x = y; y = t; printf("After SwappingnFirst integer = %dnSecond integer = %dn", x, y); }
  • 12. Swapping a number #include <stdio.h> void swap(int*, int*); //Swap function declaration int main() { int x, y; printf("Enter the value of x and yn"); scanf("%d%d",&x,&y); printf("Before Swappingnx = %dny = %dn", x, y); swap(&x, &y); printf("After Swappingnx = %dny = %dn", x, y); return 0; } void swap(int *a, int *b) //Swap function definition { int t; t = *b; *b = *a; *a = t; }
  • 13. Recursion  Recursion is a special case where function calls itself again and again. main() { printf(“this is the example of recursion”); main(); }
  • 14. … Another example is factorial: Factorial of n=n(n-1)(n-2)…….1 Factorial of 4=4*3*2*1 =24 factorial(int n) { int fact; if(n==1) return 1; else fact=n*factorial(n-1); return(fact); } fact=3*factorial(2) fact=3*2*factorial(1) fact=3*2*1 fact=6
  • 15. Tower of Hanoi  Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules: 1. Only one disk can be moved at a time. 2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack. 3. No disk may be placed on top of a smaller disk.
  • 16.
  • 17. … Desired Input/output  Input : 2 Output : Disk 1 moved from A to B Disk 2 moved from A to C Disk 1 moved from B to C  Input : 3 Output : Disk 1 moved from A to C Disk 2 moved from A to B Disk 1 moved from C to B Disk 3 moved from A to C Disk 1 moved from B to A Disk 2 moved from B to C Disk 1 moved from A to C
  • 18. #include <stdio.h> void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) { if (n==1) { printf("nMove disk 1 from rod : %d",from_rod); printf("nMove disk 1 to rod : %d",to_rod); return; } towerOfHanoi(n - 1, from_rod, aux_rod, to_rod); printf("nMove disk %d from rod : %d",n,from_rod); printf("nMove disk %d to rod : %d",n,to_rod); towerOfHanoi(n - 1, aux_rod, to_rod, from_rod); } int main() { int n = 3; // Number of disks towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods return 0; }
  翻译: