RECURSION IN C

V
FUNCTIONS IN C FUNCTIONS IN C FUNCTIONS IN C FUNCTIONS IN C FUNCTIONS IN C
FUNCTIONS ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Library Functions User -defined Functions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],FUNCTIONS CLASSIFICATION OF FUNCTION
Problems encountered  when User-defined Functions  are not used ? ,[object Object],[object Object],[object Object],[object Object],[object Object]
Advantage of using  user-defined Functions   ,[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],Advantage of using  user-defined Functions   ,[object Object]
Method of Declaring and Defining a function   Classical Method ANSI (or) Modern Method ,[object Object],[object Object],[object Object],[object Object],Defining Function
FORMAT OF A C FUNCTION Function-name   ( Argument list ) argument declaration; { local variable declaration; executable statement 1; executable statement 2; . . . return  ( expression ); } (Function header ) (Function header ) (Function header ) (Function body )
RULES TO BE FOLLOWED IN NAMING A FUNCTION ,[object Object],[object Object],[object Object]
What is an argument list ? ,[object Object],[object Object],[object Object]
RETURN  STATEMENT A function may or may not send back any value to the calling function. If it sends back any value , it is done through the return statement. The return statement can return only one value to the calling function.   The return statement can take the following form :  Return ;  ( or )  return ( expression )   ;
Return;   This form does not return any value to the calling function. When a return statement is encountered the control is immediately passed back to the calling function. Return ( expression ); This form returns the value of the expression to the calling function. A function may have more than one return statements. This situation arises when the value returned is based on certain conditions.  RETURN STATEMENT
What data type is returned by a function ? ,[object Object],[object Object],[object Object],[object Object]
HANDLING  OF  NON-INTEGER FUNCTIONS In order to enable a calling function to receive a non-integer value from a called function : ,[object Object],Type - specifier   function- name   (  argument list  ) argument declaration  ; { function statements ;  /* body of the function */ }
HANDLING  OF  NON-INTEGER FUNCTIONS 2. The called function must be declared at the start of the body in the calling function, like any other variable. This is to inform the calling function about the type of data that the called function is returning. EXAMPLE
HOW TO CALL A FUNCTION ? A function can be called by simply using the function name in a statement. When the compiler encounters a function call, the control is transferred to the function.  The statements within the body of the function is executed line by line and the value is returned when a return statement is encountered.
A function which returns a value can be used in expression like any other variable. WHERE CAN A FUNCTION CALL APPEAR ? However, a function cannot be used on the left side of an assignment statement.  A function that does not return any value may not be used in expressions, but can be called to perform certain tasks specified in the function.
EXAMPLE OF FUNCTION : ( CLASSIC Method) void main ( ) { int num1, num2 , sum ; sum  =  add  (  num1 , num2 ) ; printf ( “ %d ” , sum ) ; } Function header Body of the function Argument declaration Function call add ( a , b ) int a , b ; { return ( a + b ) ; }
EXAMPLE OF FUNCTION : (  ANSI  Method ) void main ( ) { int num1, num2 , sum ; sum  =  add  (  num1 , num2 )  ; printf ( “ %d ” , sum ) ; } add ( int a , int b ) { return ( a + b ) ; } Function header Body of the function Argument declaration Function call
EXAMPLE OF FUNCTION : (CLASSIC Method) void main ( ) { int num1 ; float num2 , sum ,add(int, float ); sum  =  add  (  num1 , num2 )  ; printf ( “ %f ” , sum ) ; } float add ( a , b ) int a  ; float b ; { return ( a + b ) ; } Function header Body of the function Argument declaration Function call
EXAMPLE OF FUNCTION : ( ANSI Method ) void  main ( ) { int num1 ; float num2 , sum , add ( int , float )  ; sum  =  add  (  num1 , num2 )  ; printf ( “ %f ” , sum ) ; } float add ( int a , float b ) { return ( a + b ) ; } Function header Body of the function Function call
CATEGORY OF FUNCTIONS ,[object Object],[object Object],[object Object]
Function with no argument & no return values void  main ( ) { void printwel( ); printwel ( ) ; } void printwel ( )  { int i ; for ( i = 1; i < = 5 ; i + + ) printf  ( “ n  WELCOME” ) ; } Function header Body of the function Function call Argument declaration
Function with argument & no return values void  main ( ) { int num1, num2  ; void  add  ( int , int )  ; add  (  num1 , num2 )  ; } void  add ( int a , int b ) { int sum ; sum =  a + b  ; printf (“ n SUM OF %d  AND %d IS =  %d”, a, b, sum ); } Function header Body of the function Function call Argument declaration
Function with arguments & return values void  main ( ) { int num1 ; float num2 , sum , add ( ) ; sum  =  add  (  num1 , num2 )  ; printf ( “ %f ” , sum ) ; } float add ( a , b ) int a ; float b ; { return ( a + b ) ; } Function header Body of the function Function call Argument declaration
NESTING OF FUNCTIONS void main ( ) { int m1=10, m2=20, m3=30; int m4=40, m5=50; void sum (int, int, int, int, int ); sum (m1, m2, m3, m4, m5 ); } void sum ( s1, s2, s3, s4, s4) int s1, s2, s3, s4, s5 ; { void avg (float ); float total; total = s1 + s2 + s3 + s4 + s5 ; avg ( total ); } void avg ( tot) float tot; { float average; average = tot / 5 ; printf ( “  The average is %f “, average); }
RECURSION When a called function in turn calls another function a process of chaining occurs. Recursion is a special case of this process, where a function calls itself. Example : void main ( )   { printf ( “  This is an example of recursion”); main ( ) ;  } Recursive function call
factorial (  n  )  int n ; { int fact ; if ( n = = 1 ) return ( 1 ) ; else  fact = n *   factorial ( n - 1 )  ; return ( fact ) ; } void main( ) { int num ; num_fact  = factorial ( num ) ; printf ( “ Factorial is % d”,num); } FACTORIAL OF A GIVEN NUMBER USING RECURSION
void main( ) { int  num  = 5 ; int num_fact  = 0 ;  num_fact   =  factorial ( 5 )  ; printf ( “ Factorial is % d”, num_fact ); } num 5 TO FIND FACTORIAL OF A GIVEN NUMBER  5  num_fact 0 num_fact 1 2 0
factorial ( 5 )  int n ; { int fact ; if ( 5 = = 1 ) return ( 1 ) ; else  fact =  5  *  factorial ( 5 - 1 )   ; return ( fact  )  ; } 120 fact 1 2 0
factorial ( 4 )  int n ; { int fact ; if ( 4 = = 1 ) return ( 1 ) ; else  fact =  4  *     factorial ( 4 - 1 )  ; return ( fact )  ; } 24 fact 2 4
factorial ( 3 )  int n ; { int fact ; if ( 3 = = 1 ) return ( 1 ) ; else  fact = 3 *   factorial ( 3 - 1 )  ; return ( fact ) ; } 6 fact 6
factorial ( 2 )  int n ; { int fact ; if ( 2 = = 1 ) return ( 1 ) ; else  fact = 2  *  factorial ( 2 - 1 )   ; return ( fact )  ; } 2 fact 2
factorial ( 1 )  int n ; { int fact ; if ( 1 = = 1 ) return ( 1 )  ; else  fact = n  *  factorial ( n - 1 )   ; return ( fact ) ; } fact 1
THANK YOU M.O.P. VAISHNAV COLLEGE FOR WOMEN CHENNAI - 34 DATE : 1/10/2001 Presented by A. ANGAYARKANNI Dept. of Computer Science
1 de 35

Recomendados

Functions in C por
Functions in CFunctions in C
Functions in CKamal Acharya
25.2K vistas22 diapositivas
Function in C program por
Function in C programFunction in C program
Function in C programNurul Zakiah Zamri Tan
70.4K vistas23 diapositivas
FUNCTIONS IN c++ PPT por
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT03062679929
25.3K vistas24 diapositivas
C functions por
C functionsC functions
C functionsUniversity of Potsdam
9.9K vistas32 diapositivas
07. Virtual Functions por
07. Virtual Functions07. Virtual Functions
07. Virtual FunctionsHaresh Jaiswal
18.4K vistas20 diapositivas
Recursive Function por
Recursive FunctionRecursive Function
Recursive FunctionHarsh Pathak
6.8K vistas8 diapositivas

Más contenido relacionado

La actualidad más candente

Operators in C Programming por
Operators in C ProgrammingOperators in C Programming
Operators in C Programmingprogramming9
37.6K vistas19 diapositivas
Strings in python por
Strings in pythonStrings in python
Strings in pythonPrabhakaran V M
18.9K vistas29 diapositivas
Data types in C por
Data types in CData types in C
Data types in CTarun Sharma
33.9K vistas12 diapositivas
Functions in c++ por
Functions in c++Functions in c++
Functions in c++Rokonuzzaman Rony
1.3K vistas25 diapositivas
Operator Overloading por
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
27.3K vistas54 diapositivas
Dynamic memory allocation in c por
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in clavanya marichamy
13.2K vistas14 diapositivas

