SlideShare une entreprise Scribd logo
1  sur  21
Télécharger pour lire hors ligne
UUSSEERR DDEEFFIINNEE 
FFUUNNCCTTIIOONNSS
• Definition - function 
• 
●A set of statements working together with 
• common goal is known as function. 
• 
Also known as subprograms which are used to 
●• compute a value or perform a specific task. 
• 
They can’t run independently and are always 
●• called by the main() program or by some other 
• function. 
2
Functions 
Function: 
The strength of C language is to define and use function. 
The strength of C language is that C function are easy to define and 
use. 
Function 
Library User define 
function function
Library function: 
The library function are not required to be written 
by us. 
Eg: 
printf 
scanf
User define function: 
User defined functions are self-contained blocks 
of statements which are written by the user to 
compute or perform a task. 
●They can be called by the main program 
repeatedly as per the requirement. 
Eg: main() 
‘main’ is specially used function in C. Ever program must have main 
function to indicate, where the program begins its execution. When a 
program to large and complex then the result of debugging testing 
and maintaining becomes difficult.
Main program 
Function A function B functionC 
B1 B2
UUssiinngg FFuunnccttiioonnss 
• The main functions and other library functions does 
need to be declared and defined but the main 
function’s body need to be defined by the 
programmer.
TThhee 33 ccoommppoonneennttss aassssoocciiaatteedd 
wwiitthh ffuunnccttiioonnss aarree:: 
1. The Declaration 
2. The function definition 
3. The Calling Statement
FFuunnccttiioonn DDeeccllaarraattiioonn 
• In C user- written functions should normally be 
declared prior to its use to allow compiler to 
perform type checking on arguments used in its call 
statement. 
• The general form is: 
• Retirn_data_type function_name (data_type 
Var_name, …..);
 Function name: this is the name given to the 
function. It follows the same naming convention as 
that of any valid variable in C. 
 Return data type: this specifies the type of data 
given back to the calling construct. 
Data type list: this list specifies the data type of 
each variables, the values of which are expected 
to be transmitted to the function. These variables 
are known as formal parameters.
NNoottee:: 
• It is possible not to declare functions prior to the use 
but the error checking will not be performed. 
• ; is required at the end of function declaration. 
• E.g. int FindMax(int x, int y);
FFuunnccttiioonn DDeeffiinniittiioonn 
 The collection of program statements that does a 
specific tasks done by the function is called the 
function definition. 
 It conist of function header: 
◦ Int FindMax(int x, int y) 
◦ { 
◦ } 
 and function body. 
 Int FindMax(int x, int y) 
 { 
 //body of the function…. 
 }
