User-defined functions


In the C programming language, user-defined functions allow you to define your own functions to perform a specific task apart from the standard library functions. These functions provide a way to modularize your code, make it more organized, and improve code reusability. Therefore, you can create a function to perform specific tasks and call it from different parts of your program.

Note: User-defined functions, also known as โ€œtailor-made functions,โ€ build only to satisfy the given condition.

To create a user-defined function in C, you need to follow these steps:

  1. Function Prototype:

    Declare the function prototype at the beginning of your program. The prototype informs the compiler about the functionโ€™s name, return type, and parameters (if any). This step is optional if you define the function before calling it in the code.

  2. Function Definition:

    Define the function body where you describe what the function does. The function definition includes the return type, function name, and parameters (if any). The function body contains the statements executed when you call the function.
  3. Function Call:

    To use the user-defined function in your code, you call it from within the main function or any other function, depending on where you desire the functionโ€™s functionality to execute.

    ย 

Syntax for creating a user-defined function in C:

// Function prototype (optional)
return_type function_name(data_type parameter1, data_type parameter2, ...) ;

// Function definition
return_type function_name(data_type parameter1, data_type parameter2, ...) {
    // Function body (statements to be executed)
    // You can use the parameters and perform tasks here
    // Optionally, return a value if the return type is not void
}

int main() {
    // Function call
    function_name(arg1, arg2, ...);
    return 0;
}


Letโ€™s see an example of a simple user-defined function that calculates the sum of two integers:



For instance:

#include <stdio.h>

// Function prototype (optional)
int sum(int a, int b);

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

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

Hereโ€™s how it the for break works:

  1. Function prototype:
    The function prototype int sum(int a, int b); informs the compiler about the sum function, which takes two integer parameters (int a and int b) and returns an integer (int). Itโ€™s optional here, but including it is good practice for clarity, especially when functions are defined after they are called.

  2. Function definition:
    The function definition provides the actual implementation of the sum function. It starts with the return type (int in this case), followed by the function name (sum), and the parameters inside parentheses (int a and int b). The function body is enclosed in curly braces {}.

    In this example, the function calculates the sum of its two integer parameters (a and b) and stores the result in a local variable called result. The return statement then sends the result back to the calling function (in this case, the main function).

  3. main function:
    The main function serves as the entry point of every C program and requires no explicit call; the program automatically starts executing from this function.

  4. Function call:
    Inside the main function, we declare two integer variables num1 and num2 and initialize them with the values 5 and 10, respectively. To calculate the sum of num1 and num2, we call the sum function with the arguments num1 and num2. Hence, this is achieved with the line int result = sum(num1, num2);.

  5. Printing the result:
    After calling the sum function, the result of the addition is stored in the variable result. We then use the printf function to display the result to the console. The line printf(โ€œThe sum is: %d\nโ€, result); prints the message โ€œThe sum is:โ€ followed by the value of result (the sum of num1 and num2).

When you run this program, it will output:

Output

The sum is: 15

The user-defined function sum accurately calculates the sum of num1 and num2, and it prints the result using printf.

Thus, in this example, we declare a user-defined function sum that takes two integers as parameters and returns their sum.ย The main function calls the function with num1 and num2 as arguments, and it prints the result to the console.

In C programming, you should define functions before calling them.If you place the function definition after the main function, you should provide a function prototype before main to inform the compiler about the functionโ€™s signature. However, itโ€™s generally a good practice to define the function before calling it or place the function in a separate header file and include it in your code.


ย