La actualidad más candente(20)

Operators in C Programming por programming9
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
programming937.6K vistas
Data types in C por Tarun Sharma
Data types in CData types in C
Data types in C
Tarun Sharma33.9K vistas
Operator Overloading por Nilesh Dalvi
Operator OverloadingOperator Overloading
Operator Overloading
Nilesh Dalvi27.3K vistas
Presentation on Function in C Programming por Shuvongkor Barman
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman14.4K vistas
While , For , Do-While Loop por Abhishek Choksi
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
Abhishek Choksi14.4K vistas
User defined functions in C por Harendra Singh
User defined functions in CUser defined functions in C
User defined functions in C
Harendra Singh17.1K vistas
Pointers in c++ por Vineeta Garg
Pointers in c++Pointers in c++
Pointers in c++
Vineeta Garg25.2K vistas
Functions in python slide share por Devashish Kumar
Functions in python slide shareFunctions in python slide share
Functions in python slide share
Devashish Kumar21.5K vistas
Programming in c por indra Kishor
Programming in cProgramming in c
Programming in c
indra Kishor10.9K vistas
Introduction to oops concepts por Nilesh Dalvi
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops concepts
Nilesh Dalvi71.8K vistas
Class and object in C++ por rprajat007
Class and object in C++Class and object in C++
Class and object in C++
rprajat00720.7K vistas
Operator in c programming por Manoj Tyagi
Operator in c programmingOperator in c programming
Operator in c programming
Manoj Tyagi34.5K vistas

Similar a RECURSION IN C

Dti2143 chapter 5 por
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5alish sha
736 vistas38 diapositivas
function_v1.ppt por
function_v1.pptfunction_v1.ppt
function_v1.pptssuser823678
12 vistas33 diapositivas
Function in c por
Function in cFunction in c
Function in cRaj Tandukar
6.8K vistas22 diapositivas
Function in c por
Function in cFunction in c
Function in cCGC Technical campus,Mohali
656 vistas22 diapositivas
Function por
Function Function
Function Kathmandu University
101 vistas56 diapositivas
C Programming Language Part 7 por
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7Rumman Ansari
465 vistas23 diapositivas

Similar a RECURSION IN C (20)

Dti2143 chapter 5 por alish sha
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
alish sha736 vistas
C Programming Language Part 7 por Rumman Ansari
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
Rumman Ansari465 vistas
UNIT3.pptx por NagasaiT
UNIT3.pptxUNIT3.pptx
UNIT3.pptx
NagasaiT6 vistas
functionsinc-130108032745-phpapp01.pdf por mounikanarra3
functionsinc-130108032745-phpapp01.pdffunctionsinc-130108032745-phpapp01.pdf
functionsinc-130108032745-phpapp01.pdf
mounikanarra31 vista
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx por vekariyakashyap
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
vekariyakashyap13 vistas
Function in c program por umesh patil
Function in c programFunction in c program
Function in c program
umesh patil1.3K vistas

Último

The Accursed House by Émile Gaboriau por
The Accursed House  by Émile GaboriauThe Accursed House  by Émile Gaboriau
The Accursed House by Émile GaboriauDivyaSheta
187 vistas15 diapositivas
Class 10 English lesson plans por
Class 10 English  lesson plansClass 10 English  lesson plans
Class 10 English lesson plansTARIQ KHAN
280 vistas53 diapositivas
Community-led Open Access Publishing webinar.pptx por
Community-led Open Access Publishing webinar.pptxCommunity-led Open Access Publishing webinar.pptx
Community-led Open Access Publishing webinar.pptxJisc
91 vistas9 diapositivas
Recap of our Class por
Recap of our ClassRecap of our Class
Recap of our ClassCorinne Weisgerber
74 vistas15 diapositivas
Class 10 English notes 23-24.pptx por
Class 10 English notes 23-24.pptxClass 10 English notes 23-24.pptx
Class 10 English notes 23-24.pptxTARIQ KHAN
125 vistas53 diapositivas
Computer Introduction-Lecture06 por
Computer Introduction-Lecture06Computer Introduction-Lecture06
Computer Introduction-Lecture06Dr. Mazin Mohamed alkathiri
82 vistas12 diapositivas

Último(20)

