SlideShare uma empresa Scribd logo
1 de 35
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

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
Loops c++
Loops c++Loops c++
Loops c++
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Recursion
RecursionRecursion
Recursion
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Programming in ansi C by Balaguruswami
Programming in ansi C by BalaguruswamiProgramming in ansi C by Balaguruswami
Programming in ansi C by Balaguruswami
 
Functions in c
Functions in cFunctions in c
Functions in c
 
C if else
C if elseC if else
C if else
 
Conditional operators
Conditional operatorsConditional operators
Conditional operators
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Inline function
Inline functionInline function
Inline function
 
C functions
C functionsC functions
C functions
 
Operators in C++
Operators in C++Operators in C++
Operators in C++
 

Semelhante a RECURSION IN C (20)

Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
Function in c
Function in cFunction in c
Function in c
 
Function in c
Function in cFunction in c
Function in c
 
Function
Function Function
Function
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
 
UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptx
 
functionsinc-130108032745-phpapp01.pdf
functionsinc-130108032745-phpapp01.pdffunctionsinc-130108032745-phpapp01.pdf
functionsinc-130108032745-phpapp01.pdf
 
Functions
Functions Functions
Functions
 
C and C++ functions
C and C++ functionsC and C++ functions
C and C++ functions
 
Function
FunctionFunction
Function
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
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
 
functions
functionsfunctions
functions
 
Function
FunctionFunction
Function
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdf
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 

Último

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 

Último (20)

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 

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