Function C programming

Appili Vamsi Krishna
Appili Vamsi KrishnaAerospace Engineering Student at Madras Institute of Technology Campus, Anna University, Chennai um Ecell, IIT Madras
Function C programming
 What is C function?
 Uses of C functions
 Types of C functions
◦ Library functions in C
◦ User defined functions in C
 Creating/Adding user defined function in C library
 C function declaration, function call and
definition with example program
 How to call C functions in a program?
◦ Call by value
◦ Call by reference
 C function arguments and return values
◦ C function with arguments and with return value
◦ C function with arguments and without return value
◦ C function without arguments and without return value
◦ C function without arguments and with return value
 A large C program is divided into basic
building blocks called C function.
 C function contains set of instructions
enclosed by “{ }” which performs specific
operation in a C program.
 Actually, Collection of these functions creates
a C program.
◦ C functions are used to avoid rewriting same logic/code
again and again in a program.
◦ There is no limit in calling C functions to make use of
same functionality wherever required.
◦ We can call functions any number of times in a program
and from any place in a program.
◦ A large C program can easily be tracked when it is
divided into functions.
◦ The core concept of C functions are, re-usability,
dividing a big task into small pieces to achieve the
functionality and to improve understandability of very
large C programs.
 C functions can be classified into two
categories
◦ Library functions
◦ User-defined functions
◦ Library functions are not required to be written by
us
◦ printf and scanf belong to the category of library
function
Examples:
Printf(),scanf(),Sqrt(), cos(), strcat(),rand(), etc are
some of library functions
 Consider the following example.
#include <stdio.h>
#include <math.h>
main( )
{
float x,y ;
scanf("%f", &x);
y=sqrt(x);
printf("Square root of %f is %fn", x,y);
}
main() calls 3 built-in functions:
scanf(), sqrt() & printf()
 Every program must have a main function
 It is possible to code any program utilizing only main
function, it leads to a number of problems
 The program may become too large and complex and as a
result the task of debugging, testing, and maintaining
becomes difficult
 If a program is divided into functional parts, then each part
may be independently coded and later combined into a single
unit
 These subprograms called ‘functions’ are much easier to
understand, debug, and test
 There are times when some types of operation or
calculation is repeated at many points throughout a
program
 In such situations, we may repeat the program
statements whenever they are needed
 Another approach is to design a function that can
be called and used whenever required
 This saves both time and space
 Function declaration or prototype - informs compiler about
the function name, function parameters and return value’s
data type.
 Function call – This calls the actual function
 Function definition – This contains all the statements to be
executed.
Sno C Function aspects Syntax
1 Function definition return_type function_name(arguments list)
{ Body of function; }
2 function call function_name ( arguments list );
3 function declaration return_type function_name ( argument list
);
 Functions are classified as one of the derived data
types in C
 Can define functions and use them like any other
variables in C programs.
 Similarities between functions and variables in C
◦ Both function name and variable names are
considered identifiers and therefore they must adhere
to the rules for identifiers.
◦ Like variables, functions have types (such as int)
associated with them
◦ Like variables, function names and their types must
be declared and defined before they are used in a
program
 There are three elements related to functions
◦ Function definition
◦ Function call
◦ Function declaration
 The function definition is an independent program
module that is specially written to implement the
requirements of the function
 To use this function we need to invoke it at a required
place in the program. This is known as the function call.
 The program that calls the function is referred to as the
calling program or calling function.
 The calling program should declare any function that is
to be used later in the program. This is known as the
function declaration or function prototype.

 A function definition, also known as function implementation
shall include the following elements;
◦ Function name;
◦ Function type; Function header
◦ List of parameters;
◦ Local variable declaration;
◦ Function statements; and Function body
◦ A return statement.
 All six elements are grouped into two parts, namely,
◦ Function header (First three elements); and
◦ Function body (Second three elements)
return_type function_name(parameter list)
{
local variable declaration;
executable statement1;
executable statement2;
----------------
----------------
return(expression);
}
 The first line function_type function_name(parameter list) is
known as the function header.
 The statements within the opening and the closing brace
constitute the function body.
 Function Header
◦ The function header consists of three parts: the function type (also
known as return type), the function name and formal parameter list.
◦ Semicolon is not used at the end of the function header
 Name and Type
