SlideShare a Scribd company logo
2
Most read
13
Most read
14
Most read
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.

More Related Content

PPT
RECURSION IN C
PPTX
Functions in C
PPSX
Function in c
PPTX
Recursive Function
PPTX
Function C programming
PPTX
User defined functions in C
PPT
User defined functions in C programmig
PPTX
Function in c
RECURSION IN C
Functions in C
Function in c
Recursive Function
Function C programming
User defined functions in C
User defined functions in C programmig
Function in c

What's hot (20)

PPTX
Type casting in c programming
PPTX
Dynamic memory allocation in c
PPT
Input and output in C++
PPTX
PPTX
07. Virtual Functions
PPT
friend function(c++)
PPTX
Storage class in C Language
PPTX
classes and objects in C++
PDF
Modular Programming in C
PPTX
Programming in C Presentation upto FILE
PPTX
Functions in C
PPTX
Methods in java
PPT
Function overloading(c++)
PDF
Managing I/O in c++
PPTX
Python Functions
PPTX
Storage classes in C
PPTX
Java exception handling
PPTX
User defined functions
PPTX
Functions in c++
PDF
Exception handling
Type casting in c programming
Dynamic memory allocation in c
Input and output in C++
07. Virtual Functions
friend function(c++)
Storage class in C Language
classes and objects in C++
Modular Programming in C
Programming in C Presentation upto FILE
Functions in C
Methods in java
Function overloading(c++)
Managing I/O in c++
Python Functions
Storage classes in C
Java exception handling
User defined functions
Functions in c++
Exception handling
Ad

Similar to user defined function (20)

PPTX
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
PDF
Chapter 1. Functions in C++.pdf
PDF
Chapter_1.__Functions_in_C++[1].pdf
PDF
Functions in c mrs.sowmya jyothi
PDF
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
PDF
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
PDF
Unit 3 (1)
DOC
4. function
PPTX
Unit-III.pptx
PPTX
FUNCTIONS IN C.pptx
PPTX
FUNCTIONS IN C.pptx
PDF
VIT351 Software Development VI Unit1
DOCX
Introduction to c programming
PPTX
user-definedfunctions-converted.pptx
ODP
Function
PPTX
unit_2.pptx
PPT
arrays.ppt
PPTX
FUNCTION.pptxfkrdutytrtttrrtttttttttttttt
PPTX
FUNCTIONengineeringtechnologyslidesh.pptx
PPTX
unit_2 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Chapter 1. Functions in C++.pdf
Chapter_1.__Functions_in_C++[1].pdf
Functions in c mrs.sowmya jyothi
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
Unit 3 (1)
4. function
Unit-III.pptx
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptx
VIT351 Software Development VI Unit1
Introduction to c programming
user-definedfunctions-converted.pptx
Function
unit_2.pptx
arrays.ppt
FUNCTION.pptxfkrdutytrtttrrtttttttttttttt
FUNCTIONengineeringtechnologyslidesh.pptx
unit_2 (1).pptx
Ad

Recently uploaded (20)

PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Digital Logic Computer Design lecture notes
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
additive manufacturing of ss316l using mig welding
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
Geodesy 1.pptx...............................................
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PDF
PPT on Performance Review to get promotions
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Well-logging-methods_new................
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPT
Project quality management in manufacturing
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Structs to JSON How Go Powers REST APIs.pdf
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
CH1 Production IntroductoryConcepts.pptx
Digital Logic Computer Design lecture notes
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Operating System & Kernel Study Guide-1 - converted.pdf
additive manufacturing of ss316l using mig welding
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Model Code of Practice - Construction Work - 21102022 .pdf
Internet of Things (IOT) - A guide to understanding
Geodesy 1.pptx...............................................
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Arduino robotics embedded978-1-4302-3184-4.pdf
PPT on Performance Review to get promotions
Foundation to blockchain - A guide to Blockchain Tech
Well-logging-methods_new................
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Project quality management in manufacturing
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Structs to JSON How Go Powers REST APIs.pdf
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx

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.