SlideShare une entreprise Scribd logo
1  sur  20
Télécharger pour lire hors ligne
C Programming
Functions
Structured Programming
 Keep the flow of control in a
program as simple as
possible.
 Use top-down design.
• Keep decomposing (also known as
factoring) a problem into
smaller problems until you have
a collection of small problems
that you can easily solve.
Top-Down Design Using Functions
 C programs normally consist
of a collection of user-
defined functions.
• Each function solves one of the
small problems obtained using
top-down design.
• Functions call or invoke other
functions as needed.
Function Definitions,
Prototypes, and Calls
#include <stdio.h>
void prn_message(void); /* fct prototype */
/* tells the compiler that this */
/* function takes no arguments */
int main(void) /* and returns no value. */
{
prn_message(); /* fct invocation */
}
void prn_message(void) /* fct definition */
{
printf(“A message for you: “);
printf(“Have a nice day!n”);
}
Form of a Function Definition
type function_name ( parameter type list )
{
declarations
statements
}
Some Terminology
Header: Everything before the first brace.
Body: Everything between the braces.
Type: Type of the value returned by the function.
Parameter List: A list of identifiers that provide information
for use within the body of the function.
Also called formal parameters.
The return Statement
 When a return statement is
executed, program control is
immediately passed back to the
calling environment.
• If an expression follows the keyword
return, the value of the expression
is returned to the calling
environment as well.
return;
return expression;
If There is No return
 Control is passed back to the
calling environment when the
closing brace of the body is
encountered.
• Known as “falling of the end.”
Exit Status and return Verus exit( )
 In main() either
return expr;
or
exit(expr);
will return an integer value to the
operating system.
 In functions other than main(),
the effects of return and exit are
different.
return expr Versus exit(expr)
 return expr returns the value
of expr to the calling
function.
 exit(expr) always causes the
program to terminate and
returns an exit status to the
operating system. The value
in expr is the exit status.
Demo Program – Using a Function
to Calculate the Minimum of 2 Values
#include <stdio.h>
int min(int a, int b);
int main(void)
{
int j, k, m;
printf(“Input two integers: “);
scanf(“%d%d”, &j, &k);
m = min(j, k);
printf(“nOf the two values %d and %d, “
“the minimum is %d.nn”, j, k, m);
return 0;
}
int min(int a, int b)
{
if (a < b)
return a;
else
return b;
}
Function Prototypes
 A function prototype tells the
compiler:
• The number and type of arguments that
are to be passed to the function.
• The type of the value that is to be
returned by the function.
 General Form of a Function
Prototype
type function_name( parameter type list);
Examples of Function Prototypes
double sqrt(double);
 The parameter list is typically a
comma-separated list of types.
Identifiers are optional.
void f(char c, int i);
is equivalent to
void f(char, int);
The Keyword void
 void is used if:
• A function takes no arguments.
• If no value is returned by the
function.
Function Invocation
 As we have seen, a function is
invoked (or called) by writing
its name and an appropriate
list of arguments within
parentheses.
• The arguments must match in
number and type the parameters
in the parameter list of the
function definition.
Call-by-Value
 In C, all arguments are
passed call-by-value.
• This means that each argument is
evaluated, and its value is used
in place of the corresponding
formal parameter in the called
function.
Demonstration Program for Call-by-Value
#include <stdio.h>
int compute_sum(int n);
int main(void)
{
int n = 3, sum;
printf(“%dn”, n); /* 3 is printed */
sum = compute_sum(n);
printf(“%dn”, n); /* 3 is printed */
printf(“%dn”, sum);
return 0;
}
int compute_sum(int n)
{
int sum = 0;
for (; n > 0; --n) /* in main(), n is unchanged */
sum += n;
printf(“%dn”, n); /* 0 is printed */
return sum;
}
#include <stdio.h>
#include <stdlib.h>
list of function prototypes
int main(void)
{
. . .
}
int max(int a, int b)
{
. . .
}
int min(int a, int b)
{
. . .
}
void prn_random_numbers(int k)
{
. . .
}
Standard Style for Function Definition Order
#include <stdio.h>
#include <stdlib.h>
int max(int a, int b)
{
. . .
}
int min(int a, int b)
{
. . .
}
void prn_random_numbers(int k)
{
. . .
}
int main(void)
{
. . .
}
“Alternate Style for Function Definition Order
We will use the
standard style.
Common Programming Errors
 If f() is a function and v is
