C Reference ยท Functions and Scope
Parameters and arguments in C
Reference for C parameters and arguments: pass by value, argument conversion, default promotions, const parameters, arrays and structs as arguments.
A parameter is the variable named in a function's definition; an argument is the value supplied by the caller. C passes every argument by value: the function receives a copy, and assigning to the parameter changes only that copy. The caller's variable is untouched.
To let a function modify the caller's data, pass a pointer to it; the pointer itself is still passed by value, but it points at the original object. Arrays and functions are never copied: they are converted to pointers.
void bump(int n) { n++; } /* changes a copy */ void bump2(int *n) { (*n)++; } /* changes the caller's variable */
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
return-type name ( type1 param1, type2 param2 ) { /* ... */ } /* parameters */ name ( expression1, expression2 ); /* arguments */
| Name | Description |
|---|---|
| parameter | A local variable of the function, initialized from the matching argument when the function is called. |
| argument | An expression in the call. Each is converted to the parameter type as if by assignment, when a prototype is visible. |
... |
Variadic functions accept extra arguments, which undergo the default promotions. |
Rules: the number of arguments must equal the number of parameters (except for ...). Arguments are all evaluated, in unspecified order, with a sequence point before the body starts. Array-typed arguments become pointers to the first element; function-typed ones become function pointers. A parameter declared int a[10] or int a[] is int *a.
Errors / undefined behavior: too few or too many arguments, or a type that cannot be converted, is a compile error with a visible prototype. Passing the wrong number or types to a function without a prototype, or through a mismatched function pointer, is undefined behavior. Using an indeterminate (uninitialized) value as an argument is also undefined for most types.
Usage notes
- Pass by value everywhere. There is no pass by reference in C; use pointers. The usual pattern is a pointer parameter for output (
int *out) andconst T *for read-only inputs. See pointer parameters. - Large structs. Passing a struct by value copies all of it (including arrays inside it). For big ones, pass
const struct x *to avoid the copy. - Arrays lose their size.
sizeofof an array parameter is the pointer size. Pass a length, or a pointer to the array with a size (int (*a)[10]). See array parameters. - Default promotions apply to variadic arguments and to functions with no prototype:
charandshortbecomeint,floatbecomesdouble. With a prototype, the declared parameter types are used. constparameters.const int nstops the function modifying its own copy and is not part of the function's type;const char *sis about the pointed-to data and matters to callers.- Parameter lifetime. A parameter exists only during the call; returning a pointer to one is a dangling pointer.
- Order of evaluation.
f(i++, i++)is undefined (see sequence points). Compute arguments into variables first. - Restrict.
restricton pointer parameters promises the memory is not aliased, which allows optimizations; wrong promises cause undefined behavior. - Unused parameters. Omit the name (allowed in C23 definitions) or use
[[maybe_unused]]to silence warnings. - Modern signatures often include sizes and flags as separate parameters; keep the count small and group related values in a struct.