C Function Parameters
Introduction to Function Parameters
Function parameters (also called formal parameters) are variables declared in the function signature that receive values when the function is called. They allow functions to accept input data and work with different values each time they're called.
Parameters make functions flexible and reusable. Without parameters, functions would always produce the same result or rely on global variables, which is considered poor practice.
Parameter vs Argument
Term | Definition | Example |
---|---|---|
Parameter | Variable in function declaration/definition | void printSum(int a, int b) |
Argument | Actual value passed to function | printSum(5, 3) |
Parameter Passing Techniques
C always passes arguments by value. To let a function modify a caller's variable, you pass the variable's address (a pointer), which emulates pass-by-reference.
- Pass by value: Copies the argument's value (default in C)
- Pass by pointer (emulates pass-by-reference): Pass the address using pointers so the function can modify the caller's object
- Array parameters: Arrays decay to pointers when passed; pass the length separately
Pass by Value Example
#include <stdio.h>
// Pass by value - parameters get copies of values
void swapByValue(int a, int b) {
int temp = a;
a = b;
b = temp;
printf("Inside function: a = %d, b = %d\n", a, b);
}
int main(void) {
int x = 5, y = 10;
printf("Before swap: x = %d, y = %d\n", x, y);
swapByValue(x, y);
printf("After swap: x = %d, y = %d\n", x, y);
return 0;
}
Before swap: x = 5, y = 10 Inside function: a = 10, b = 5 After swap: x = 5, y = 10
Pass by Pointer (Emulating Pass-by-Reference)
#include <stdio.h>
// Pass by pointer - parameters are addresses of variables
void swapByPointer(int *a, int *b) {
if (!a || !b) return; // validate pointers
int temp = *a;
*a = *b;
*b = temp;
printf("Inside function: *a = %d, *b = %d\n", *a, *b);
}
int main(void) {
int x = 5, y = 10;
printf("Before swap: x = %d, y = %d\n", x, y);
swapByPointer(&x, &y);
printf("After swap: x = %d, y = %d\n", x, y);
return 0;
}
Before swap: x = 5, y = 10 Inside function: *a = 10, *b = 5 After swap: x = 10, y = 5
Array Parameters
When arrays are passed to functions, they decay to pointers to the first element. This means you need to pass the size separately since sizeof() won't yield the original array size inside the function.
#include <stdio.h>
// Array parameter - arrays decay to pointers; mark read-only with const when appropriate
void printArray(const int arr[], int size) {
printf("Array elements: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// sizeof(arr) gives pointer size here, not array size
printf("sizeof(arr) in function: %zu bytes\n", sizeof(arr));
printf("sizeof(void*) on this platform: %zu bytes\n", sizeof(void*));
}
int main(void) {
int numbers[] = {1, 2, 3, 4, 5};
int size = (int)(sizeof(numbers) / sizeof(numbers[0]));
printf("sizeof(numbers) in main: %zu bytes\n", sizeof(numbers));
printArray(numbers, size);
return 0;
}
sizeof(numbers) in main: 20 bytes Array elements: 1 2 3 4 5 sizeof(arr) in function: 8 bytes sizeof(void*) on this platform: 8 bytes
Best Practices for Parameters
- Use const for pointer/array parameters that are not modified (e.g., const int arr[])
- Validate pointers (check for NULL) before dereferencing
- Always pass array length(s) alongside arrays; avoid using sizeof inside the callee to infer element count
- Keep parameter lists focused; consider structs to group related parameters
- Prefer meaningful parameter names for readability