FFLLOOWW OOFF FFUUNNCCTTIIOONN 
• ●When the program is executed (that is, run) execution always begins at 
• the first statement in the function main no matter where it is placed in the 
• program. 
• ●Other functions are executed only when they are called. 
• ●Function prototypes appear before any function definition, so the 
• compiler translates these first. The compiler can then correctly translate a 
• function call. 
• ●A function call statement results in the transfer of control to the first 
• statement in the body of the called function. 
• ●After the last statement of the called function is executed, the control is 
• passed back to the point immediately following the function call. 
• ●A value-returning function returns a value. Therefore, for value-returning 
• functions, after executing the function when the control goes back to the 
• caller, the value that the function returns replaces the function call 
• statement.
FFuunnccttiioonn ccaallll 
• The function is called from the main() 
• The function can in turn call a another function. 
• the function call statements invokes the function, which 
means the program control passes to that function. Once the 
function completes its task, the program control is passed 
back to the calling environment. 
• The general form of calling stmt is: 
• Function_name (var1, var2,..); 
• Or 
• var_name=function name(var1, var2,..);
SSaalliieenntt ppooiinnttss ttoo bbee ttaakkeenn 
iinnttoo ccoonnssiiddeerraattiioonn 
• The func name and the type and number of 
arguments must match with that of the function 
declaration stmt and the header of the function 
definition. 
• Arguments present in the form of expression are 
evaluated and converted to the type of formal 
parameters at the beginning of the body of the 
function.
PPaassssiinngg ooff aarrgguummeennttss ttoo 
tthhee ffuunnccttiioonn 
1. Call by value or pass by value: 
1. When arguments are passed by values this means that local copies of 
the values of the arguments are passed to the function. 
2. Call by reference or pass by reference. 
1. The address of the variable is passed as value of parameters to the 
function.
Passing arrays to 
functions 
• Arrays can also be the arguments of function 
• Only the base address of the array is passed to the 
function 
• Hence the passing of arguments is done by 
reference. 
• When arrays is passed as arguments then actual 
contents of the arrays is altered.
RReeccuurrssiivvee FFuunnccttiioonnss.. 
• Recursion in programming is a technique for 
defining a problem in terms of one or more smaller 
versions of the problem. 
• A recursive function is one that calls itself directly or 
indirectly to solve a smaller version of its task until a 
final call which does not require a self call.
TThhee mmeecchhaanniiccss ooff 
rreeccuurrssiivvee ccaallll 
• Start main program 
• ….. 
• 1st call to print backward 
• enter a char : H 
o 2nd call to print_backward 
• enter a char: i. 
• 3rd call to print_backward 
• enter a char: . 
• //now it will not call againcoz its ‘.’ 
• 3rd call finish 
• print i. 
• 2nd call finish 
• Print H. 
First call Finish… 
… 
End of main program………
HHooww rreeccuurrssiioonn iiss 
iimmpplleemmeenntteedd.. 
• The storage mechanism in most modern languages 
is stack storage mgmt. 
• In this mechanism the program’s data area is 
allocated at load time. 
• While storage for functions data is allocated when 
function is invoked. 
• On exit from function this storage is de-allocated. 
• This results in a runtime stack. 
• In recursive cases one call does not overlap with 
the other.
WWhhaatt iiss nneeeedd ffoorr 
iimmpplleemmeennttiinngg rreeccuurrssiioonn 
• Decomposition into smaller problems of same size. 
• Recursive call must diminish problem size. 
• Necessity of base case. 
• Base case must be reached.

Contenu connexe

Tendances (20)

Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
User defined functions in C programmig
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmig
 
Function in c
Function in cFunction in c
Function in c
 
Tokens in C++
Tokens in C++Tokens in C++
Tokens in C++
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
C functions
C functionsC functions
C functions
 
Strings
StringsStrings
Strings
 
Data types
Data typesData types
Data types
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Classes,object and methods java
Classes,object and methods javaClasses,object and methods java
Classes,object and methods java
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
Implementation Of String Functions In C
Implementation Of String Functions In CImplementation Of String Functions In C
Implementation Of String Functions In C
 
Strings in C
Strings in CStrings in C
Strings in C
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Procedural programming
Procedural programmingProcedural programming
Procedural programming
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
 
C function
C functionC function
C function
 
Functions in c
Functions in cFunctions in c
Functions in c
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
 

Similaire à user defined function

358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2sumitbardhan
 
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdfManiMala75
 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C ProgrammingAnil Pokhrel
 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxGebruGetachew2
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxsangeeta borde
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxKhurramKhan173
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfTeshaleSiyum
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdfTeshaleSiyum
 
Functions in c language1
Functions in c language1Functions in c language1
Functions in c language1sirikeshava
 
Functions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothiFunctions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothiSowmya Jyothi
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptAmanuelZewdie4
 
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxFUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxSKUP1
 
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxFUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxLECO9
 

Similaire à user defined function (20)

358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2
 
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
 
Unit 7. Functions
Unit 7. FunctionsUnit 7. Functions
Unit 7. Functions
 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptx
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
Unit iii
Unit iiiUnit iii
Unit iii
 
Lecture6
Lecture6Lecture6
Lecture6
 
Functions in c language1
Functions in c language1Functions in c language1
Functions in c language1
 
