C Function Parameters and Arguments

A function in C encapsulates a set of instructions that perform a specific task. Parameters and arguments allow you to pass data into these functions, making your code modular and reusable. In this tutorial, you’ll learn what parameters and arguments are and see several examples to understand how they work.


Understanding Parameters vs. Arguments

Parameters are variables listed in the function’s definition that act as placeholders for the values the function will receive. Arguments are the actual values you pass to the function when calling it.

Example 1: Simple Function with Parameters and Arguments

This example demonstrates a function that adds two numbers. The parameters int a and int b in the function definition serve as placeholders, and the arguments 10 and 20 are passed when the function is called.

main.c

</>
Copy
#include <stdio.h>

// Function definition with parameters a and b
int add(int a, int b) {
    return a + b;
}

int main() {
    // Calling the function with arguments 10 and 20
    int result = add(10, 20);
    printf("Sum: %d\n", result);
    return 0;
}

Explanation:

  1. The add function is defined with two parameters, int a and int b, which will hold the values passed to the function.
  2. When the function is called in main() with the arguments 10 and 20, these values replace the parameters a and b respectively.
  3. The function returns the sum of the two numbers, which is stored in the variable result.
  4. printf() is used to display the result.

Output:

Sum: 30

Example 2: Demonstrating Pass by Value

This example illustrates that in C, function parameters are passed by value. Changes made to the parameter inside the function do not affect the original argument.

main.c

</>
Copy
#include <stdio.h>

// Function that attempts to double the value
void doubleValue(int num) {
    num = num * 2;
    printf("Inside function: %d\n", num);
}

int main() {
    int value = 15;
    printf("Before function call: %d\n", value);
    doubleValue(value);
    printf("After function call: %d\n", value);
    return 0;
}

Explanation:

  1. The doubleValue function takes one parameter int num and attempts to double its value.
  2. Inside doubleValue, the parameter num is modified, but this change is local to the function.
  3. In main(), the original variable value remains unchanged after the function call, demonstrating that C uses pass by value.

Output:

Before function call: 15
Inside function: 30
After function call: 15

Example 3: Function with Multiple Parameters

This example shows a function that takes multiple parameters of different types to display a formatted message.

main.c

</>
Copy
#include <stdio.h>

// Function that prints a formatted greeting message
void greet(const char name[], int age) {
    printf("Hello %s, you are %d years old.\n", name, age);
}

int main() {
    // Calling the greet function with a string and an integer argument
    greet("Arjun", 28);
    return 0;
}

Explanation:

  1. The greet function is defined with two parameters: const char name[] (a string) and int age (an integer).
  2. The const keyword ensures that the string name is not modified within the function.
  3. In main(), the function is called with the arguments "Arjun" and 28, which are passed to the parameters name and age respectively.
  4. The printf() function inside greet displays the formatted greeting message.

Output:

Hello Arjun, you are 28 years old.

Conclusion

In this tutorial, we explored the concepts of function parameters and arguments in C. We learned that:

  1. Parameters are variables defined in a function’s signature that receive values.
  2. Arguments are the actual values passed to the function when it is called.
  3. Changes to parameters inside a function do not affect the original arguments (pass by value).
  4. You can define functions with multiple parameters to handle various data types and tasks.