Unit5 - Functions
Unit5 - Functions
Unit5 - Functions
UNIT - 5
Parameter (Argument) names are not important in function declaration only their type is
required, so following is also valid declaration: int max(int, int);
2) Calling a Function:
While creating a C function, we must provide a definition of, what the function has to do. To
use a function, we have to call that function to perform the defined task.
When a program calls a function, program control is transferred to the called function. A
called function performs defined task and when its return statement is executed or when its
function-ending closing brace is reached, it returns program control back to the main
program.
To call a function, we simply need to pass the required parameters along with function name,
and if function returns a value, then we can store returned value.
3) Defining a Function:
The general form of a function definition in C programming language is as follows:
Function Arguments:
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. The formal
parameters behave like other local variables inside the function and are created upon entry
into the function and destroyed upon exit.
In this method, no arguments are passed to the called function (Addition() )& no values are
returned to the calling function (main().
In this method, no arguments are passed to the called function (Multiplication()), but a value is
returned to the calling function (main()). The data type of the return value will depend upon the
return type of function declaration. For instance, if the return type is int, then return value will be
int.
4
In this method , arguments are passed to the called function (Addition()), but does not return any
value to the calling function (main()).
This method allows to pass the arguments to the called function (Multiplication()) & returns some
value to the calling function (main()). Data type of the return value will depend upon the return type
of function declaration. For instance, if the return type is int, then return value will be int.
5
II. Call by reference method copies the address of an argument into the formal
parameter. In this method, the address is used to access the actual argument used in
the function call. It means that changes made in the parameter alter the passing
argument. In this method, the memory allocation is the same as the actual
parameters. All the operation in the function are performed on the value stored at
the address of the actual parameter, and the modified value will be stored at the
same address.
printf("After swapping values in main a = %d, b = %d\n",a,b); // The values of actual parameters
do change in call by reference, a = 10, b = 20
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
printf("After swapping values in function a = %d, b = %d\n",*a,*b); // Formal parameters, a = 20
, b = 10
}
Output
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10
Function as an argument
{
int y=2,x;
x = double(square(y)); printf(“x=%d”,x);
}
int double(int m)
{
int p;
p=m*2;
return(p);
}
int square(int k)
{
int z;
z = k * k;
return(z);
}
Output: x = 8
***************************