◦ The function type specifies the type of value (like float or double) that
the function id expected to return to the program calling the function
◦ If the return type is not explicitly specified, C assume it as an integer
type.
◦ If the function is not returning anything then we need to specify the
return type as void
◦ The function name is any valid C identifier and therefore ,just follow the
same rules of formation as other variable names in C
 The parameter list declares the variables that will receive the data sent
by the calling program.
 They serve as input data to the function to carry out the specified task.
 They represent actual input values, they are often referred to as formal
parameters.
 These parameters can also be used to send values to the calling
programs
 The parameter is known as arguments.
◦ float quadratic (int a, int b, int c) { ….. }
◦ double power (double x, int n) { ….. }
◦ int sum (int a, int b) { ….. }
 There is no semicolon after the closing parenthesis
 The declaration parameter variables cannot be combined
 To indicate that the parameter list is empty, we
use the keyword void between the parentheses as
in
void printline (void)
{
…
}
 Many compiler accept an empty set of parentheses
void printline()
 It is good to use void to indicate a nill parameter
list
 The function body contains the declarations and
statements necessary for performing the required
task. The body enclosed in braces, contains three
parts,
◦ Local declarations that specify the variables needed by the
function
◦ Function statements that perform the task of the function
◦ A return statement that returns the value evaluated by the
function
 If a function does not return any value, we can omit
the return statement.
 Its return type should be specified as void
 A function may or may not send back any value to the calling
function
 Done through return statement
 It is possible to send any number of values to the called function
 The called function can only return one value per call
 SYNTAX:
return;
or
return (expression);
 There are two ways that a C function can be called from a
program. They are,
◦ Call by value
◦ Call by reference
1. Call by value:
◦ In call by value method, the value of the variable is passed to the
function as parameter.
◦ The value of the actual parameter can not be modified by formal
parameter.
◦ Different Memory is allocated for both actual and formal
parameters. Because, value of actual parameter is copied to
formal parameter.
 Note:
◦ Actual parameter – This is the argument which is used in
function call.
◦ Formal parameter – This is the argument which is used in
function definition
 #include<stdio.h>
void swap(int a, int b); // function prototype, also called function declaration
int main()
{
int m = 22, n = 44;
printf(" values before swap m = %d nand n = %d", m, n);
swap(m, n); // calling swap function by value
}
void swap(int a, int b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
printf(" nvalues after swap m = %dn and n = %d", a, b);
}
Output
values before swap m = 22 and n =44
values after swap m = 44 and n = 22
 In call by reference method, the address of the variable is
passed to the function as parameter.
 The value of the actual parameter can be modified by formal
parameter.
 Same memory is used for both actual and formal parameters
since only address is used by both parameters.
#include<stdio.h>
void swap(int *a, int *b); // function prototype, also called function declaration
int main()
{
int m = 22, n = 44;
// calling swap function by reference
printf("values before swap m = %d n and n = %d",m,n);
swap(&m, &n);
}
void swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
printf("n values after swap a = %d nand b = %d", *a, *b);
}
Output
values before swap m = 22 and n =44
values after swap m = 44 and n = 22
 C function with arguments (parameters) and with return value
 C function with arguments (parameters) and without return value
 C function without arguments (parameters) and without return value
 C function without arguments (parameters) and with return value
Sno C function C function
1 with arguments and with
return values
int function ( int ); // function declaration
function ( a ); // function call
int function( int a ) // function definition
{statements; return a;}
2 with arguments and without
return values
void function ( int ); // function declaration
function( a ); // function call
void function( int a ) // function definition
{statements;}
3 without arguments and without
return values
void function(); // function declaration
function(); // function call
void function() // function definition
{statements;}
4 without arguments and with
return values
int function ( ); // function declaration
function ( ); // function call
int function( ) // function definition
{statements; return a;}
Function C programming
Function C programming
Function C programming
Function C programming
1 von 29

Recomendados

Functions in c language von
Functions in c language Functions in c language
Functions in c language tanmaymodi4
2.6K views30 Folien
Function in c von
Function in cFunction in c
Function in csavitamhaske
1.3K views18 Folien
Functions in c von
Functions in cFunctions in c
Functions in csunila tharagaturi
1.5K views42 Folien
FUNCTIONS IN c++ PPT von
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT03062679929
25.3K views24 Folien
C functions von
C functionsC functions
C functionsUniversity of Potsdam
9.9K views32 Folien
Function in c von
Function in cFunction in c
Function in cRaj Tandukar
6.8K views22 Folien

