The function call statement invokes the function. When a function is invoked the compiler jumps to the called function to execute the statements that are part of that function.
FUNCTION CALL
The
function call statement invokes the function. When a function is invoked the
compiler jumps to the called function to execute the statements that are part
of that function. Once the called function is executed, the program control
passes back to the calling function.
Function
call statement has the following syntax:
function_name (variablel,
variable2, ...) ;
When
the function declaration is present before the function call, the compiler can
check if the correct number and type of arguments are used in the function call
and the returned value, if any, is being used reasonably.
Function
definitions are often placed in separate header files which can be included in
other C source files that wish to use
these functions. For example, the header file stdio.h contains the definition of scanf and printf functions. We simply include this header file and call these
functions without worrying about the code to implement their functionality.
Note
List
of variables used in function call is known as actual parameter list. The
actual parameter list may contain variable names, expressions, or constants.
The
following points are to be kept in mind while calling functions:
•
Function name and the number and type of arguments in the function call must be
same as that given in the function declaration and function header of the
function definition.
Programming Tip: A
logical error will be generated if the arguments in the function call are
placed in the wrong order.
•
If the parameters passed to a function are more than what it is specified to
accept then the extra arguments will be discarded.
•
If
the parameters passed to a function are less than what it is specified to
accept then the unmatched arguments will be initialized to some garbage value.
•
Names (and not the types) of variables in function declaration, function call,
and header of function definition may vary.
•
If the data type of the argument passed does not match with that specified in
the function declaration then either the unmatched argument will be initialized
to some be garbage value or a compile time error will be generated.
•
Arguments may be passed in the form of expressions to the called function. In
such cases, arguments are first evaluated and converted to the type of formal
parameter and then the body of the function gets executed.
•
The parameter list must be separated with commas.
•
If the return type of the function is not void, then the value returned by the
called function may be assigned to some variable as shown in the following
statement.
variable_name = function_name
(variablel, variable2, ...);
Let
us now try writing a program using function.
1.
Write a program to add two integers using functions.
#include <stdio.h>
// FUNCTION DECLARATION
int sum (int a, int b);
int main()
{
int num1, num2, total =0;
printf("\n Enter the first
number: ");
scanf("%d", &num1);
printf("\n Enter the second
number: ");
scanf("%d", &num2);
total = sum (num1, num2);
// FUNCTION CALL
printf("\n Total = %d",
total);
return 0;
}
// FUNCTION DEFINITION
int sum (int a, int b) // FUNCTION
HEADER
{ // FUNCTION BODY
int result;
result= a + b;
return result;
}
Output
Enter the first number: 20
Enter the second number: 30
Total = 50
The
variables declared within the function and its parameters are local to that
function. The programmer may use same names for variables in other functions.
This eliminates the need for thinking and keeping unique names for variables
declared in all the functions in the program.
Programming Tip:
It
is an error to use void as the return type when the function is expected to
return a value to the calling function.
In
the sum() function, we have declared a variable result just like any other variable. Variables declared within a
function are called automatic local variables because of two reasons.
•
First, they are local to the function. So, their effect (in terms of scope,
lifetime, or accessibility) is limited to the function. Any change made to
these variables is visible only in that function.
•
Second, they are automatically created whenever the function is called and
cease to exist after control exits the function.
Note
Programming in C: Unit III (a): Functions : Tag: : with Example C Programs - Function Call
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation