DevAcademia
C++C#CPythonJava
  • C Basics

  • Introduction to C
  • Getting Started with C
  • C Syntax
  • C Output
  • C Comments
  • C Variables
  • C Data Types
  • C Constants
  • C Operators
  • C Booleans
  • C If...Else Statements
  • C Switch Statement
  • C While Loops
  • C For Loops
  • C Break and Continue
  • C Strings
  • C User Input
  • C Memory Address
  • C Pointers
  • C Files
  • C Functions

  • C Functions
  • C Function Parameters
  • C Scope
  • C Function Declaration
  • C Recursion
  • C Math Functions
  • C Structures

  • C Structures
  • C Structs & Pointers
  • C Unions
  • C Enums

  • C Enums
  • C Memory

  • C Allocate Memory
  • C Access Memory
  • C Reallocate Memory
  • C Deallocate Memory
  • C Structs and Memory
  • C Memory Example
  • C Quiz

  • C Quiz
  • C Basics

  • Introduction to C
  • Getting Started with C
  • C Syntax
  • C Output
  • C Comments
  • C Variables
  • C Data Types
  • C Constants
  • C Operators
  • C Booleans
  • C If...Else Statements
  • C Switch Statement
  • C While Loops
  • C For Loops
  • C Break and Continue
  • C Strings
  • C User Input
  • C Memory Address
  • C Pointers
  • C Files
  • C Functions

  • C Functions
  • C Function Parameters
  • C Scope
  • C Function Declaration
  • C Recursion
  • C Math Functions
  • C Structures

  • C Structures
  • C Structs & Pointers
  • C Unions
  • C Enums

  • C Enums
  • C Memory

  • C Allocate Memory
  • C Access Memory
  • C Reallocate Memory
  • C Deallocate Memory
  • C Structs and Memory
  • C Memory Example
  • C Quiz

  • C Quiz

Loading C tutorial…

Loading content
C FunctionsTopic 49 of 64
←PreviousPrevNextNext→

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

ℹ️ Note: Parameters are what the function expects; arguments are what you actually pass to it.
TermDefinitionExample
ParameterVariable in function declaration/definitionvoid printSum(int a, int b)
ArgumentActual value passed to functionprintSum(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

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;
}
Output
Before swap: x = 5, y = 10
Inside function: a = 10, b = 5
After swap: x = 5, y = 10
ℹ️ Note: Pass by value doesn't affect original variables because functions work with copies.

Pass by Pointer (Emulating Pass-by-Reference)

⚠️ Warning: In C, arguments are still passed by value; you pass the value of the pointer. Ensure pointers are valid before dereferencing them.
Example
#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;
}
Output
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.

Example
#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;
}
Output
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
ℹ️ Note: Pointer size is platform-dependent (commonly 8 bytes on 64-bit, 4 bytes on 32-bit). Your output may differ.

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
Test your knowledge: C Function Parameters
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
C FunctionsTopic 49 of 64
←PreviousPrevNextNext→