Más contenido relacionado

Was ist angesagt?

Functions in C++ von
Functions in C++Functions in C++
Functions in C++Mohammed Sikander
2.4K views29 Folien
Functions in C++ von
Functions in C++Functions in C++
Functions in C++Sachin Sharma
13.7K views8 Folien
Function in C von
Function in CFunction in C
Function in CDr. Abhineet Anand
10.2K views31 Folien
Constructors and Destructors von
Constructors and DestructorsConstructors and Destructors
Constructors and DestructorsDr Sukhpal Singh Gill
9.1K views17 Folien
Templates in C++ von
Templates in C++Templates in C++
Templates in C++Tech_MX
18.1K views21 Folien
Pointer to function 1 von
Pointer to function 1Pointer to function 1
Pointer to function 1Abu Bakr Ramadan
1.3K views9 Folien

Was ist angesagt?(20)

Templates in C++ von Tech_MX
Templates in C++Templates in C++
Templates in C++
Tech_MX18.1K views
Conditional Statement in C Language von Shaina Arora
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
Shaina Arora11.9K views
Datatype in c++ unit 3 -topic 2 von MOHIT TOMAR
Datatype in c++ unit 3 -topic 2Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2
MOHIT TOMAR2.4K views
Class and object in C++ von rprajat007
Class and object in C++Class and object in C++
Class and object in C++
rprajat00720.7K views
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf von SowmyaJyothi3
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdfSTRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3500 views
08 c++ Operator Overloading.ppt von Tareq Hasan
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
Tareq Hasan16.7K views

Destacado

Difference between structure and union von
Difference between structure and unionDifference between structure and union
Difference between structure and unionAppili Vamsi Krishna
8.5K views2 Folien
C programming for Computing Techniques von
C programming for Computing TechniquesC programming for Computing Techniques
C programming for Computing TechniquesAppili Vamsi Krishna
906 views260 Folien
Pointers C programming von
Pointers  C programmingPointers  C programming
Pointers C programmingAppili Vamsi Krishna
13K views36 Folien
C language for Semester Exams for Engineers von
C language for Semester Exams for Engineers C language for Semester Exams for Engineers
C language for Semester Exams for Engineers Appili Vamsi Krishna
2.1K views71 Folien
Storage classess of C progamming von
Storage classess of C progamming Storage classess of C progamming
Storage classess of C progamming Appili Vamsi Krishna
242 views2 Folien
BE Aerospace Syllabus of MIT, Anna University R2015 von
BE Aerospace Syllabus of MIT, Anna University R2015BE Aerospace Syllabus of MIT, Anna University R2015
BE Aerospace Syllabus of MIT, Anna University R2015Appili Vamsi Krishna
960 views46 Folien

Destacado(13)

Planers machine von Bilalwahla
Planers machinePlaners machine
Planers machine
Bilalwahla23.2K views
Introduction to FPGA, VHDL von Amr Rashed
Introduction to FPGA, VHDL  Introduction to FPGA, VHDL
Introduction to FPGA, VHDL
Amr Rashed11.4K views
Conventional machining vs. non conventional machining von onlinemetallurgy.com
Conventional machining vs. non conventional machiningConventional machining vs. non conventional machining
Conventional machining vs. non conventional machining
onlinemetallurgy.com110.6K views

Similar a Function C programming

CH.4FUNCTIONS IN C_FYBSC(CS).pptx von
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxSangeetaBorde3
29 views31 Folien
PSPC-UNIT-4.pdf von
PSPC-UNIT-4.pdfPSPC-UNIT-4.pdf
PSPC-UNIT-4.pdfArshiniGubbala3
2 views18 Folien
arrays.ppt von
arrays.pptarrays.ppt
arrays.pptBharath904863
2 views42 Folien
User defined function in C.pptx von
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptxRhishav Poudyal
2 views57 Folien
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx von
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).pptxvekariyakashyap
12 views27 Folien
Functions in c language von
Functions in c languageFunctions in c language
Functions in c languageTanmay Modi
602 views30 Folien

Similar a Function C programming(20)