The Accursed House by Émile Gaboriau por DivyaSheta
The Accursed House  by Émile GaboriauThe Accursed House  by Émile Gaboriau
The Accursed House by Émile Gaboriau
DivyaSheta187 vistas
Class 10 English lesson plans por TARIQ KHAN
Class 10 English  lesson plansClass 10 English  lesson plans
Class 10 English lesson plans
TARIQ KHAN280 vistas
Community-led Open Access Publishing webinar.pptx por Jisc
Community-led Open Access Publishing webinar.pptxCommunity-led Open Access Publishing webinar.pptx
Community-led Open Access Publishing webinar.pptx
Jisc91 vistas
Class 10 English notes 23-24.pptx por TARIQ KHAN
Class 10 English notes 23-24.pptxClass 10 English notes 23-24.pptx
Class 10 English notes 23-24.pptx
TARIQ KHAN125 vistas
Solar System and Galaxies.pptx por DrHafizKosar
Solar System and Galaxies.pptxSolar System and Galaxies.pptx
Solar System and Galaxies.pptx
DrHafizKosar89 vistas
The basics - information, data, technology and systems.pdf por JonathanCovena1
The basics - information, data, technology and systems.pdfThe basics - information, data, technology and systems.pdf
The basics - information, data, technology and systems.pdf
JonathanCovena1106 vistas
Dance KS5 Breakdown por WestHatch
Dance KS5 BreakdownDance KS5 Breakdown
Dance KS5 Breakdown
WestHatch69 vistas
OEB 2023 Co-learning To Speed Up AI Implementation in Courses.pptx por Inge de Waard
OEB 2023 Co-learning To Speed Up AI Implementation in Courses.pptxOEB 2023 Co-learning To Speed Up AI Implementation in Courses.pptx
OEB 2023 Co-learning To Speed Up AI Implementation in Courses.pptx
Inge de Waard169 vistas
Psychology KS4 por WestHatch
Psychology KS4Psychology KS4
Psychology KS4
WestHatch76 vistas
Sociology KS5 por WestHatch
Sociology KS5Sociology KS5
Sociology KS5
WestHatch65 vistas
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively por PECB
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks EffectivelyISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively
PECB 574 vistas
Lecture: Open Innovation por Michal Hron
Lecture: Open InnovationLecture: Open Innovation
Lecture: Open Innovation
Michal Hron99 vistas
Drama KS5 Breakdown por WestHatch
Drama KS5 BreakdownDrama KS5 Breakdown
Drama KS5 Breakdown
WestHatch73 vistas

