C PROGRAMMING
MULTIPLE CHOICE QUESTION
OLD QUESTION BANK
SAMPLE QUESTION 2080 AND SOLUTION

A function is a self-contained block of code that performs a specific task or set of tasks. 

Components of a function:

  • Function Name: This is the identifier used to call the function.
  • Return Type: Specifies the type of data that the function will return after its execution. If a function does not return any value, its return type is specified as void.
  • Parameters: These are optional and are used to pass values to the function. Parameters are enclosed within parentheses following the function name. They specify the data type and the name of each parameter.
  • Function Body: This contains the set of statements that define what the function does when it is called. It is enclosed within curly braces {}.
#include <stdio.h>

// Function prototype
int add(int a, int b);

int main() {
  int result;

  // Function call
  result = add(3, 5);

  printf("The sum is: %d\n", result);

  return 0;
}

// Function definition
int add(int a, int b) { return a + b; }

In the above example:

  • The function add takes two parameters a and b of type int and returns their sum.
  • Inside the main function, add is called with arguments 3 and 5.
  • The add function calculates the sum of its parameters and returns the result, which is then stored in the variable result.
  • Finally, the sum is printed out in the main function.