Types of User-defined Functions in C

In C programming language, there are two types of user-defined functions:

  1. Function with no arguments and no return value
  2. Function with no arguments but returns a value
  3. Function with arguments and no return value
  4. Function with arguments and a return value

Function with no arguments and no return value

In C programming language, a function with no arguments and no return value is used when we want to perform a task that does not require any input from the user and does not need to return any value. For example, a function that displays a message on the screen.

Here’s an example of a function with no arguments and no return value:

#include <stdio.h>

void display_message() {
    printf("Hello, World!\n");
}

int main() {
    display_message();
    return 0;
}

In this example, the “display_message” function takes no arguments and returns no value. It simply prints the message “Hello, World!” to the screen using the “printf” function from the standard input/output library.

To call the function, we simply use its name followed by parentheses, like this: “display_message();”. In the “main” function, we call the “display_message” function to print the message to the screen.

Note that the function prototype for the “display_message” function is not required in this case because the function is defined before it is called in the program. However, it is good practice to declare the function prototype before calling the function to avoid any potential issues.

Function with no arguments but returns a value

In C programming language, a function with no arguments but returns a value is not possible because the return value of a function is determined by the function’s logic, which in turn usually depends on some inputs provided through function arguments.

However, if a function does not require any input arguments but needs to return a value, we can define a variable within the function and return it. Here’s an example:

#include <stdio.h>

int generate_random_number() {
    int random_number;
    random_number = rand(); // rand() function generates a random number
    return random_number;
}

int main() {
    int number;
    number = generate_random_number();
    printf("Random number: %d\n", number);
    return 0;
}

In this example, the “generate_random_number” function generates a random number using the “rand” function from the standard library and returns it. The “main” function calls the “generate_random_number” function and assigns its return value to the “number” variable, which is then printed to the screen.

Although this function does not take any input arguments, it relies on the “rand” function to generate the random number. Therefore, it cannot be considered as a function with no input arguments.

Function with arguments and no return value

In C programming language, a function with arguments and no return value is used when we want to perform a task that requires some input from the user but does not need to return any value. For example, a function that takes two numbers as input and prints their sum on the screen.

Here’s an example of a function with arguments and no return value:

#include <stdio.h>

void print_sum(int a, int b) {
    int sum = a + b;
    printf("Sum of %d and %d is: %d\n", a, b, sum);
}

int main() {
    int x = 5, y = 10;
    print_sum(x, y);
    return 0;
}

In this example, the “print_sum” function takes two integer arguments, “a” and “b”, calculates their sum, and prints the result on the screen using the “printf” function from the standard input/output library.

To call the function, we pass two integer values as arguments separated by commas within the parentheses, like this: “print_sum(x, y);”. In the “main” function, we call the “print_sum” function with two integer values, “x” and “y”, to print their sum to the screen.

Note that the function prototype for the “print_sum” function must be declared before the function is called in the program. The function prototype specifies the function name, argument types, and return type.

Function with arguments and a return value

In C programming language, a function with arguments and a return value is used when we want to perform a task that requires some input from the user and returns a value as a result of that task. For example, a function that takes two numbers as input and returns their sum.

Here’s an example of a function with arguments and a return value:

#include <stdio.h>

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

int main() {
    int x = 5, y = 10, result;
    result = add(x, y);
    printf("Sum of %d and %d is: %d\n", x, y, result);
    return 0;
}

In this example, the “add” function takes two integer arguments, “a” and “b”, calculates their sum, and returns the result as an integer value.

To call the function, we pass two integer values as arguments separated by commas within the parentheses, like this: “add(x, y);”. In the “main” function, we call the “add” function with two integer values, “x” and “y”, and assign its return value to the “result” variable. Then, we print the result using the “printf” function from the standard input/output library.

Note that the function prototype for the “add” function must be declared before the function is called in the program. The function prototype specifies the function name, argument types, and return type.

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial