Function Call
A function call is the process by which a program invokes (or “calls”) a function to execute the block of code defined in that function. When a function is called, the program’s control is transferred to the function’s definition, where the task specified in the function is performed. Once the task is completed, control is returned to the point in the program where the function was called.
How a Function Call Works:
- Transfer Control: When a function is called, control is transferred to the function’s code block.
- Execute Function: The function performs its defined task.
- Return Control: After the function finishes its task, control is returned to the calling function (usually
main()
or another function).
Function Call Syntax:
function_name(arguments);
function_name
: The name of the function you want to call.arguments
: The values or variables you want to pass to the function. The number and type of arguments should match the function definition.
#include <stdio.h>
// Function definition
int add(int a, int b) {
return a + b;
}
int main() {
int x = 5, y = 10;
// Function call
int result = add(x, y);
printf("Sum: %d\n", result); // Output: Sum: 15
return 0;
}
Detailed Explanation:
- Function Definition: The function
add()
is defined to take two integers (a
andb
) and return their sum. - Function Call (
add(x, y)
): Inmain()
, theadd()
function is called with argumentsx
andy
. These values are passed to the function, and the function performs the addition. - Return Value: The result of the addition is returned by the function to the calling code and stored in the variable
result
.