Functions 317
10.2 DEFINITION OF A FUNCTION
A function is a self-contained block or a sub-program of one or more statements that perform a
special task when called.
Why use Functions
1) If we want to perform a task repeatedly then it is not necessary to rewrite the particular block of
a program again and again. Shift the particular block of statements in a user-defined function.
The function defined can be called any number of times to perform the task.
2) Using functions, large programs can be reduced to smaller ones. It is easy to debug and find out
the errors in it. It also increases readability.
How a Function Works
1) Once a function is defined and called, it takes some data from the calling function and returns a
value to the called function.
2) The details of inner working of a function is unknown to the rest of the program. Whenever a
function is called, control passes to the called function and working of calling function is paused.
When the execution of the called function is completed, control returns back to the calling function
and executes the next statement.
3) The values of actual arguments passed by the calling function are received by the formal
arguments of the called function. The number of actual and formal arguments should be the
same. Extra arguments are discarded if they are defined. If the formal arguments are more than
the actual arguments then the extra arguments appear as garbage. Any mismatch in the data
type will produce an unexpected result.
4) The function operates on formal arguments and sends back the result to the calling function.
The return () statement performs this task.
10.3 DECLARATION OF A FUNCTION
A function is declared as per the format given below.
funct ion_name (argument/parameter list)
argument declaration;
{ '
local variable declaration;
Statementl;
Statement2;
return(value);
}
Table 10.1 simulates the working of a function with its necessary components.
Table 10.1 Working of a function
318 Programming and Data Structures
>
---
>
abc(l,k, j)
Function definition
{ |
--
>
Formal
argument
return (); return value
}
a) Actual argument: The arguments of calling functions are actual arguments. In Table 10.1, variables
V , 'y' and 'z' are actual arguments.
b) Formal argument: The arguments of the called function are formal arguments. In Table 10.1,
variables T, 'k' and 'j' are formal arguments.
c) Function name: A function must follow the same rule as we use for variable naming.
Example sum (int a, int b);
where^sum () is a user-defined function and 'a' and 'b ' are integer variable arguments. The function
name must be ended by a semicolon (;).
d) Argument/parameter list: The argument list means variable names enclosed within the
parentheses. They must be separated by comma (,). These formal arguments (consignment) are to
be received values from the actual argument and for performing this a communication between
consignee and consignor functions is made.
e) Function call: A compiler executes the function when a semicolon (;) is followed by function
name. A function can be called simply by using its name like any other C statement, terminated
by semicolon (;).
Consider the following example.
10.1 Write a program to show how a user-defined function is called.
main()
{
int x = l, y=2, z ;
z=add(x,y); /* Function c a ll/
p rin tf ( wz=% d",z);
Functions 319
/* Function d e fin itio n */
add(a,b);
{
retum(a+b);
OUTPUT:
z=3
Explanation In the above program values of V and 'y' are passed to function add (). Formal
arguments'a' and 'b' receive the values. The function add () calculates addition of both the variables
and returns the result. The result is collected by the variable /z/ of the main () function which is
printed through the p r i n t f () statement.
10.2 Write a program to define a user-defined function. Call them at different places.
# in clude < stdio.h>
# inclu d e cc o n io .h>
vo id y ();
v o id y ()
{
p r in t f ("Y " );
}
v o id mainO
{
vo id a () ,b () ,c () , d ( );
c lr s c r O ;
y ( );
a ( );
b ();
c ();
d ();
vo id a ()
{
p r in t f ("A " );
y ( ) ;
}
320 Programming and Data Structures
void b ()
{
p r in t f(* B *)»
« ( ) ;
}
void c ()
{
a()i
b ()j
p r i n t f ( *C");
}
void d ()
{
p rin tf ( *D");
o ();
b (),
« ( ) »
}
QUTFUT;
YAYBAY AYBA YCDA YBAYCBAYAY
Explanation From the above program the following things are clear.
1) The main () function can call any other function defined before or after the main () function.
2) Any other user-defined function can call the main () function.
3) The user-defined function can call only those user-defined functions which are defined before it,
that is, the function a () can call only y () and not b (), c () and d (). The function b () cannot
call function c () and d (). The function d () can call all the functions because it is the last function.
f) Local and global variables: There are two kinds of variables: (i) local and (ii) global.
i) Local variables: The local variables are defined within the body of the function or block. The
variable defined is local to that function or block only. Other functions cannot access these variables.
The compiler shows error in case other functions try to access the variables.
Example
value (int k, int m)
{
int r, t;
}
Functions 321
Here, V and't' are the local variables, which are defined within the body of the function valu e ().
The same variable names may be defined in different functions. They are local to that particular
function.
Programs on local and global variables are illustrated below.
10.3 Write a program to show how similar variable names can be used in different functions.
main()
{
int b=10,c=5;
c lrs c r O ;
printf ("n In main () B=%d C=%d",b,c);
fun( );
}
fun ()
I
int b=20,c=10;
printf ("n In fun () B=%d C=%d",b,c);
}
OUTPUT:
In main () B=10 C=5
In fun () B=20 C=10
Explanation In the above program two functions are used. One is main () and the other user-
defined f un (). The variables V and 'c' are defined in both the functions. Their effect is only within
the function in which they are defined. Both the functions print local values of V and 'c' assigned in
their functions.
We can also declare the same variable names for actual and formal arguments. The compiler does
not confuse same variables. The scope of every auto variable is local to the block in which they are
defined.
ii) Global variables: Global variables are defined outside the main () function. Multiple functions
can use them. An example is illustrated below for understanding.
10.4 Write a program to show the effect of global variables on different functions.
int b=10 , c = 5 ;
main()
{
c lr s c r ();
printf ("n In mainO B=%d C=%d",b,c)
funO
b++;
c /
printf ("n Again in mainOB=%d C=%d",b,dt
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.33.235