Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

What is user defined function in C programming ?

In C programming, a user-defined function is a function that is created by the programmer to perform a specific task. It allows the programmer to break down the program into smaller, more manageable parts, which can be easier to read and maintain.

Here is an example program that demonstrates how to create and use a user-defined function in C:

#include <stdio.h>

// Function declaration
int sum(int a, int b);

int main() {
    int num1 = 5, num2 = 7, result;
    
    // Function call
    result = sum(num1, num2);
    
    printf("The sum of %d and %d is %d", num1, num2, result);
    
    return 0;
}

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

In this program, we define a function called sum which takes two integer arguments and returns their sum. The function is declared before the main function so that it can be used inside main.

Inside main, we declare two integer variables num1 and num2 and initialize them with values of 5 and 7 respectively. We then call the sum function with num1 and num2 as arguments and store the result in a variable called result. Finally, we print the result using printf.

When you run this program, you should see the output:

The sum of 5 and 7 is 12