CH.4FUNCTIONS IN C_FYBSC(CS).pptx von SangeetaBorde3
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
SangeetaBorde329 views
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx von vekariyakashyap
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
vekariyakashyap12 views
Functions in c language von Tanmay Modi
Functions in c languageFunctions in c language
Functions in c language
Tanmay Modi602 views
Chapter 1. Functions in C++.pdf von TeshaleSiyum
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
TeshaleSiyum8 views
Chapter_1.__Functions_in_C++[1].pdf von TeshaleSiyum
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
TeshaleSiyum1 view
User Defined Functions in C von RAJ KUMAR
User Defined Functions in CUser Defined Functions in C
User Defined Functions in C
RAJ KUMAR78 views

Último

EIT-Digital_Spohrer_AI_Intro 20231128 v1.pptx von
EIT-Digital_Spohrer_AI_Intro 20231128 v1.pptxEIT-Digital_Spohrer_AI_Intro 20231128 v1.pptx
EIT-Digital_Spohrer_AI_Intro 20231128 v1.pptxISSIP
256 views50 Folien
Material del tarjetero LEES Travesías.docx von
Material del tarjetero LEES Travesías.docxMaterial del tarjetero LEES Travesías.docx
Material del tarjetero LEES Travesías.docxNorberto Millán Muñoz
68 views9 Folien
AI Tools for Business and Startups von
AI Tools for Business and StartupsAI Tools for Business and Startups
AI Tools for Business and StartupsSvetlin Nakov
89 views39 Folien
ICS3211_lecture 08_2023.pdf von
ICS3211_lecture 08_2023.pdfICS3211_lecture 08_2023.pdf
ICS3211_lecture 08_2023.pdfVanessa Camilleri
95 views30 Folien
DU Oral Examination Toni Santamaria von
DU Oral Examination Toni SantamariaDU Oral Examination Toni Santamaria
DU Oral Examination Toni SantamariaMIPLM
138 views2 Folien
Universe revised.pdf von
Universe revised.pdfUniverse revised.pdf
Universe revised.pdfDrHafizKosar
108 views26 Folien

Último(20)

EIT-Digital_Spohrer_AI_Intro 20231128 v1.pptx von ISSIP
EIT-Digital_Spohrer_AI_Intro 20231128 v1.pptxEIT-Digital_Spohrer_AI_Intro 20231128 v1.pptx
EIT-Digital_Spohrer_AI_Intro 20231128 v1.pptx
ISSIP256 views
AI Tools for Business and Startups von Svetlin Nakov
AI Tools for Business and StartupsAI Tools for Business and Startups
AI Tools for Business and Startups
Svetlin Nakov89 views
DU Oral Examination Toni Santamaria von MIPLM
DU Oral Examination Toni SantamariaDU Oral Examination Toni Santamaria
DU Oral Examination Toni Santamaria
MIPLM138 views
Nico Baumbach IMR Media Component von InMediaRes1
Nico Baumbach IMR Media ComponentNico Baumbach IMR Media Component
Nico Baumbach IMR Media Component
InMediaRes1425 views
11.28.23 Social Capital and Social Exclusion.pptx von mary850239
11.28.23 Social Capital and Social Exclusion.pptx11.28.23 Social Capital and Social Exclusion.pptx
11.28.23 Social Capital and Social Exclusion.pptx
mary850239112 views
Plastic waste.pdf von alqaseedae
Plastic waste.pdfPlastic waste.pdf
Plastic waste.pdf
alqaseedae110 views
Scope of Biochemistry.pptx von shoba shoba
Scope of Biochemistry.pptxScope of Biochemistry.pptx
Scope of Biochemistry.pptx
shoba shoba121 views
Community-led Open Access Publishing webinar.pptx von Jisc
Community-led Open Access Publishing webinar.pptxCommunity-led Open Access Publishing webinar.pptx
Community-led Open Access Publishing webinar.pptx
Jisc69 views
UWP OA Week Presentation (1).pptx von Jisc
UWP OA Week Presentation (1).pptxUWP OA Week Presentation (1).pptx
UWP OA Week Presentation (1).pptx
Jisc68 views
Education and Diversity.pptx von DrHafizKosar
Education and Diversity.pptxEducation and Diversity.pptx
Education and Diversity.pptx
DrHafizKosar107 views
OEB 2023 Co-learning To Speed Up AI Implementation in Courses.pptx von Inge de Waard
OEB 2023 Co-learning To Speed Up AI Implementation in Courses.pptxOEB 2023 Co-learning To Speed Up AI Implementation in Courses.pptx
OEB 2023 Co-learning To Speed Up AI Implementation in Courses.pptx
Inge de Waard165 views
Compare the flora and fauna of Kerala and Chhattisgarh ( Charttabulation) von AnshulDewangan3
 Compare the flora and fauna of Kerala and Chhattisgarh ( Charttabulation) Compare the flora and fauna of Kerala and Chhattisgarh ( Charttabulation)