Functions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothiFunctions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothi
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.ppt
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
 
Basic c++
Basic c++Basic c++
Basic c++
 
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxFUNCTIONS IN C.pptx
FUNCTIONS IN C.pptx
 
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxFUNCTIONS IN C.pptx
FUNCTIONS IN C.pptx
 
FUNCTION CPU
FUNCTION CPUFUNCTION CPU
FUNCTION CPU
 

Dernier

presentation by faizan[1] [Read-Only].pptx
presentation by faizan[1] [Read-Only].pptxpresentation by faizan[1] [Read-Only].pptx
presentation by faizan[1] [Read-Only].pptxkhfaizan534
 
Artificial organ courses Hussein L1-C2.pptx
Artificial organ courses Hussein  L1-C2.pptxArtificial organ courses Hussein  L1-C2.pptx
Artificial organ courses Hussein L1-C2.pptxHusseinMishbak
 
electricity generation from food waste - based bioenergy with IOT.pptx
electricity generation from food waste - based bioenergy with IOT.pptxelectricity generation from food waste - based bioenergy with IOT.pptx
electricity generation from food waste - based bioenergy with IOT.pptxAravindhKarthik1
 
autonomous_vehicle_working_paper_01072020-_508_compliant.pdf
autonomous_vehicle_working_paper_01072020-_508_compliant.pdfautonomous_vehicle_working_paper_01072020-_508_compliant.pdf
autonomous_vehicle_working_paper_01072020-_508_compliant.pdfPandurangGurakhe
 
Conventional vs Modern method (Philosophies) of Tunneling-re.pptx
Conventional vs Modern method (Philosophies) of Tunneling-re.pptxConventional vs Modern method (Philosophies) of Tunneling-re.pptx
Conventional vs Modern method (Philosophies) of Tunneling-re.pptxSAQIB KHURSHEED WANI
 
This chapter gives an outline of the security.
This chapter gives an outline of the security.This chapter gives an outline of the security.
This chapter gives an outline of the security.RoshniIsrani1
 
Evaluation Methods for Social XR Experiences
Evaluation Methods for Social XR ExperiencesEvaluation Methods for Social XR Experiences
Evaluation Methods for Social XR ExperiencesMark Billinghurst
 
Field Report on present condition of Ward 1 and Ward 2 of Pabna Municipality
Field Report on present condition of Ward 1 and Ward 2 of Pabna MunicipalityField Report on present condition of Ward 1 and Ward 2 of Pabna Municipality
Field Report on present condition of Ward 1 and Ward 2 of Pabna MunicipalityMorshed Ahmed Rahath
 
Introduction to Data Structures .
Introduction to Data Structures        .Introduction to Data Structures        .
Introduction to Data Structures .Ashutosh Satapathy
 
Research paper publications: Meaning of Q1 Q2 Q3 Q4 Journal
Research paper publications: Meaning of Q1 Q2 Q3 Q4 JournalResearch paper publications: Meaning of Q1 Q2 Q3 Q4 Journal
Research paper publications: Meaning of Q1 Q2 Q3 Q4 JournalDr. Manjunatha. P
 
12. Stairs by U Nyi Hla ngae from Myanmar.pdf
12. Stairs by U Nyi Hla ngae from Myanmar.pdf12. Stairs by U Nyi Hla ngae from Myanmar.pdf
12. Stairs by U Nyi Hla ngae from Myanmar.pdftpo482247
 
The Journey of Process Safety Management: Past, Present, and Future Trends
The Journey of Process Safety Management: Past, Present, and Future TrendsThe Journey of Process Safety Management: Past, Present, and Future Trends
The Journey of Process Safety Management: Past, Present, and Future Trendssoginsider
 
Investigating the Efficiency of Drinking Water Treatment Sludge and Iron-Base...
Investigating the Efficiency of Drinking Water Treatment Sludge and Iron-Base...Investigating the Efficiency of Drinking Water Treatment Sludge and Iron-Base...
Investigating the Efficiency of Drinking Water Treatment Sludge and Iron-Base...J. Agricultural Machinery
 
