BCA / B.Tech 9 min read

Parameter & Argument Passing in C

Parameter and Argument Passing in C:


In the C language, parameter and argument passing is an important process used to pass data during a function call. A function is a part of a program that can be used repeatedly. Parameter and argument passing provides us with the facility to give input data to a function and receive output.

Parameter and argument passing in the C language is a significant concept that allows us to pass and retrieve data through functions. Call by value is safe but uses more memory, while call by reference saves memory but allows data to be changed. The correct use of both techniques depends on the program's requirements and functionality.

What are Parameters and Arguments?

Parameter: Parameters are the variables that are declared in the function's definition. They are also known as "formal parameters."
Argument: Arguments are the values that are passed when calling the function. They are also known as "actual parameters."

Types of Parameter Passing

There are two main types of parameter passing in the C language: Call by Value and Call by Reference.

1. Call by Value

In call by value, when an argument is passed to a function, a copy of that argument is created and passed to the function. This means that if the value of that argument is changed inside the function, it does not affect the original value. The original data remains safe.

Working Principle: In the provided example, the value of `a` is 10, and when we pass it to the `changeValue()` function, a copy of it is made. Even if the value of that copy is changed inside the function, the original value of `a` does not change.

Advantages of Call by Value:
  • Safety: The original data remains safe, and its value does not change inside the function.
  • Simplicity: It is easy to understand and implement.

Disadvantages of Call by Value:
  • Extra Memory: A new copy of the argument is created each time, which uses extra memory.
  • Inability to Change Data: The original data cannot be changed from within the function.

2. Call by Reference

In call by reference, when an argument is passed, the reference (address) of that argument is passed to the function. This means that the original data can be changed using that reference inside the function. No copy of the argument is made; instead, the address of the original data is passed.

Working Principle: In the example, when we pass the address of `a` to the `changeValue()` function, the value of `a` is directly changed using that address within the function.

Advantages of Call by Reference:
  • Memory Saving: No copy is made, so memory is saved.
  • Ability to Change Data: The original data can be changed from within the function.

Disadvantages of Call by Reference:
  • Lack of Data Security: The original data can be changed, which increases the possibility of errors.
  • Difficulty in Understanding: The process of call by reference can be difficult for new programmers to understand.