LOGBOOK

HELP

Quiz Entry - updated: 2026.07.10

What is the structure of a C function definition?

A function is <return_type> <name>(<parameters>) { <body> }; use void for "returns nothing" or "takes nothing", and it must be declared (defined or prototyped) before it's called.

<return_type> <name>(<parameter_list>)
{
    <body>
}

Example - factorial:

int fact(int n)
{
    if (n > 1) {
        return n * fact(n-1);
    } else {
        return 1;
    }
}

Special cases:

No return value - use void:

void print_hello(void) {
    printf("Hello!\n");
    // No return statement needed (or use "return;")
}

No parameters - use void (recommended) or empty:

// Explicit: takes no arguments
int get_value(void) { ... }
// C: takes unknown arguments! (legacy)
int get_value() { ... }

Return composite types:

typedef struct { int key; int value; } pair;

pair make_pair(int k, int v) {
    pair p;
    p.key = k;
    p.value = v;
    // Returns entire struct by value (copy)
    return p;
}

Functions must be declared before use:

// Forward declaration (prototype)
int helper(int x);

int main(void) {
    // OK - helper is declared above
    return helper(5);
}

// Definition can come later
int helper(int x) {
    return x * 2;
}

From Quiz: REVE1 / C Programming | Updated: Jul 10, 2026