Skip to main content

C Function Parameters

Function parameters in C allow you to pass data into functions, making them more versatile and reusable. This page covers how parameters work in C, different parameter passing mechanisms, and best practices.

What Are Function Parameters?

Function parameters are variables listed in the function declaration that act as placeholders for values passed to the function when it's called. These values are called arguments.

c
// Function with two parameters: 'a' and 'b'
int add(int a, int b) {
return a + b;
}

// Function call with arguments 5 and 3
int result = add(5, 3);

Parameter Passing Mechanisms in C

C uses "pass by value" as its default parameter passing mechanism, but you can simulate "pass by reference" using pointers.

Pass by Value

In pass by value, a copy of the argument's value is passed to the function. Changes to the parameter inside the function do not affect the original argument.

c
void increment(int num) {
num = num + 1; // Only modifies the local copy
printf("Inside function: %d\n", num);
}

int main() {
int x = 10;
increment(x);
printf("In main: %d\n", x); // Still 10, unchanged
return 0;
}

Pass by Reference (Using Pointers)

To modify the original variable, you can pass its address and work with it through a pointer.

c
void increment(int *num) {
(*num) = (*num) + 1; // Modifies the original variable
printf("Inside function: %d\n", *num);
}

int main() {
int x = 10;
increment(&x);
printf("In main: %d\n", x); // Now 11, changed
return 0;
}

Array Parameters

When passing arrays to functions, C passes a pointer to the first element of the array.

c
// Using array notation
void printArray(int arr[], int size) {
for(int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

// Using pointer notation (equivalent)
void printArrayAlt(int *arr, int size) {
for(int i = 0; i < size; i++) {
printf("%d ", arr[i]); // or *(arr + i)
}
printf("\n");
}
tip

Always pass the array size as an additional parameter since arrays don't carry size information when passed to functions.

Default Parameters

C does not support default parameters like C++ or other languages. All parameters must be provided in function calls.

Function Prototypes and Parameters

Function prototypes can include parameter names (recommended for documentation) or omit them:

c
// With parameter names (recommended)
int calculate(int width, int height);

// Without parameter names (valid but less readable)
int calculate(int, int);

Variable Number of Parameters

C supports functions with a variable number of parameters using the <stdarg.h> header.

c
#include <stdio.h>
#include <stdarg.h>

float average(int count, ...) {
va_list args;
va_start(args, count);

float sum = 0;
for(int i = 0; i < count; i++) {
sum += va_arg(args, int);
}

va_end(args);
return sum / count;
}

int main() {
printf("Average: %.2f\n", average(4, 10, 20, 30, 40));
return 0;
}

Parameter Type Checking

C performs minimal type checking on function arguments. If you pass an argument of a different type than the parameter, C will attempt to convert it if possible.

c
void example(int x) {
printf("Value: %d\n", x);
}

int main() {
example(3.14); // 3.14 is converted to 3
return 0;
}

Const Parameters

Use the const qualifier to indicate that a function will not modify a parameter:

c
// Function that promises not to modify the input array
int findMax(const int arr[], int size) {
int max = arr[0];
for(int i = 1; i < size; i++) {
if(arr[i] > max) {
max = arr[i];
}
// arr[i] = 0; // This would cause a compiler error
}
return max;
}

Best Practices

  • Be clear about ownership: Document whether a function takes ownership of memory allocations
  • Use const for read-only parameters: This prevents accidental modifications
  • Keep parameter lists short: Consider grouping related parameters into structures
  • Pass large structures by pointer: To avoid expensive copies
  • Document parameter requirements: Note valid ranges, special values, etc.

Common Mistakes

  • Forgetting to pass an array's size
  • Not checking if pointer parameters are NULL
  • Modifying caller variables unintentionally
  • Accessing out-of-bounds array elements
  • Using local pointers after the function returns

Summary

Function parameters in C work by value by default, with pointers providing a way to simulate pass by reference. Understanding how to properly use and design function parameters is crucial for writing clean, maintainable C code.



If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)