Functions in C :
Overview :
A function is a group of statements that together perform a task. You can divide up your code into
separate functions. The division of code among different functions is up to the user, but logically the
division usually is so each function performs a specific task. It is possible to pass variables to a function
and return value from a function. So function is a part of the program which can be called for a specific
task to be done.
Every C program has at least one function, which is main(), and all the most trivial programs can define
additional functions. A function declaration tells the compiler about a function's name, return type, and
parameters. A function definition provides the actual body of the function. A function is known with
various names like a method or a sub-routine or a procedure, etc. A function can be called from any other
function in the program, including the same function itself.
Description:
General nature of computer programs :
The general nature of a computer program may be specified as follows :



Read or Write to computer memory.
Do operations on the data in the computer memory.
Hand over the data to other parts of the program for more operations.
There are two types of functions in C programming:
1. Library function.
2. User defined function.
6.1 Library functions :
C Standard library functions or simply C Library functions are inbuilt functions in C programming. Function
prototype and data definitions of these functions are written in their respective header file. For example: If to
use the printf() function, the header file <stdio.h> should be included.
There is at least one function in any C program, i.e., the main() function (which is also a library function)
where the program starts. There are many library functions available in C programming to help the
programmer to write a good efficient program.
6.2 User Defined functions :
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
C allows programmer to define their own function according to their requirement. These types of functions
are known as user-defined functions. A function is generally composed of,
1. Function Declarations.
2. Function Definitions.
6.3 Function Declarations:
A function declaration tells the compiler about a function name and how to call the function. The actual body
of the function can be defined separately.
A function declaration has the following parts:
return_type function_name( parameter list );
6.4 Function Definitions :
The general form of a function definition in C programming language is as follows:
returntype Function_Name( Parameter_List )
{
// Body of the function
}
A function definition in C programming language consists of a function header and a function body. Here are
all the parts of a function:

Return Type: A function may return a value. The returntype is the data type of the value the function
returns. Some functions perform the desired operations without returning a value. In this case, the
returntype is the keyword void.

Function Name: This is the actual name of the function. The function name and the parameter list
together constitute the function signature.

Parameters: A parameter is like a placeholder. When a function is invoked, you pass a value to the
parameter. This value is referred to as actual parameter or argument. The parameter list refers to the
type, order, and number of the parameters of a function. Parameters are optional; that is, a function
may contain no parameters.

Function Body: The function body contains a collection of statements that define what the function
does.
Consider the following example :
#include "stdafx.h"
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
int sum ( int a, int b); // Function Declaration
int sum ( int a, int b) // Function Definition
{
return a+b; // Body of the Function.
}
int _tmain(int argc, _TCHAR* argv[])
{
printf("helloworld");
int mya = 10;
int myb = 20;
int mysum = sum ( mya, myb); // Function Call
int anothersum = sum ( 30, 50); // Function Call
return 0;
}
The above program shows the declaration of a user defined function above the main function. An example of
a library function is print() which is defined inside the header file <stdio.h> which takes a string as the input
to be printed on the screen.The function definition for the function sum() is defined above the main function.
The function definition needs to be placed anywhere below the function declaration in the program.
The body of the function returns the sum of the values passed to it as parameters from the main() function.
The main function makes two calls to the function sum(), one by passing the variables which contain the
values for which the sum is to be calculated and the other function call where the values are directly passed
as parameters to the function.
6.5 Parameters in C :
If a function is to use arguments, it must declare variables that accept the values of the arguments. These
variables are called the formal parameters of the function. A parameter is the symbolic name for "data" that
goes into a function.The formal parameters behave like other local variables inside the function and are
created upon entry into the function and destroyed upon exit.
While calling a function, there are two ways that arguments can be passed to a function:

Call by value : This method copies the actual value of an argument into the formal parameter of the
function. In this case, changes made to the parameter inside the function have no effect on the
argument.

Call by Reference : This method copies the address of an argument into the formal parameter. Inside
the function, the address is used to access the actual argument used in the call. This means that
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
changes made to the parameter affect the argument.
By default, C uses call by value to pass arguments. In general, this means that code within a function cannot
alter the arguments used to call the function.
Figure 6.1
The above figure 6.1 shows the execution of the particular line which is highlighted in the code. The integer
variables initially have a garbage value stored, as we can see from the watch window, before the value
returned from the function call is assigned in it.
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
Figure 6.2
The above figure 6.2 shows execution of the statements inside the function sum(). The function key F11 can
be used to jump inside the function to see the line by line execution of the statements. To use it, press the
function key when the particular function call in the main() function is highlighted to jump to the function
definition.
Figure 6.3
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
The above figure 6.3 shows value change in the watch window once the function call has been executed and
the control returns to the main() function to execute the second function call to the same function.
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)