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

Function can be categorized into four types:

1.Functions with no arguments and no return values

2. Functions with arguments and no return values

3. Functions with arguments and return values

4. Functions with no arguments and return values

1.Functions with no arguments and no return values

Functions with no arguments and no return values are commonly referred to as "void functions." These functions perform a specific task or set of tasks without taking any input parameters and without returning any value.

Syntax:

void functionName() {
    // Function body
    // Statements to execute
}

int main() {
    // Function call
    functionName();
    return 0;
}

void: Specifies that the function doesn't return any value. If a function doesn't return a value, you use void as the return type.

functionName: This is the name of the function. Choose a descriptive name that reflects the purpose of the function.

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

Example 

#include <stdio.h>

// Void function definition
void add() 
{
    int a,b,sum;
    printf("enter the value of a and b\n");
    scanf("%d%d",&a,&b);
    sum=a+b;
    printf("Sum is %d",sum);
}

int main() 
{
    // Function call
    add();
    return 0;
}

2. Functions with arguments and no return values

Functions with arguments and no return values are quite common in C programming. They're useful when you need a function to perform some action or task using input values but don't need to return any result. 

void functionName(argument1_type arg1, argument2_type arg2, ...) {
    // Function body
    // Statements to execute
}

int main() {
    // Function call
    functionName(value1, value2, ...);
    return 0;
}

void: Specifies that the function doesn't return any value. If a function doesn't return a value, you use void as the return type.

functionName: This is the name of the function. Choose a descriptive name that reflects the purpose of the function.

argument1_type, argument2_type, ...: These are the data types of the function's parameters (arguments).

arg1, arg2, ...: These are the names of the function's parameters (arguments).

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

Example:

#include <stdio.h>

// Void function definition
void add(int x, int y) 
{
    sum=x+y;
    printf("Sum is %d",sum);
}

int main() 
{
    int a, b;
    printf("enter the value of a and b\n");
    scanf("%d%d",&a,&b);
    // Function call
    add(a,b);
    return 0;
}

3. Functions with arguments and return values

Functions with arguments and return values are very common in C programming. These functions take input values (arguments), perform some computation, and then return a result. The return value can be of any data type, including basic types like int, float, char, or even user-defined structures.

Syntax:

return_type functionName(argument1_type arg1, argument2_type arg2, ...) {
    // Function body
    // Statements to execute
    return result; // (optional) Return statement
}

int main() {
    // Function call
    return_type result = functionName(value1, value2, ...);
    return 0;
}

return_type: Specifies the data type of the value that the function returns. It can be any valid C data type, including user-defined types. If the function doesn't return a value, you use void as the return type.

functionName: This is the name of the function. Choose a descriptive name that reflects the purpose of the function.

argument1_type, argument2_type, ...: These are the data types of the function's parameters (arguments).

arg1, arg2, ...: These are the names of the function's parameters (arguments).

Function body: This contains the set of statements that define what the function does when it's called. It's 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.

Example:

#include <stdio.h>

// function definition
int add(int x, int y) 
{
    sum=x+y;
    return sum;
}

int main() 
{
    int a, b, s;
    printf("enter the value of a and b\n");
    scanf("%d%d",&a,&b);
    // Function call
    s=add(a,b);
    printf("Sum is %d",s);

    return 0;
}

4. Functions with no arguments and return values

Functions with no arguments and return values are quite straightforward. They perform a specific task or set of tasks without taking any input parameters and without returning any value. 

Syntax:

void functionName() {
    // Function body
    // Statements to execute
}

int main() {
    // Function call
    functionName();
    return 0;
}

void: Specifies that the function doesn't return any value. If a function doesn't return a value, you use void as the return type.

functionName: This is the name of the function. Choose a descriptive name that reflects the purpose of the function.

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

Example:

#include <stdio.h>

// function definition
int add() 
{
    int a, b, s;
    printf("enter the value of a and b\n");
    scanf("%d%d",&a,&b);    
    sum=x+y;
    return sum;
}

int main() 
{
    
    // Function call
    s=add();
    printf("Sum is %d",s);
    return 0;
}