Functions in C
Functions in C
FUNCTIONS IN ‘C’
15.1 INTRODUCTION
In the earlier lessons we have already seen that C supports the use
of library functions, which are used to carry out a number of com-
monly used operations or calculations. C also allows programmers
to define their own functions for carrying out various individual tasks.
In this lesson we will cover the creation and utilization of such user-
defined functions.
15.2 OBJECTIVES
l explain of function
l describe access to function
l define parameters data types specification
l explain function prototype and recursion
l define storage classes – automatic, external, static variables
The first line of a function definition contains the data type of the
information return by the function, followed by function name, and
a set of arguments or parameters, separated by commas and enclosed
in parentheses. The set of arguments may be skipped over. The data
type can be omitted if the function returns an integer or a charac-
ter. An empty pair of parentheses must follow the function name if
the function definition does not include any argument or param-
eters.
Functions in ‘C’ :: 235
#include <stdio.h>
main()
{
int x,y;
maxi(int, int); /*function declaration*/
printf(“Enter two integer values”);
scanf(“%d %d”’ &x,&y);
maxi(x,y); /*call to function*/
}
maxi(x,y) /*function definition*/
int x,y;
{
int z;
z=(x>=y)?x:y; Body of the
function
print(“\n\n Maximum value %d”,z); maxi
return;
}
This ‘maxi’ function do not return any value to the calling program,
it simply returns the control to the calling programs, so if it is even
not present, then also program will work efficiently.
INTEXT QUESTIONS
#include <stdio.h>
main()
{
int a,b,c;
printf(“Enter two numbers”);
scanf(“%d%d”, &a,&b);
c=sum_v(a,b,);
printf(“\n The sum of two variables is %d\n,”,c);
}
sum_v(a,b)
int a,b
{
int d;
d=a+b;
return d;
}
This program returns the sum of two variables a and b to the calling
program from where sum_v is executing. The sum is present in the
variable c through the ‘return d’ statement. There may be several
different calls to the same function from various places within a
program. The actual parameters may differ from one function call to
another. Within each function call, the actual arguments must cor-
respond to the formal arguments in the function definition, i.e. the
number of actual arguments must be same as the number of formal
arguments and each actual argument must be of the same data
type as its corresponding formal argument.
238 :: Computer Applications
#include <stdio.h>
main()
{
int a,b,c,d;
printf(“\n Enter value of a=”);
scanf(“%d”, &a);
printf(“\n Enter value of b=”);
scanf(“%d”,&b);
printf(“\n Enter value of c=”);
scanf(“%d”, &c);
d=maxi(a,b);
printf(“\n maximum =%d”, maxi(c,d));
}
maxi(x,y);
int x,y
{
int z;
z=(x>=y)? x:y;
return z;
}
#include <stdio.h>
main()
{
int x=3;
printf(“\n x=%d(from main, before calling the
function”),x);
change(x);
printf(“\n\nx=%d(from main, after calling the
function)”,x);
}
change(x)
int x;
{
x=x+3;
printf(“\nx=%d(from the function, after being
modified)”,x);
return;
}
The original value of x (i.e. x=3) is displayed when main begins ex-
ecution. This value is then passed to the function change, where it
is sum up by 3 and the new value displayed. This new value is the
altered value of the formal argument that is displayed within the
function. Finally, the value of x within main is again displayed, after
control is transferred back to main from change.
#include <stdio.h>
main()
{
int number;
long int fact(int number);
242 :: Computer Applications
printf(“Enter number”);
scanf(“%d”, & number);
printf(“Factorial of number is % d\n”, fact(number));
}
long int fact(int number)
{
if(number <=1)
return(1);
else
return(number *fact(number-1));
}
The point to be noted here is that the function ‘fact’ calls itself re-
cursively, with an actual argument (n-1) that decrease in value for
each successive call. The recursive calls terminate the value of the
actual argument becomes equal to 1.
INTEXT QUESTIONS
a part of the variable definitions, but the initial values must be ex-
pressed as constants rather than as expression. These initial values
will be assigned only once, at the beginning of the program. If an
initial value is not included in the definition of an external variable,
the variable will automatically be assigned a value of zero.
static int a;
If the keyword static is replaced with the keyword auto, the variable
is declared to be of storage class auto. If a static local variable is
assigned a value the first time the function is called, that value is
still there when the function is called second time.
display_number()
{
static int number=2;
printf(“number=%d\n”, number);
number++;
}
In this lesson you have learnt about functions, how to define and
declare functions, what are the functions different data types. You
are now able to execute function from different places of a program.
You are now familiar with useful feature of functions say recursion.
In this lesson we are also discussed different storage classes like
auto, extern and static.
3. One
4. Yes
5. It means the function does not return any value to the calling
program
6. It is not necessary
7. Recursion is a process by which a function calls itself repeat-
edly.