a variable, then the function
call f(v) cannot change the
value in the variable v.
• A common error for beginners is
assuming the the value in v can
be changed by a function call
such as f(v).
Style
 Avoid naming functions you write
with the same name as system
functions.
• Example: read, write, print
 Minimize the number of return
statements in a given function.
 Use names for parameters that
clearly identify their purpose.

Contenu connexe

Tendances

functions in C and types
functions in C and typesfunctions in C and types
functions in C and typesmubashir farooq
 
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)VC Infotech
 
Pre defined Functions in C
Pre defined Functions in CPre defined Functions in C
Pre defined Functions in CPrabhu Govind
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C ProgrammingShuvongkor Barman
 
Lecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.pptLecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.ppteShikshak
 
Presentation on function
Presentation on functionPresentation on function
Presentation on functionAbu Zaman
 
Functions in C - Programming
Functions in C - Programming Functions in C - Programming
Functions in C - Programming GaurangVishnoi
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C v_jk
 
predefined and user defined functions
predefined and user defined functionspredefined and user defined functions
predefined and user defined functionsSwapnil Yadav
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functionsAlisha Korpal
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in CHarendra Singh
 

Tendances (20)

Function in c
Function in cFunction in c
Function in c
 
functions in C and types
functions in C and typesfunctions in C and types
functions in C and types
 
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)
 
Pre defined Functions in C
Pre defined Functions in CPre defined Functions in C
Pre defined Functions in C
 
Function in c
Function in cFunction in c
Function in c
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Lecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.pptLecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.ppt
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Function in C Language
Function in C Language Function in C Language
Function in C Language
 
Presentation on function
Presentation on functionPresentation on function
Presentation on function
 
Functions in C - Programming
Functions in C - Programming Functions in C - Programming
Functions in C - Programming
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
predefined and user defined functions
predefined and user defined functionspredefined and user defined functions
predefined and user defined functions
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functions
 
C function
C functionC function
C function
 
C and C++ functions
C and C++ functionsC and C++ functions
C and C++ functions
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 

Similaire à Functions in c

Similaire à Functions in c (20)

Functions
FunctionsFunctions
Functions
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
 
CHAPTER 6
CHAPTER 6CHAPTER 6
CHAPTER 6
 
Functions
FunctionsFunctions
Functions
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptx
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
 
Functions
FunctionsFunctions
Functions
 
eee2-day4-structures engineering college
eee2-day4-structures engineering collegeeee2-day4-structures engineering college
eee2-day4-structures engineering college
 
UNIT-II CP DOC.docx
UNIT-II CP DOC.docxUNIT-II CP DOC.docx
UNIT-II CP DOC.docx
 
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
 
functions
functionsfunctions
functions
 

Dernier

A brief about Jeypore Sub-station Presentation
A brief about Jeypore Sub-station PresentationA brief about Jeypore Sub-station Presentation
A brief about Jeypore Sub-station PresentationJeyporess2021
 
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
 
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
 
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
 
Governors ppt.pdf .
Governors ppt.pdf                              .Governors ppt.pdf                              .
Governors ppt.pdf .happycocoman
 
Flutter GDE session GDSC ZHCET AMU, aligarh
Flutter GDE session GDSC ZHCET AMU, aligarhFlutter GDE session GDSC ZHCET AMU, aligarh
Flutter GDE session GDSC ZHCET AMU, aligarhjamesbond00714
 