RECURSION IN C

  • 1. FUNCTIONS IN C FUNCTIONS IN C FUNCTIONS IN C FUNCTIONS IN C FUNCTIONS IN C
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8. FORMAT OF A C FUNCTION Function-name ( Argument list ) argument declaration; { local variable declaration; executable statement 1; executable statement 2; . . . return ( expression ); } (Function header ) (Function header ) (Function header ) (Function body )
  • 9.
  • 10.
  • 11. RETURN STATEMENT A function may or may not send back any value to the calling function. If it sends back any value , it is done through the return statement. The return statement can return only one value to the calling function. The return statement can take the following form : Return ; ( or ) return ( expression ) ;
  • 12. Return; This form does not return any value to the calling function. When a return statement is encountered the control is immediately passed back to the calling function. Return ( expression ); This form returns the value of the expression to the calling function. A function may have more than one return statements. This situation arises when the value returned is based on certain conditions. RETURN STATEMENT
  • 13.
  • 14.
  • 15. HANDLING OF NON-INTEGER FUNCTIONS 2. The called function must be declared at the start of the body in the calling function, like any other variable. This is to inform the calling function about the type of data that the called function is returning. EXAMPLE
  • 16. HOW TO CALL A FUNCTION ? A function can be called by simply using the function name in a statement. When the compiler encounters a function call, the control is transferred to the function. The statements within the body of the function is executed line by line and the value is returned when a return statement is encountered.
  • 17. A function which returns a value can be used in expression like any other variable. WHERE CAN A FUNCTION CALL APPEAR ? However, a function cannot be used on the left side of an assignment statement. A function that does not return any value may not be used in expressions, but can be called to perform certain tasks specified in the function.
  • 18. EXAMPLE OF FUNCTION : ( CLASSIC Method) void main ( ) { int num1, num2 , sum ; sum = add ( num1 , num2 ) ; printf ( “ %d ” , sum ) ; } Function header Body of the function Argument declaration Function call add ( a , b ) int a , b ; { return ( a + b ) ; }
  • 19. EXAMPLE OF FUNCTION : ( ANSI Method ) void main ( ) { int num1, num2 , sum ; sum = add ( num1 , num2 ) ; printf ( “ %d ” , sum ) ; } add ( int a , int b ) { return ( a + b ) ; } Function header Body of the function Argument declaration Function call
  • 20. EXAMPLE OF FUNCTION : (CLASSIC Method) void main ( ) { int num1 ; float num2 , sum ,add(int, float ); sum = add ( num1 , num2 ) ; printf ( “ %f ” , sum ) ; } float add ( a , b ) int a ; float b ; { return ( a + b ) ; } Function header Body of the function Argument declaration Function call
  • 21. EXAMPLE OF FUNCTION : ( ANSI Method ) void main ( ) { int num1 ; float num2 , sum , add ( int , float ) ; sum = add ( num1 , num2 ) ; printf ( “ %f ” , sum ) ; } float add ( int a , float b ) { return ( a + b ) ; } Function header Body of the function Function call
  • 22.
  • 23. Function with no argument & no return values void main ( ) { void printwel( ); printwel ( ) ; } void printwel ( ) { int i ; for ( i = 1; i < = 5 ; i + + ) printf ( “ n WELCOME” ) ; } Function header Body of the function Function call Argument declaration
  • 24. Function with argument & no return values void main ( ) { int num1, num2 ; void add ( int , int ) ; add ( num1 , num2 ) ; } void add ( int a , int b ) { int sum ; sum = a + b ; printf (“ n SUM OF %d AND %d IS = %d”, a, b, sum ); } Function header Body of the function Function call Argument declaration
  • 25. Function with arguments & return values void main ( ) { int num1 ; float num2 , sum , add ( ) ; sum = add ( num1 , num2 ) ; printf ( “ %f ” , sum ) ; } float add ( a , b ) int a ; float b ; { return ( a + b ) ; } Function header Body of the function Function call Argument declaration
  • 26. NESTING OF FUNCTIONS void main ( ) { int m1=10, m2=20, m3=30; int m4=40, m5=50; void sum (int, int, int, int, int ); sum (m1, m2, m3, m4, m5 ); } void sum ( s1, s2, s3, s4, s4) int s1, s2, s3, s4, s5 ; { void avg (float ); float total; total = s1 + s2 + s3 + s4 + s5 ; avg ( total ); } void avg ( tot) float tot; { float average; average = tot / 5 ; printf ( “ The average is %f “, average); }
  • 27. RECURSION When a called function in turn calls another function a process of chaining occurs. Recursion is a special case of this process, where a function calls itself. Example : void main ( ) { printf ( “ This is an example of recursion”); main ( ) ; } Recursive function call
  • 28. factorial ( n ) int n ; { int fact ; if ( n = = 1 ) return ( 1 ) ; else fact = n * factorial ( n - 1 ) ; return ( fact ) ; } void main( ) { int num ; num_fact = factorial ( num ) ; printf ( “ Factorial is % d”,num); } FACTORIAL OF A GIVEN NUMBER USING RECURSION
  • 29. void main( ) { int num = 5 ; int num_fact = 0 ; num_fact = factorial ( 5 ) ; printf ( “ Factorial is % d”, num_fact ); } num 5 TO FIND FACTORIAL OF A GIVEN NUMBER 5 num_fact 0 num_fact 1 2 0
  • 30. factorial ( 5 ) int n ; { int fact ; if ( 5 = = 1 ) return ( 1 ) ; else fact = 5 * factorial ( 5 - 1 ) ; return ( fact ) ; } 120 fact 1 2 0
  • 31. factorial ( 4 ) int n ; { int fact ; if ( 4 = = 1 ) return ( 1 ) ; else fact = 4 * factorial ( 4 - 1 ) ; return ( fact ) ; } 24 fact 2 4
  • 32. factorial ( 3 ) int n ; { int fact ; if ( 3 = = 1 ) return ( 1 ) ; else fact = 3 * factorial ( 3 - 1 ) ; return ( fact ) ; } 6 fact 6
  • 33. factorial ( 2 ) int n ; { int fact ; if ( 2 = = 1 ) return ( 1 ) ; else fact = 2 * factorial ( 2 - 1 ) ; return ( fact ) ; } 2 fact 2
  • 34. factorial ( 1 ) int n ; { int fact ; if ( 1 = = 1 ) return ( 1 ) ; else fact = n * factorial ( n - 1 ) ; return ( fact ) ; } fact 1
  • 35. THANK YOU M.O.P. VAISHNAV COLLEGE FOR WOMEN CHENNAI - 34 DATE : 1/10/2001 Presented by A. ANGAYARKANNI Dept. of Computer Science