Wave Energy Technologies Overtopping 1 - Tom Thorpe.pdf
Wave Energy Technologies Overtopping 1 - Tom Thorpe.pdfWave Energy Technologies Overtopping 1 - Tom Thorpe.pdf
Wave Energy Technologies Overtopping 1 - Tom Thorpe.pdfErik Friis-Madsen
 
Support nodes for large-span coal storage structures
Support nodes for large-span coal storage structuresSupport nodes for large-span coal storage structures
Support nodes for large-span coal storage structureswendy cai
 
Tekom Netherlands | The evolving landscape of Simplified Technical English b...
Tekom Netherlands | The evolving landscape of Simplified Technical English  b...Tekom Netherlands | The evolving landscape of Simplified Technical English  b...
Tekom Netherlands | The evolving landscape of Simplified Technical English b...Shumin Chen
 
pulse modulation technique (Pulse code modulation).pptx
pulse modulation technique (Pulse code modulation).pptxpulse modulation technique (Pulse code modulation).pptx
pulse modulation technique (Pulse code modulation).pptxNishanth Asmi
 
Final PPT.ppt about human detection and counting
Final PPT.ppt  about human detection and countingFinal PPT.ppt  about human detection and counting
Final PPT.ppt about human detection and countingArbazAhmad25
 
First Review Group 1 PPT.pptx with slide
First Review Group 1 PPT.pptx with slideFirst Review Group 1 PPT.pptx with slide
First Review Group 1 PPT.pptx with slideMonika860882
 

Dernier (20)

Update on the latest research with regard to RAP
Update on the latest research with regard to RAPUpdate on the latest research with regard to RAP
Update on the latest research with regard to RAP
 
presentation by faizan[1] [Read-Only].pptx
presentation by faizan[1] [Read-Only].pptxpresentation by faizan[1] [Read-Only].pptx
presentation by faizan[1] [Read-Only].pptx
 
Artificial organ courses Hussein L1-C2.pptx
Artificial organ courses Hussein  L1-C2.pptxArtificial organ courses Hussein  L1-C2.pptx
Artificial organ courses Hussein L1-C2.pptx
 
electricity generation from food waste - based bioenergy with IOT.pptx
electricity generation from food waste - based bioenergy with IOT.pptxelectricity generation from food waste - based bioenergy with IOT.pptx
electricity generation from food waste - based bioenergy with IOT.pptx
 
autonomous_vehicle_working_paper_01072020-_508_compliant.pdf
autonomous_vehicle_working_paper_01072020-_508_compliant.pdfautonomous_vehicle_working_paper_01072020-_508_compliant.pdf
autonomous_vehicle_working_paper_01072020-_508_compliant.pdf
 
Conventional vs Modern method (Philosophies) of Tunneling-re.pptx
Conventional vs Modern method (Philosophies) of Tunneling-re.pptxConventional vs Modern method (Philosophies) of Tunneling-re.pptx
Conventional vs Modern method (Philosophies) of Tunneling-re.pptx
 
This chapter gives an outline of the security.
This chapter gives an outline of the security.This chapter gives an outline of the security.
This chapter gives an outline of the security.
 
Evaluation Methods for Social XR Experiences
Evaluation Methods for Social XR ExperiencesEvaluation Methods for Social XR Experiences
Evaluation Methods for Social XR Experiences
 
Field Report on present condition of Ward 1 and Ward 2 of Pabna Municipality
Field Report on present condition of Ward 1 and Ward 2 of Pabna MunicipalityField Report on present condition of Ward 1 and Ward 2 of Pabna Municipality
Field Report on present condition of Ward 1 and Ward 2 of Pabna Municipality
 
Introduction to Data Structures .
Introduction to Data Structures        .Introduction to Data Structures        .
Introduction to Data Structures .
 
