Skip to main content

Function definition; sum of two numbers

Short answer

A function definition is the complete code of a function with header and body. A program to sum two numbers reads two integers, calls a user-defined function int sum(int a, int b) { return a + b; }, and prints the result. For input 12 and 30, output is Sum = 42.

Define "function definition" and write the program to find the sum of two numbers using user-defined functions.

A function definition is the complete code of a function: the header (return type, name, parameters) followed by the body in braces which performs the task. A function must be defined once, and it can be called many times.

#include <stdio.h>
int sum(int a, int b);
int main(void)
{
    int x, y;
    printf("Enter two numbers: ");
    scanf("%d %d", &x, &y);
    printf("Sum = %d\n", sum(x, y));
    return 0;
}
int sum(int a, int b)
{
    return a + b;
}

Output for the input 12 and 30:

Enter two numbers: Sum = 42

The prototype int sum(int a, int b); tells the compiler about the function; sum(x, y) calls it with the actual parameters x and y; the definition adds the formal parameters a and b and returns a + b.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 6 questions →