Functions
A function is a self-contiained block of statements that perform a coherent task of some kind. Making function is a way of isolating one block of code from other independent blocks of code.
# include < std i o h>
void message ( ) ; /* Function prototype declaration*/
void main ( )
{
message ( ) ; / * Function cal I*/
pri ntf (“\n After calling function” ) ;
{
void message ( ) /* Function definition * /
{
pri ntf ‘(“\n I am called function”) ;
}
Output I am cal led functi on
After calling function, a function can take a number of arguments or parameters and a function can be called any number of times. A function can call itself such a process is called recursion.
Functions can be of two types
(I) Library functions (ii) User-defined functions
.Key Points
- Avoids rewriting the same code again and again e., ensures reusability.
- Separating the code into modular functions also makes the program easier to design and understand.
- The value of each of the actual arguments in the calling function is copied into corresponding formal argument of the called function. With this method, the changes made to the formal arguments in the called function have no effect on the values of actual arguments in the calling function.
Call by Value
If we pass values of variables to the function as parameters, such kind °I function calling is known as call by value.
#include <stdi0.h>
void swapv (int x, int y);
void main ( ) // calling function
{
int a := 10, b= 20;
// values of arguments in calling function will be
passed to the called function.
swapv (a, b);
printf (“\n a = %d b=%d”, a, b);
}
Void swapv (int x, int y) // called function
Formal arguments in the called function
{
int t;
t=x;
x=y;
y =t;
printf (“\n x= %d y =%d”, x, y);
}
Output x= 20 y = 10
a = 10 b= 20
Note The values of a and b remain unchanged even after exchanging or swapping the values of x and y.
Call by Reference
Variables are stored somewhere in memory. So, instead of passing the
value of a variable, if we pass the location number/address of the variable
to the function, then it would become ‘a call by reference’.
Key Points | .. “‘ |
- In call by reference method, the address of actual arguments in the calling ‘ function are copied into the formal arguments of the called function.
- This means that, using these addresses; we would have an access to the actual arguments and hence we would be able to manipulate them.
# include <stdio.h>
void swapr (int *, int *);
void main ( ) { int a= 10, b= 20; swapr (&a, &b); printf (“\n a= %d b= %d, a,b); } |
void swapr (int * x, int *y)
{
int t;
t =*x; // Hear, we are assigning value at x to
the variable t
*x=*y;
*y=t;
}
Previous to Swapping
In swapr (&a, &b); we are passing addresses of a and b i.e, 100 and
- Which are being taken by the pointer variables x and y. Now, we
are swapping the value at x to the value at y. That’s why when we are
getting the output it will be as follows
Output a = 20 b =10
After Swapping
This program manages to exchange the values of a and b using their
addresses stored in x and y.