Research paper publications: Meaning of Q1 Q2 Q3 Q4 Journal
Research paper publications: Meaning of Q1 Q2 Q3 Q4 JournalResearch paper publications: Meaning of Q1 Q2 Q3 Q4 Journal
Research paper publications: Meaning of Q1 Q2 Q3 Q4 Journal
 
12. Stairs by U Nyi Hla ngae from Myanmar.pdf
12. Stairs by U Nyi Hla ngae from Myanmar.pdf12. Stairs by U Nyi Hla ngae from Myanmar.pdf
12. Stairs by U Nyi Hla ngae from Myanmar.pdf
 
The Journey of Process Safety Management: Past, Present, and Future Trends
The Journey of Process Safety Management: Past, Present, and Future TrendsThe Journey of Process Safety Management: Past, Present, and Future Trends
The Journey of Process Safety Management: Past, Present, and Future Trends
 
Investigating the Efficiency of Drinking Water Treatment Sludge and Iron-Base...
Investigating the Efficiency of Drinking Water Treatment Sludge and Iron-Base...Investigating the Efficiency of Drinking Water Treatment Sludge and Iron-Base...
Investigating the Efficiency of Drinking Water Treatment Sludge and Iron-Base...
 
Wave Energy Technologies Overtopping 1 - Tom Thorpe.pdf
Wave Energy Technologies Overtopping 1 - Tom Thorpe.pdfWave Energy Technologies Overtopping 1 - Tom Thorpe.pdf
Wave Energy Technologies Overtopping 1 - Tom Thorpe.pdf
 
Support nodes for large-span coal storage structures
Support nodes for large-span coal storage structuresSupport nodes for large-span coal storage structures
Support nodes for large-span coal storage structures
 
Tekom Netherlands | The evolving landscape of Simplified Technical English b...
Tekom Netherlands | The evolving landscape of Simplified Technical English  b...Tekom Netherlands | The evolving landscape of Simplified Technical English  b...
Tekom Netherlands | The evolving landscape of Simplified Technical English b...
 
pulse modulation technique (Pulse code modulation).pptx
pulse modulation technique (Pulse code modulation).pptxpulse modulation technique (Pulse code modulation).pptx
pulse modulation technique (Pulse code modulation).pptx
 
Final PPT.ppt about human detection and counting
Final PPT.ppt  about human detection and countingFinal PPT.ppt  about human detection and counting
Final PPT.ppt about human detection and counting
 
First Review Group 1 PPT.pptx with slide
First Review Group 1 PPT.pptx with slideFirst Review Group 1 PPT.pptx with slide
First Review Group 1 PPT.pptx with slide
 