The Art of Cloud Native Defense on Kubernetes
The Art of Cloud Native Defense on KubernetesThe Art of Cloud Native Defense on Kubernetes
The Art of Cloud Native Defense on KubernetesJacopo Nardiello
 
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
 
Navigating Process Safety through Automation and Digitalization in the Oil an...
Navigating Process Safety through Automation and Digitalization in the Oil an...Navigating Process Safety through Automation and Digitalization in the Oil an...
Navigating Process Safety through Automation and Digitalization in the Oil an...soginsider
 
Chapter 2 Canal Falls at Mnnit Allahabad .pptx
Chapter 2 Canal Falls at Mnnit Allahabad .pptxChapter 2 Canal Falls at Mnnit Allahabad .pptx
Chapter 2 Canal Falls at Mnnit Allahabad .pptxButcher771
 
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
 
Searching and Sorting Algorithms
Searching and Sorting AlgorithmsSearching and Sorting Algorithms
Searching and Sorting AlgorithmsAshutosh Satapathy
 
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
 
Introduction to Data Structures .
Introduction to Data Structures        .Introduction to Data Structures        .
Introduction to Data Structures .Ashutosh Satapathy
 
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
 
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
 

Dernier (20)

A brief about Jeypore Sub-station Presentation
A brief about Jeypore Sub-station PresentationA brief about Jeypore Sub-station Presentation
A brief about Jeypore Sub-station Presentation
 
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
 
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...
 
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
 
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
 
Governors ppt.pdf .
Governors ppt.pdf                              .Governors ppt.pdf                              .
Governors ppt.pdf .
 
FOREST FIRE USING IoT-A Visual to UG students
FOREST FIRE USING IoT-A Visual to UG studentsFOREST FIRE USING IoT-A Visual to UG students
FOREST FIRE USING IoT-A Visual to UG students
 
Flutter GDE session GDSC ZHCET AMU, aligarh
Flutter GDE session GDSC ZHCET AMU, aligarhFlutter GDE session GDSC ZHCET AMU, aligarh
Flutter GDE session GDSC ZHCET AMU, aligarh
 
The Art of Cloud Native Defense on Kubernetes
The Art of Cloud Native Defense on KubernetesThe Art of Cloud Native Defense on Kubernetes
The Art of Cloud Native Defense on Kubernetes
 
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
 
Navigating Process Safety through Automation and Digitalization in the Oil an...
Navigating Process Safety through Automation and Digitalization in the Oil an...Navigating Process Safety through Automation and Digitalization in the Oil an...
Navigating Process Safety through Automation and Digitalization in the Oil an...
 
Chapter 2 Canal Falls at Mnnit Allahabad .pptx
Chapter 2 Canal Falls at Mnnit Allahabad .pptxChapter 2 Canal Falls at Mnnit Allahabad .pptx
Chapter 2 Canal Falls at Mnnit Allahabad .pptx
 
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
 
Searching and Sorting Algorithms
Searching and Sorting AlgorithmsSearching and Sorting Algorithms
Searching and Sorting Algorithms
 
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
 
Caltrans view on recycling of in-place asphalt pavements
Caltrans view on recycling of in-place asphalt pavementsCaltrans view on recycling of in-place asphalt pavements
Caltrans view on recycling of in-place asphalt pavements
 
Introduction to Data Structures .
Introduction to Data Structures        .Introduction to Data Structures        .
Introduction to Data Structures .
 
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.
 
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
 