Compare the flora and fauna of Kerala and Chhattisgarh ( Charttabulation)
AnshulDewangan3275 views
Narration ppt.pptx von TARIQ KHAN
Narration  ppt.pptxNarration  ppt.pptx
Narration ppt.pptx
TARIQ KHAN110 views
Class 10 English notes 23-24.pptx von TARIQ KHAN
Class 10 English notes 23-24.pptxClass 10 English notes 23-24.pptx
Class 10 English notes 23-24.pptx
TARIQ KHAN95 views

Function C programming

  • 2.  What is C function?  Uses of C functions  Types of C functions ◦ Library functions in C ◦ User defined functions in C  Creating/Adding user defined function in C library  C function declaration, function call and definition with example program  How to call C functions in a program? ◦ Call by value ◦ Call by reference  C function arguments and return values ◦ C function with arguments and with return value ◦ C function with arguments and without return value ◦ C function without arguments and without return value ◦ C function without arguments and with return value
  • 3.  A large C program is divided into basic building blocks called C function.  C function contains set of instructions enclosed by “{ }” which performs specific operation in a C program.  Actually, Collection of these functions creates a C program.
  • 4. ◦ C functions are used to avoid rewriting same logic/code again and again in a program. ◦ There is no limit in calling C functions to make use of same functionality wherever required. ◦ We can call functions any number of times in a program and from any place in a program. ◦ A large C program can easily be tracked when it is divided into functions. ◦ The core concept of C functions are, re-usability, dividing a big task into small pieces to achieve the functionality and to improve understandability of very large C programs.
  • 5.  C functions can be classified into two categories ◦ Library functions ◦ User-defined functions
  • 6. ◦ Library functions are not required to be written by us ◦ printf and scanf belong to the category of library function Examples: Printf(),scanf(),Sqrt(), cos(), strcat(),rand(), etc are some of library functions
  • 7.  Consider the following example. #include <stdio.h> #include <math.h> main( ) { float x,y ; scanf("%f", &x); y=sqrt(x); printf("Square root of %f is %fn", x,y); } main() calls 3 built-in functions: scanf(), sqrt() & printf()
  • 8.  Every program must have a main function  It is possible to code any program utilizing only main function, it leads to a number of problems  The program may become too large and complex and as a result the task of debugging, testing, and maintaining becomes difficult  If a program is divided into functional parts, then each part may be independently coded and later combined into a single unit  These subprograms called ‘functions’ are much easier to understand, debug, and test
  • 9.  There are times when some types of operation or calculation is repeated at many points throughout a program  In such situations, we may repeat the program statements whenever they are needed  Another approach is to design a function that can be called and used whenever required  This saves both time and space
  • 10.  Function declaration or prototype - informs compiler about the function name, function parameters and return value’s data type.  Function call – This calls the actual function  Function definition – This contains all the statements to be executed. Sno C Function aspects Syntax 1 Function definition return_type function_name(arguments list) { Body of function; } 2 function call function_name ( arguments list ); 3 function declaration return_type function_name ( argument list );
  • 11.  Functions are classified as one of the derived data types in C  Can define functions and use them like any other variables in C programs.  Similarities between functions and variables in C ◦ Both function name and variable names are considered identifiers and therefore they must adhere to the rules for identifiers. ◦ Like variables, functions have types (such as int) associated with them ◦ Like variables, function names and their types must be declared and defined before they are used in a program
  • 12.  There are three elements related to functions ◦ Function definition ◦ Function call ◦ Function declaration  The function definition is an independent program module that is specially written to implement the requirements of the function  To use this function we need to invoke it at a required place in the program. This is known as the function call.  The program that calls the function is referred to as the calling program or calling function.  The calling program should declare any function that is to be used later in the program. This is known as the function declaration or function prototype.
  • 13.
  • 14.  A function definition, also known as function implementation shall include the following elements; ◦ Function name; ◦ Function type; Function header ◦ List of parameters; ◦ Local variable declaration; ◦ Function statements; and Function body ◦ A return statement.  All six elements are grouped into two parts, namely, ◦ Function header (First three elements); and ◦ Function body (Second three elements)
  • 15. return_type function_name(parameter list) { local variable declaration; executable statement1; executable statement2; ---------------- ---------------- return(expression); }  The first line function_type function_name(parameter list) is known as the function header.  The statements within the opening and the closing brace constitute the function body.
  • 16.  Function Header ◦ The function header consists of three parts: the function type (also known as return type), the function name and formal parameter list. ◦ Semicolon is not used at the end of the function header  Name and Type ◦ The function type specifies the type of value (like float or double) that the function id expected to return to the program calling the function ◦ If the return type is not explicitly specified, C assume it as an integer type. ◦ If the function is not returning anything then we need to specify the return type as void ◦ The function name is any valid C identifier and therefore ,just follow the same rules of formation as other variable names in C
  • 17.  The parameter list declares the variables that will receive the data sent by the calling program.  They serve as input data to the function to carry out the specified task.  They represent actual input values, they are often referred to as formal parameters.  These parameters can also be used to send values to the calling programs  The parameter is known as arguments. ◦ float quadratic (int a, int b, int c) { ….. } ◦ double power (double x, int n) { ….. } ◦ int sum (int a, int b) { ….. }  There is no semicolon after the closing parenthesis  The declaration parameter variables cannot be combined
  • 18.  To indicate that the parameter list is empty, we use the keyword void between the parentheses as in void printline (void) { … }  Many compiler accept an empty set of parentheses void printline()  It is good to use void to indicate a nill parameter list
  • 19.  The function body contains the declarations and statements necessary for performing the required task. The body enclosed in braces, contains three parts, ◦ Local declarations that specify the variables needed by the function ◦ Function statements that perform the task of the function ◦ A return statement that returns the value evaluated by the function  If a function does not return any value, we can omit the return statement.  Its return type should be specified as void
  • 20.  A function may or may not send back any value to the calling function  Done through return statement  It is possible to send any number of values to the called function  The called function can only return one value per call  SYNTAX: return; or return (expression);
  • 21.  There are two ways that a C function can be called from a program. They are, ◦ Call by value ◦ Call by reference 1. Call by value: ◦ In call by value method, the value of the variable is passed to the function as parameter. ◦ The value of the actual parameter can not be modified by formal parameter. ◦ Different Memory is allocated for both actual and formal parameters. Because, value of actual parameter is copied to formal parameter.  Note: ◦ Actual parameter – This is the argument which is used in function call. ◦ Formal parameter – This is the argument which is used in function definition
  • 22.  #include<stdio.h> void swap(int a, int b); // function prototype, also called function declaration int main() { int m = 22, n = 44; printf(" values before swap m = %d nand n = %d", m, n); swap(m, n); // calling swap function by value } void swap(int a, int b) { int tmp; tmp = a; a = b; b = tmp; printf(" nvalues after swap m = %dn and n = %d", a, b); } Output values before swap m = 22 and n =44 values after swap m = 44 and n = 22
  • 23.  In call by reference method, the address of the variable is passed to the function as parameter.  The value of the actual parameter can be modified by formal parameter.  Same memory is used for both actual and formal parameters since only address is used by both parameters.
  • 24. #include<stdio.h> void swap(int *a, int *b); // function prototype, also called function declaration int main() { int m = 22, n = 44; // calling swap function by reference printf("values before swap m = %d n and n = %d",m,n); swap(&m, &n); } void swap(int *a, int *b) { int tmp; tmp = *a; *a = *b; *b = tmp; printf("n values after swap a = %d nand b = %d", *a, *b); } Output values before swap m = 22 and n =44 values after swap m = 44 and n = 22
  • 25.  C function with arguments (parameters) and with return value  C function with arguments (parameters) and without return value  C function without arguments (parameters) and without return value  C function without arguments (parameters) and with return value Sno C function C function 1 with arguments and with return values int function ( int ); // function declaration function ( a ); // function call int function( int a ) // function definition {statements; return a;} 2 with arguments and without return values void function ( int ); // function declaration function( a ); // function call void function( int a ) // function definition {statements;} 3 without arguments and without return values void function(); // function declaration function(); // function call void function() // function definition {statements;} 4 without arguments and with return values int function ( ); // function declaration function ( ); // function call int function( ) // function definition {statements; return a;}