user defined function

  • 2. • Definition - function • ●A set of statements working together with • common goal is known as function. • Also known as subprograms which are used to ●• compute a value or perform a specific task. • They can’t run independently and are always ●• called by the main() program or by some other • function. 2
  • 3. Functions Function: The strength of C language is to define and use function. The strength of C language is that C function are easy to define and use. Function Library User define function function
  • 4. Library function: The library function are not required to be written by us. Eg: printf scanf
  • 5. User define function: User defined functions are self-contained blocks of statements which are written by the user to compute or perform a task. ●They can be called by the main program repeatedly as per the requirement. Eg: main() ‘main’ is specially used function in C. Ever program must have main function to indicate, where the program begins its execution. When a program to large and complex then the result of debugging testing and maintaining becomes difficult.
  • 6. Main program Function A function B functionC B1 B2
  • 7. UUssiinngg FFuunnccttiioonnss • The main functions and other library functions does need to be declared and defined but the main function’s body need to be defined by the programmer.
  • 8. TThhee 33 ccoommppoonneennttss aassssoocciiaatteedd wwiitthh ffuunnccttiioonnss aarree:: 1. The Declaration 2. The function definition 3. The Calling Statement
  • 9. FFuunnccttiioonn DDeeccllaarraattiioonn • In C user- written functions should normally be declared prior to its use to allow compiler to perform type checking on arguments used in its call statement. • The general form is: • Retirn_data_type function_name (data_type Var_name, …..);
  • 10.  Function name: this is the name given to the function. It follows the same naming convention as that of any valid variable in C.  Return data type: this specifies the type of data given back to the calling construct. Data type list: this list specifies the data type of each variables, the values of which are expected to be transmitted to the function. These variables are known as formal parameters.
  • 11. NNoottee:: • It is possible not to declare functions prior to the use but the error checking will not be performed. • ; is required at the end of function declaration. • E.g. int FindMax(int x, int y);
  • 12. FFuunnccttiioonn DDeeffiinniittiioonn  The collection of program statements that does a specific tasks done by the function is called the function definition.  It conist of function header: ◦ Int FindMax(int x, int y) ◦ { ◦ }  and function body.  Int FindMax(int x, int y)  {  //body of the function….  }
  • 13. FFLLOOWW OOFF FFUUNNCCTTIIOONN • ●When the program is executed (that is, run) execution always begins at • the first statement in the function main no matter where it is placed in the • program. • ●Other functions are executed only when they are called. • ●Function prototypes appear before any function definition, so the • compiler translates these first. The compiler can then correctly translate a • function call. • ●A function call statement results in the transfer of control to the first • statement in the body of the called function. • ●After the last statement of the called function is executed, the control is • passed back to the point immediately following the function call. • ●A value-returning function returns a value. Therefore, for value-returning • functions, after executing the function when the control goes back to the • caller, the value that the function returns replaces the function call • statement.
  • 14. FFuunnccttiioonn ccaallll • The function is called from the main() • The function can in turn call a another function. • the function call statements invokes the function, which means the program control passes to that function. Once the function completes its task, the program control is passed back to the calling environment. • The general form of calling stmt is: • Function_name (var1, var2,..); • Or • var_name=function name(var1, var2,..);
  • 15. SSaalliieenntt ppooiinnttss ttoo bbee ttaakkeenn iinnttoo ccoonnssiiddeerraattiioonn • The func name and the type and number of arguments must match with that of the function declaration stmt and the header of the function definition. • Arguments present in the form of expression are evaluated and converted to the type of formal parameters at the beginning of the body of the function.
  • 16. PPaassssiinngg ooff aarrgguummeennttss ttoo tthhee ffuunnccttiioonn 1. Call by value or pass by value: 1. When arguments are passed by values this means that local copies of the values of the arguments are passed to the function. 2. Call by reference or pass by reference. 1. The address of the variable is passed as value of parameters to the function.
  • 17. Passing arrays to functions • Arrays can also be the arguments of function • Only the base address of the array is passed to the function • Hence the passing of arguments is done by reference. • When arrays is passed as arguments then actual contents of the arrays is altered.
  • 18. RReeccuurrssiivvee FFuunnccttiioonnss.. • Recursion in programming is a technique for defining a problem in terms of one or more smaller versions of the problem. • A recursive function is one that calls itself directly or indirectly to solve a smaller version of its task until a final call which does not require a self call.
  • 19. TThhee mmeecchhaanniiccss ooff rreeccuurrssiivvee ccaallll • Start main program • ….. • 1st call to print backward • enter a char : H o 2nd call to print_backward • enter a char: i. • 3rd call to print_backward • enter a char: . • //now it will not call againcoz its ‘.’ • 3rd call finish • print i. • 2nd call finish • Print H. First call Finish… … End of main program………
  • 20. HHooww rreeccuurrssiioonn iiss iimmpplleemmeenntteedd.. • The storage mechanism in most modern languages is stack storage mgmt. • In this mechanism the program’s data area is allocated at load time. • While storage for functions data is allocated when function is invoked. • On exit from function this storage is de-allocated. • This results in a runtime stack. • In recursive cases one call does not overlap with the other.
  • 21. WWhhaatt iiss nneeeedd ffoorr iimmpplleemmeennttiinngg rreeccuurrssiioonn • Decomposition into smaller problems of same size. • Recursive call must diminish problem size. • Necessity of base case. • Base case must be reached.