SlideShare una empresa de Scribd logo
1 de 12
User-defined Functions
Topics
 Function and it’s advantages
 Form of C Function
 Calling function
Function and it’s advantages
 Function: A set of statement(s) to solve
certain kind of problem is called function.
Each function has it’s own name. Ex:
printf(), scanf(), sqrt() etc.
 Advantages of Function in C:
1. The length of source program can be
reduced.
2. It is easy to locate and isolate a faulty
function.
3. A function may be used by many other
function.
Classification of Function
 Functions can be classified into two
categories.
1. Built in function: The function which is
build in the C is called built in function. Ex:
printf(), scanf(), getch().
2. User defined function: The functions
which are designed by programmer called
user defined function.
Form of C Function
 The form of function:
return_type function_name(argument list)
{
local variable declarations;
statement(s);
return(expression);
}
 Here argument list contains valid variable names
separated by commas. Return_type is the type of
the data returned from function.
Continue…
 Example of function:
int add(int a, int b)
{
int sum;
sum= a+b;
return(sum);
}
Calling of Function
 We can call C function by mentioning the
name with appropriate argument.
 Example of calling:
main()
{
int x=10, y=20,z;
z = add(x,y);
printf(“Summation = %d”,z);
}
Call by Value
 In this case of function calling, the value of actual
parameter is passed to the formal parameter. Here
constant value or variable can be used as actual
parameter. If we change the value of formal parameter then
there is no effect in actual parameter.
 void change(int x)
{
x = x + 10;
}
main()
{ int k = 10;
change(k);
printf(“%d”,k);
}
Call by Reference
 Here the address of actual parameter is passed to the
formal parameter. We cannot use address directly. If we
change the value of formal parameter then the value of
actual parameter also changed.
 Void change(int *x)
{
*x = *x+10;
}
main()
{ int k=10;
change(&k);
printf(“%d”,k);
}
Nesting of C Function
 C permits nesting of functions freely. Function1 can call function2,
function2 can call function3,……….and so on.
 main()
{
int a,b,c;
function1();
}
function1()
{
…….
function2();
}
function2()
{
………
}
Recursion
 When a function calls itself is called
recursion. Simple example-
 main()
{
printf(“This is Recursion”);
main();
}
 When executed, the output like
This is Recursion
This is Recursion
This is
Continue..
 A recursive function for factorial:
int factorial(int n)
{
if(n==0)
return(1);
else
return(n*factorial(n-1));
}
Function with Array
 Like the values of simple variables, it is also
possible to pass the values of an array to a
function.
 main()
{
static int value[ ]={10,45,25,40};
printf(“Largest value: %d”,maximum(value,4));
}
int maximum(int x[ ], int n)
{ int i, max=0;
for(i=0;i<n;i++)
if(max<x[i])
max=x[i];
return(max);
}

Más contenido relacionado

La actualidad más candente (20)

lets play with "c"..!!! :):)
lets play with "c"..!!! :):)lets play with "c"..!!! :):)
lets play with "c"..!!! :):)
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functions
 
Function in c
Function in cFunction in c
Function in c
 
C function
C functionC function
C function
 
Function in c program
Function in c programFunction in c program
Function in c program
 
C function presentation
C function presentationC function presentation
C function presentation
 
Function in c language(defination and declaration)
Function in c language(defination and declaration)Function in c language(defination and declaration)
Function in c language(defination and declaration)
 
Function in C Language
Function in C Language Function in C Language
Function in C Language
 
User Defined Functions in MATLAB Part-4
User Defined Functions in MATLAB Part-4User Defined Functions in MATLAB Part-4
User Defined Functions in MATLAB Part-4
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Functions
FunctionsFunctions
Functions
 
functions in C
functions in Cfunctions in C
functions in C
 
Function
FunctionFunction
Function
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Functions
FunctionsFunctions
Functions
 
Function & Recursion in C
Function & Recursion in CFunction & Recursion in C
Function & Recursion in C
 
Functions (Computer programming and utilization)
Functions (Computer programming and utilization)Functions (Computer programming and utilization)
Functions (Computer programming and utilization)
 
Method parameters in c#
Method parameters in c#Method parameters in c#
Method parameters in c#
 
Function in c
Function in cFunction in c
Function in c
 
Function Pointer in C
Function Pointer in CFunction Pointer in C
Function Pointer in C
 

Similar a Chap 9(functions)

Similar a Chap 9(functions) (20)

C and C++ functions
C and C++ functionsC and C++ functions
C and C++ functions
 
USER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdfUSER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdf
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
 
Ch4 functions
Ch4 functionsCh4 functions
Ch4 functions
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdf
 
C functions list
C functions listC functions list
C functions list
 
function in in thi pdf you will learn what is fu...
function in  in thi pdf you will learn   what                           is fu...function in  in thi pdf you will learn   what                           is fu...
function in in thi pdf you will learn what is fu...
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
Computer-programming-User-defined-function-1.pptx
Computer-programming-User-defined-function-1.pptxComputer-programming-User-defined-function-1.pptx
Computer-programming-User-defined-function-1.pptx
 
functions
functionsfunctions
functions
 
1.6 Function.pdf
1.6 Function.pdf1.6 Function.pdf
1.6 Function.pdf
 
