C Function Parameters
1. What are Function Parameters?
-
Function parameters are values you pass to a function to use inside it.
-
Parameters allow a function to work on different data each time it’s called.
Syntax:
2. Types of Function Parameters
A) Call by Value (Default in C)
-
A copy of the value is passed to the function.
-
Changes inside the function do not affect the original variable.
Example:
Output:
B) Call by Reference (Using Pointers)
-
Address of variable is passed to the function.
-
Changes inside the function affect the original variable.
Example:
Output:
Use
*to access the value at the address (dereference).
3. Multiple Parameters
Output:
-
Functions can have any number of parameters.
4. Default Values
-
C does not support default parameter values (unlike C++).
-
You must pass all required arguments.
5. Key Points About Function Parameters
-
Parameters allow functions to be flexible and reusable.
-
Call by Value: passes a copy; original variable is unchanged.
-
Call by Reference: passes the address; function can modify the original.
-
Use pointers when you want a function to modify variables outside its scope.
-
Functions can take multiple parameters of any type (int, float, char, arrays, pointers).
