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

A user-defined function consists of three components:

Function Declaration: This component specifies the function's interface, including its name, return type, and parameter types (if any). The declaration tells the compiler about the function's existence and how it can be used. It usually appears before the function is called or defined. While it's optional in some cases (especially when the function definition precedes its usage), providing a declaration is good practice for clarity and maintainability.

Function Definition: This component contains the implementation of the function, including the actual code that performs the desired task. It specifies what the function does when called with particular arguments. The function definition includes the function header (name, return type, parameters) followed by the function body, which contains the statements to execute.

Function Call: This component involves invoking the function to execute its defined task. It includes the function name followed by parentheses containing the arguments (if any) required by the function. The function call passes control to the function's definition, executes its code, and may return a value back to the calling code.

Example:

#include <stdio.h>

// Function declaration
int add(int , int );

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 declaration int add(int , int ); appears before the main function, informing the compiler about the add function's signature.
  • The add function definition specifies the implementation of adding two integers.
  • Inside main, the add function is called with arguments 3 and 5, and the returned value is stored in the variable result.

1. Function Definition

 

A function definition in C consists of the function's header, followed by its body.

Syntax of Function Definition:

return_type function_name(parameter_list) {
    // Function body
    // Statements to execute
    return value; // (optional) Return statement
}

Explanation of Each Part :

return_type: This specifies the type of data that the function will return after its execution. If the function doesn't return any value, its return type should be void.

function_name: This is the identifier used to call the function. Choose a descriptive name that reflects the purpose of the function.

parameter_list: This is a list of parameters (arguments) that the function expects to receive when it is called. Parameters are optional, and you can have zero or more of them. Each parameter consists of a data type followed by a parameter name. Multiple parameters are separated by commas.

Function body: This contains the set of statements that define what the function does when it is called. It is enclosed within curly braces {}.

Return statement: If the function has a return type other than void, it must contain a return statement that specifies the value to be returned to the calling code. This statement is optional for void functions.

Example of Function Definition:

#include <stdio.h>

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

int main() {
    int result;
    
    // Function call
    result = add(3, 5);
    
    printf("The sum is: %d\n", result);
    
    return 0;
}

The function add takes two integer parameters a and b and returns their sum.

Inside the function body, sum is calculated as the sum of a and b.

The calculated sum is returned using the return statement.

In the main function, add is called with arguments 3 and 5, and the returned value is stored in the variable result.

Finally, the value of the result is printed to the console.

2. Function Prototype (Declaration)

 

A function declaration is similar to a function prototype, but it's typically used to inform the compiler about the existence and signature of a function without providing its full definition. This allows you to use the function before it's actually defined in the source code.

The syntax for a function declaration is:

return_type function_name(parameter_list);
  • return_type: Specifies the data type of the value that the function returns. If the function doesn't return anything, you use void as the return type.
  • function_name: This is the name of the function.
  • parameter_list: This is a list of parameters (arguments) that the function accepts. If the function doesn't take any parameters, you leave the list empty.

Example :

#include <stdio.h>

// Function declaration
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) {
    int sum = a + b;
    return sum;
}
  • The function declaration int add(int a, int b); appears before the main function, informing the compiler about the add function's signature.
  • The function add is then defined later in the code.
  • Inside the main function, add is called with arguments 3 and 5, and the returned value is stored in the variable result.
  • Finally, the value of result is printed to the console.

3. Function Call

Syntax of Function Call:

return_type variable_name = function_name(argument_list);
  • return_type: This is the data type of the value that the function returns. If the function returns nothing (void), you can omit this part.
  • variable_name: This is the name of the variable where you want to store the value returned by the function. If the function returns nothing (void), you can omit this part.
  • function_name: This is the name of the function you want to call.
  • argument_list: This is a comma-separated list of values (arguments) that you pass to the function. If the function doesn't take any arguments, you can omit this part or provide an empty list of parentheses.

Example :

#include <stdio.h>

// Function declaration
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) {
    int sum = a + b;
    return sum;
}
  • Inside the main function, the add function is called with arguments 3 and 5.
  • The returned value (the sum of 3 and 5) is stored in the variable result.
  • Finally, the value of result is printed to the console using printf.

4. Function Parameters 

Function parameters (also known as function arguments) are the values passed to a function when it's called. They allow the function to operate on data provided by the caller. 

Syntax:

return_type function_name(parameter1_type parameter1_name, parameter2_type parameter2_name, ...);
  • return_type: Specifies the data type of the value that the function returns. If the function doesn't return anything, you use void as the return type.
  • function_name: This is the name of the function.
  • parameter1_type, parameter2_type, ...: These are the data types of the parameters that the function expects to receive. If the function doesn't take any parameters, you leave the list empty or provide void to indicate no parameters.
  • parameter1_name, parameter2_name, ...: These are the names given to the parameters. Inside the function body, you can use these names to refer to the values passed by the caller.