Problem Solving using C Language

0 of 76 lessons complete (0%)

Functions

Elements of Functions

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:

  1. Transfer Control: When a function is called, control is transferred to the function’s code block.
  2. Execute Function: The function performs its defined task.
  3. 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 and b) and return their sum.
  • Function Call (add(x, y)): In main(), the add() function is called with arguments x and y. 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.