CA2 Notes Functions For Students 2
CA2 Notes Functions For Students 2
C functions are basic building blocks in a program. All C programs are written using functions to improve
re-usability, understandability and to keep track on them. You can learn below concepts of C functions in
this section in detail.
1. What is C function?
2. Uses of C functions
3. C function declaration, function call and definition with example program
4. How to call C functions in a program?
1. Call by value
2. Call by reference
5. C function arguments and return values
1. C function with arguments and with return value
2. C function with arguments and without return value
3. C function without arguments and without return value
4. C function without arguments and with return value
6. Types of C functions
1. Library functions in C
2. User defined functions in C
1. Creating/Adding user defined function in C library
1. WHAT IS C FUNCTION?
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.
2. USES OF C FUNCTIONS:
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.
3. C FUNCTION DECLARATION, FUNCTION CALL AND FUNCTION DEFINITION:
Function declaration or prototype – This 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.
Syntax of a function
function_name: It can be anything, however it is advised to have a meaningful name for the functions so
that it would be easy to understand the purpose of function just by seeing it’s name.
argument list: Argument list contains variables names along with their data types. These arguments are
kind of inputs for the function. For example – A function which is used to add two integer variables, will be
having two integer argument.
Block of code: Set of C statements, which will be executed whenever a call will be made to the function.
int main( )
{
float m, n ;
printf ( "\nEnter some number for finding square \n");
scanf ( "%f", &m ) ;
// function call
n = square ( m ) ;
printf ( "\nSquare of the given number %f is %f",m,n );
}
1. Call by value
2. 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:
function definition:
int function( int a )
{
statements;
return a;
return values }
function declaration:
void function ( int );
function call: function( a );
function definition:
void function( int a )
{
2. With arguments and without statements;
return values }
function declaration:
void function();
function declaration:
int function ( );
function call: function ( );
function definition:
int function( )
{
statements;
4. Without arguments and with return a;
return values }
NOTE:
If the return data type of a function is “void”, then, it can’t return any values to the calling function.
If the return data type of the function is other than void such as “int, float, double etc”, then, it can
return values to the calling function.