Functions in c

  • 2. Structured Programming  Keep the flow of control in a program as simple as possible.  Use top-down design. • Keep decomposing (also known as factoring) a problem into smaller problems until you have a collection of small problems that you can easily solve.
  • 3. Top-Down Design Using Functions  C programs normally consist of a collection of user- defined functions. • Each function solves one of the small problems obtained using top-down design. • Functions call or invoke other functions as needed.
  • 4. Function Definitions, Prototypes, and Calls #include <stdio.h> void prn_message(void); /* fct prototype */ /* tells the compiler that this */ /* function takes no arguments */ int main(void) /* and returns no value. */ { prn_message(); /* fct invocation */ } void prn_message(void) /* fct definition */ { printf(“A message for you: “); printf(“Have a nice day!n”); }
  • 5. Form of a Function Definition type function_name ( parameter type list ) { declarations statements } Some Terminology Header: Everything before the first brace. Body: Everything between the braces. Type: Type of the value returned by the function. Parameter List: A list of identifiers that provide information for use within the body of the function. Also called formal parameters.
  • 6. The return Statement  When a return statement is executed, program control is immediately passed back to the calling environment. • If an expression follows the keyword return, the value of the expression is returned to the calling environment as well. return; return expression;
  • 7. If There is No return  Control is passed back to the calling environment when the closing brace of the body is encountered. • Known as “falling of the end.”
  • 8. Exit Status and return Verus exit( )  In main() either return expr; or exit(expr); will return an integer value to the operating system.  In functions other than main(), the effects of return and exit are different.
  • 9. return expr Versus exit(expr)  return expr returns the value of expr to the calling function.  exit(expr) always causes the program to terminate and returns an exit status to the operating system. The value in expr is the exit status.
  • 10. Demo Program – Using a Function to Calculate the Minimum of 2 Values #include <stdio.h> int min(int a, int b); int main(void) { int j, k, m; printf(“Input two integers: “); scanf(“%d%d”, &j, &k); m = min(j, k); printf(“nOf the two values %d and %d, “ “the minimum is %d.nn”, j, k, m); return 0; } int min(int a, int b) { if (a < b) return a; else return b; }
  • 11. Function Prototypes  A function prototype tells the compiler: • The number and type of arguments that are to be passed to the function. • The type of the value that is to be returned by the function.  General Form of a Function Prototype type function_name( parameter type list);
  • 12. Examples of Function Prototypes double sqrt(double);  The parameter list is typically a comma-separated list of types. Identifiers are optional. void f(char c, int i); is equivalent to void f(char, int);
  • 13. The Keyword void  void is used if: • A function takes no arguments. • If no value is returned by the function.
  • 14. Function Invocation  As we have seen, a function is invoked (or called) by writing its name and an appropriate list of arguments within parentheses. • The arguments must match in number and type the parameters in the parameter list of the function definition.
  • 15. Call-by-Value  In C, all arguments are passed call-by-value. • This means that each argument is evaluated, and its value is used in place of the corresponding formal parameter in the called function.
  • 16. Demonstration Program for Call-by-Value #include <stdio.h> int compute_sum(int n); int main(void) { int n = 3, sum; printf(“%dn”, n); /* 3 is printed */ sum = compute_sum(n); printf(“%dn”, n); /* 3 is printed */ printf(“%dn”, sum); return 0; } int compute_sum(int n) { int sum = 0; for (; n > 0; --n) /* in main(), n is unchanged */ sum += n; printf(“%dn”, n); /* 0 is printed */ return sum; }
  • 17. #include <stdio.h> #include <stdlib.h> list of function prototypes int main(void) { . . . } int max(int a, int b) { . . . } int min(int a, int b) { . . . } void prn_random_numbers(int k) { . . . } Standard Style for Function Definition Order
  • 18. #include <stdio.h> #include <stdlib.h> int max(int a, int b) { . . . } int min(int a, int b) { . . . } void prn_random_numbers(int k) { . . . } int main(void) { . . . } “Alternate Style for Function Definition Order We will use the standard style.
  • 19. Common Programming Errors  If f() is a function and v is a variable, then the function call f(v) cannot change the value in the variable v. • A common error for beginners is assuming the the value in v can be changed by a function call such as f(v).
  • 20. Style  Avoid naming functions you write with the same name as system functions. • Example: read, write, print  Minimize the number of return statements in a given function.  Use names for parameters that clearly identify their purpose.