Functions in C
Defining and calling functions in C, parameters, return values, prototypes and pass by value.
A function has a return type, a name, and parameters. Calling it runs its body with the arguments copied into the parameters.
C passes arguments by value: the function gets copies, so changing a parameter doesn't change the caller's variable. To let a function change the caller's variable, pass a pointer to it.
A function must be declared before it's used. Either define it above main, or put a prototype (just the first line, ending with ;) near the top or in a header file.
Example
#include <stdio.h>
int square(int x);
int main(void) {
printf("%d\n", square(7));
return 0;
}
int square(int x) {
return x * x;
}
Output:
49
Watch it run: Function calls and the stack