FUNCTIONS IN C PROGRAMMING.pdf
FUNCTIONS IN C PROGRAMMING.pdfFUNCTIONS IN C PROGRAMMING.pdf
FUNCTIONS IN C PROGRAMMING.pdf
 
Module 3-Functions
Module 3-FunctionsModule 3-Functions
Module 3-Functions
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 

Más de Bangabandhu Sheikh Mujibur Rahman Science and Technology University

Más de Bangabandhu Sheikh Mujibur Rahman Science and Technology University (20)

Antenna (2)
Antenna (2)Antenna (2)
Antenna (2)
 
Voltage suppler..
Voltage suppler..Voltage suppler..
Voltage suppler..
 
Number system
Number systemNumber system
Number system
 
Chapter 15
Chapter 15Chapter 15
Chapter 15
 
Chap 13(dynamic memory allocation)
Chap 13(dynamic memory allocation)Chap 13(dynamic memory allocation)
Chap 13(dynamic memory allocation)
 
Chap 12(files)
Chap 12(files)Chap 12(files)
Chap 12(files)
 
Chap 11(pointers)
Chap 11(pointers)Chap 11(pointers)
Chap 11(pointers)
 
Chap 10(structure and unions)
Chap 10(structure and unions)Chap 10(structure and unions)
Chap 10(structure and unions)
 
Chap 8(strings)
Chap 8(strings)Chap 8(strings)
Chap 8(strings)
 
Chap 7(array)
Chap 7(array)Chap 7(array)
Chap 7(array)
 
Chap 6(decision making-looping)
Chap 6(decision making-looping)Chap 6(decision making-looping)
Chap 6(decision making-looping)
 
Chap 5(decision making-branching)
Chap 5(decision making-branching)Chap 5(decision making-branching)
Chap 5(decision making-branching)
 
Chap 3(operator expression)
Chap 3(operator expression)Chap 3(operator expression)
Chap 3(operator expression)
 
Chap 2(const var-datatype)
Chap 2(const var-datatype)Chap 2(const var-datatype)
Chap 2(const var-datatype)
 
Computer hardware ppt1
Computer hardware ppt1Computer hardware ppt1
Computer hardware ppt1
 
# Operating system
# Operating system# Operating system
# Operating system
 
Magnetism 3
Magnetism 3Magnetism 3
Magnetism 3
 
Magnetism 2
Magnetism 2Magnetism 2
Magnetism 2
 
2. sinusoidal waves
2. sinusoidal waves2. sinusoidal waves
2. sinusoidal waves
 
Magnetism 1
Magnetism 1Magnetism 1
Magnetism 1
 

Último

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 

Último (20)

Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 

Chap 9(functions)

  • 1. User-defined Functions Topics  Function and it’s advantages  Form of C Function  Calling function
  • 2. Function and it’s advantages  Function: A set of statement(s) to solve certain kind of problem is called function. Each function has it’s own name. Ex: printf(), scanf(), sqrt() etc.  Advantages of Function in C: 1. The length of source program can be reduced. 2. It is easy to locate and isolate a faulty function. 3. A function may be used by many other function.
  • 3. Classification of Function  Functions can be classified into two categories. 1. Built in function: The function which is build in the C is called built in function. Ex: printf(), scanf(), getch(). 2. User defined function: The functions which are designed by programmer called user defined function.
  • 4. Form of C Function  The form of function: return_type function_name(argument list) { local variable declarations; statement(s); return(expression); }  Here argument list contains valid variable names separated by commas. Return_type is the type of the data returned from function.
  • 5. Continue…  Example of function: int add(int a, int b) { int sum; sum= a+b; return(sum); }
  • 6. Calling of Function  We can call C function by mentioning the name with appropriate argument.  Example of calling: main() { int x=10, y=20,z; z = add(x,y); printf(“Summation = %d”,z); }
  • 7. Call by Value  In this case of function calling, the value of actual parameter is passed to the formal parameter. Here constant value or variable can be used as actual parameter. If we change the value of formal parameter then there is no effect in actual parameter.  void change(int x) { x = x + 10; } main() { int k = 10; change(k); printf(“%d”,k); }
  • 8. Call by Reference  Here the address of actual parameter is passed to the formal parameter. We cannot use address directly. If we change the value of formal parameter then the value of actual parameter also changed.  Void change(int *x) { *x = *x+10; } main() { int k=10; change(&k); printf(“%d”,k); }
  • 9. Nesting of C Function  C permits nesting of functions freely. Function1 can call function2, function2 can call function3,……….and so on.  main() { int a,b,c; function1(); } function1() { ……. function2(); } function2() { ……… }
  • 10. Recursion  When a function calls itself is called recursion. Simple example-  main() { printf(“This is Recursion”); main(); }  When executed, the output like This is Recursion This is Recursion This is
  • 11. Continue..  A recursive function for factorial: int factorial(int n) { if(n==0) return(1); else return(n*factorial(n-1)); }
  • 12. Function with Array  Like the values of simple variables, it is also possible to pass the values of an array to a function.  main() { static int value[ ]={10,45,25,40}; printf(“Largest value: %d”,maximum(value,4)); } int maximum(int x[ ], int n) { int i, max=0; for(i=0;i<n;i++) if(max<x[i]) max=x[i]; return(max); }