Define and Call a Function
Announce another table
A room coordinator needs to announce several ready tables. Instead of writing each message separately, put that job in a function. The definition void announce(int number) names the function, takes an int parameter, and returns no value. announce(3); is a call supplying the argument 3. Execution starts in main, even if another function appears first in the file.
The braces after the function header enclose its body: the statements that run when it is called. Merely defining it does not run those statements. Each call enters announce, prints a message, and returns to the next statement in main. Its local parameter number gets a fresh value for each call. With the definition before main, C can see it at both call sites:
#include <stdio.h>
void announce(int number) {
printf("Table %d is ready\n", number);
}
int main(void) {
announce(2);
announce(5);
return 0;
}
Run the example: the first call prints table 2, returns to main, and only then does the second call print table 5. On paper, replace the first argument with 7; only that announcement would change.
What each call owns
The void before announce means it returns no value. The void in main(void) means main takes no arguments in this form. Reaching the closing brace of announce returns control without a value; a return; statement could do that earlier.
An argument is the value supplied at a call; a parameter is the function’s local variable that receives it. announce(2) and announce(5) do not share a persistent number. Since announce returns void, call it as a statement, not as a numeric expression. Give a function one clear job and pass it the values that job needs.