Lesson 10 of 31

Functions

Functions

Functions in C are declared with a return type, name, and parameter list:

int add(int a, int b) {
    return a + b;
}

Function Prototypes

In C, a function must be declared before it is called. You can either define it before use, or declare a prototype:

int add(int a, int b);  // prototype (declaration)

int main() {
    printf("%d\n", add(3, 4));  // OK -- declared above
    return 0;
}

int add(int a, int b) {  // definition
    return a + b;
}

Like Scotty's engineering subroutines: "I've given her all she's got, Captain!" A well-defined function does one job and does it right.

void Functions

Functions that return nothing use void:

void greet(const char *name) {
    printf("Hello, %s!\n", name);
}

Parameters are Passed by Value

In C, function arguments are copied. Modifying a parameter inside a function does not affect the caller's variable:

void try_modify(int x) {
    x = 99;  // only modifies the local copy
}

int main() {
    int a = 5;
    try_modify(a);
    printf("%d\n", a);  // still 5
}

Your Task

Write a function int max(int a, int b) that returns the larger of two integers. Use it in main to print the results of max(3, 7), max(10, 2), and max(5, 5), each on a separate line.

TCC compiler loading...
Loading...
Click "Run" to execute your code.