Chapter-4
User Defined
Functions (UDFs)
|
syntax :
return type name(datatype variable, datatype
variable,….);
functions are set of instructions combined together
and given the name & can be used whenever you want by making a call
to that function. There are two types of functions : User defined functions
& Library functions. Both printf and scanf are examples of functions,
Other functions supplied by the system in what are called libraries.
A function without any
arguments
main()
{
display();
}
display()
{
printf("Syspro");
} |
This program makes a call to ‘display’ function which
prints Syspro on screen. Its is necessary that the function to which the
call is made should be defined in the same file.
Passing the arguments
to function :
while passing the arguments it is important to define
datatype of the argument. By default C considers that the passed value
is of integer type.
main()
{
sqr(8);
}
sqr(int val)
{
printf("squre of %d is %d", val, val*val);
} |
The above program makes a call to sqr function which
calculates the squre of the number, as well as displays the square of the
number.
As per the programming rules any function should
be cohesive which means it should solve only one purpose, it should not
display the square of the number passed. Just calculate the square and
return it.
Returning the value from
the function
This program makes a call to sqr function which only
calculates the square and returns calculated value which is stored in x
variable.
main()
{
int x;
x=sqr(8);
printf("\nsqure of 8 is %d", x);
}
sqr(int val)
{
return(val*val);
} |
Passing & returning
different datatypes.
This program makes a calls to function
float area_of_circle( float );
main()
{
float area;
area=area_of_circle(2.4);
printf("Area of circle is %f",area);
}
area_of_circle(float radius)
{
return(3.14*radius*radius);
} |
Scope of Variable
Variables can be of two types one local,
another global, depending upon place of declaration scope of variable changes.
Variable declared within a function is treated as local variable. Variable
declared before main function is treated as global variable. Local variable
is accessible locally to that function and not outside that function, but
global variables can be accessed anywhere in the program.
main()
{
int loc=30;
printf("\n loc = %d",loc);
callfunc();
}
callfunc()
{
printf("\n loc = %d",loc);
/* this statement will give the error message undefined symbol
loc */
} |
Above program will give an error message in callfunc()
function since the variable loc is defined in main function and is not
available for callfunc() function. The same variable can be made available
by making it of global type.
int g;
main()
{
g=34;
printf("Value of g is %d",g);
showg();
}
showg()
{
printf("Value of g is %d",g);
} |
int g;
This program will show the g variable since it is
of global variable. A global variable can be declared by defining it above
the main function.
Call by Value
Prameters passed to function are transfered through
call by value method. Which means though we transfer the variables to function
but it takes value only from them. Actual variables are not getting affected.
main()
{
int x=20,y=30;
printf("\n In main Function x = %d, y=%d",x,y);
swap(x,y);
printf("\n In main Function x = %d, y=%d",x,y);
}
swap(int x,int y)
{
int t;
t=x;
x=y;
y=t;
printf("\n In swap Function x = %d, y=%d",x,y);
} |
This program shows that variables defined in main
function when passed to swap function swaps the contents but as soon as
you return to main function they values you find are intact.
Here swap function is having its own local variables
with the same name as x and y. These variables are modified by swaping
but actual x and y variable remain intact as they were since variables
are never passed to function only values of variables are transfered.
|