C Function Parameters


In C programming, function parameters allow you to pass data from one function to another. Parameters make functions dynamic, enabling them to operate on different values without rewriting the code. They are essential for creating flexible, reusable functions and for implementing structured programming effectively.

Without parameters, functions would only work with fixed values or global variables, limiting their usefulness. By using parameters, a function can receive input, perform operations, and return results efficiently.

What are Function Parameters?

Function parameters, sometimes called formal parameters, are variables listed in the function definition. They receive values from the calling function, known as arguments.

Syntax:

return_type function_name(data_type parameter1, data_type parameter2, ...) {
    // function body
}

Example:

#include <stdio.h>

int add(int a, int b) {
    return a + b;
}

int main() {
    int sum = add(5, 10); // 5 and 10 are arguments
    printf("Sum: %d\n", sum);
    return 0;
}
  • a and b are parameters.

  • 5 and 10 are arguments passed from main.

  • The function uses the parameters to compute the sum and return it.

Types of Function Parameters

C supports several types of function parameters:

1. Value Parameters

  • A copy of the actual value is passed to the function.

  • Modifying the parameter inside the function does not affect the original variable.

#include <stdio.h>

void increment(int n) {
    n = n + 1;
    printf("Inside function: %d\n", n);
}

int main() {
    int num = 5;
    increment(num);
    printf("Outside function: %d\n", num);
    return 0;
}
  • Output shows that the original num remains 5 outside the function.

  • This is called pass-by-value.

2. Reference Parameters (Using Pointers)

  • Using pointers, you can pass the address of a variable.

  • Changes inside the function affect the original variable.

#include <stdio.h>

void increment(int *n) {
    *n = *n + 1;
}

int main() {
    int num = 5;
    increment(&num);
    printf("Value after increment: %d\n", num);
    return 0;
}
  • Here, *n modifies the original num.

  • This is called pass-by-reference.

3. Multiple Parameters

Functions can have more than one parameter, separated by commas:

#include <stdio.h>

int multiply(int a, int b) {
    return a * b;
}

int main() {
    int result = multiply(4, 5);
    printf("Result: %d\n", result);
    return 0;
}
  • Each parameter receives a corresponding argument in the same order.

  • Order and data type must match.

Default and Optional Parameters in C

Unlike some languages, C does not support default or optional parameters. You must provide exactly the number of arguments that match the function’s parameters. Using pointers or function overloading (in C++) can simulate optional behavior, but in pure C, each parameter must be passed explicitly.

Array Parameters

Arrays can be passed to functions using pointers or by specifying the array name:

#include <stdio.h>

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

int main() {
    int numbers[] = {10, 20, 30};
    printArray(numbers, 3);
    return 0;
}
  • The array name numbers acts as a pointer to the first element.

  • Changes made inside the function affect the original array.

String Parameters

Strings in C are character arrays, and when passed to a function, they behave like arrays:

#include <stdio.h>

void greet(char name[]) {
    printf("Hello, %s!\n", name);
}

int main() {
    char username[] = "Alice";
    greet(username);
    return 0;
}
  • The function can access and modify the characters in the string.

  • Strings are inherently passed by reference in C.

Benefits of Using Function Parameters

  1. Reusability: Functions can operate on different data without rewriting code.

  2. Modularity: Makes programs easier to maintain and understand.

  3. Flexibility: Functions can process dynamic data rather than hardcoded values.

  4. Efficiency: Reduces redundancy and promotes structured programming.

Practical Example: Calculator with Parameters

#include <stdio.h>

int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }
int multiply(int a, int b) { return a * b; }
float divide(int a, int b) { return (float)a / b; }

int main() {
    int x = 12, y = 4;
    printf("Add: %d\n", add(x, y));
    printf("Subtract: %d\n", subtract(x, y));
    printf("Multiply: %d\n", multiply(x, y));
    printf("Divide: %.2f\n", divide(x, y));
    return 0;
}
  • Each function receives parameters and returns a result.

  • Demonstrates clean, reusable, and modular code.

Common Mistakes with Function Parameters

  1. Forgetting to pass arguments in the correct order.

  2. Mismatching parameter types and argument types.

  3. Modifying value parameters expecting the original variable to change.

  4. Passing arrays without specifying size, which may cause unexpected behavior.

  5. Using uninitialized pointers for pass-by-reference, causing runtime errors.

Summary of the Tutorial

Function parameters in C allow programs to receive input, process it, and produce results efficiently. Understanding pass-by-value and pass-by-reference is key to controlling whether a function can modify the original data. Proper use of parameters makes functions flexible, reusable, and modular, forming the backbone of structured programming in C.


Practice Questions

  1. Write a function to swap two numbers using pass-by-reference (pointers).

  2. Create a function that calculates the square of an integer passed as a parameter.

  3. Write a function that finds the largest of three numbers using parameters.

  4. Develop a function that adds two numbers and returns the result.

  5. Create a function that prints a string passed as a parameter.

  6. Write a function that calculates the factorial of a number using recursion with a parameter.

  7. Develop a function to find the sum of an integer array passed as a parameter.

  8. Write a function to reverse a string passed as a parameter.

  9. Create a function that checks if a number is prime using a parameter.

  10. Write a function that prints the elements of a float array passed as a parameter.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top