C Functions program
C Functions program
Any C Function can be invoked or called by another. The invoking function typically passes
information to the invoked function. The invoked function may return information to its
invoker. All combinations are possible:
The parameters (sometimes called formal parameters) are placeholders for the arguments
that the function expects. The parameters are enclosed in parenthesis and separated by
commas.
Example 1
/* start of header */
float abs_diff(num1, num2)
float num1, num2;
/* end of header */
/* start of body */
{
If (num1 > num2)
return (num1 – num2);
else
return (num2 – num1);
}
/* end of body */
FUNCTION CALLS
When a function is invoked, its statements begin executing until either a return statement
or the last statement in its body is executed, after which the statement in the invoking
function resume execution in their regular sequence.
The return statement
When a return statement in function is executed, the function returns to its invoker. The
return statement is optional in a function that does not return a value but if used, it is written
Return;
A function’s parameters should match the function’s arguments in number and data type.
For example if a function is invoked with two arguments, it should have two parameters. If
the arguments are of type int and char respectively, then the first parameter should be of type
int and the second of type char.
Examples
Void main ()
{
int num1 = 5;
int num2 = 2;
int ans1, ans2;
int square (); /* declaring a function */
We show two calls to square to illustrate both a simple variable and a complex expression
as an argument.
Call by value lets us pass arguments to a function but with a guarantee that the original
arguments will remain unchanged no matter what the invoked function does to the copies.
The difference between
ans1 = square (num1);
ans2 = square (num1 + num2);
is that in ans1, the argument to square is a simple one while in ans2 the argument to square
is a complex expression which has to be evaluated before being passed. Also note that in the
function square, the parameter numb receives the value of the argument (expression) being
passed to the function from the main function.
So when
ans2 = square (num1 + num2);
is executed, the value of the expression: num1 + num2 is computed which is 7, then a
temporary cell numb is created and the value of the argument is copied to numb.
Note that num1’s contents have not changed from the first call to square and that this call
also does not alter the contents of either num1 or num2.
Therefore the value of ans1 is 25 and that of ans2 is 49.
The benefit of call by value is that it protects the values of the arguments in the invoking
function from being changed by the invoked function.
Example
/* Call by Reference */
Main ( )
{
int num1 = 25;
int num2;
int *ptr = &num1;
int add1( );
After this, the function add1( ) returns 26 to main, which is assigned to num2. After
executing the statement:
num2 = add1 (ptr);
in main, both num1 and num2 hold 26. So with respect to the variable num1, the call to
add1 is call by reference.
function program
#include <stdio.h>
void my_function();
main()
{
printf("Main function.\n");
my_function();
return 0;
}
void my_function()
{
printf("Welcome to my function. Feel at home.\n");
}