Skip to main content

How a function is defined in C

Short answer

A function definition includes a return type, name, parameter list in parentheses, and a body in braces containing declarations and statements. For example, int maxof(int a, int b) { return (a > b) ? a : b; } defines a function that returns the larger of two integers.

Explain how function is defined in C.

A function definition gives the complete function: its header and its body.

General form:

return_type function_name(parameter list)
{
    declarations;
    statements;
    return value;
}
  • return_type: type of the value returned (void if nothing).
  • function_name: an identifier chosen by the programmer.
  • parameter list: the type and name of each formal parameter, separated by commas.
  • body: the declarations and statements within { }; a return statement sends the value back to the caller (it may be omitted in a void function).
#include <stdio.h>
int maxof(int a, int b)       /* return type, name, parameter list */
{                             /* body starts */
    if (a > b)
        return a;
    return b;
}                             /* body ends */
int main(void)
{
    printf("%d\n", maxof(14, 9));
    return 0;
}

Output: 14

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 6 questions →