Course Title: Programming in C
Title of Topic: Passing Parameter
Parameter Passing
• Parameter passing refers to the method of supplying inputs
(arguments) to functions. In C programming, the two main
ways to pass parameters are:
• Pass by Value
• Pass by Reference
Pass by Value – Concept
• In Pass by Value, a copy of the actual variable is passed to the
function.
Changes made to the parameter inside the function do not
affect the original variable.
This is the default behavior in C.
Pass by Value – Example
• void update(int a) {
• a = a + 10;
• }
• int main() {
• int x = 5;
• update(x);
• printf("%d", x); // Output: 5
• }
Slide 4: Pass by Value – Pros
and Cons
• Advantages:
• Original data is protected from changes.
• Easy to read and understand.
• Disadvantages:
• Inefficient for large data (creates copies).
• Cannot return multiple results.
Pass by Reference – Concept
• In Pass by Reference, the memory address of the variable is
passed to the function using pointers.
This allows the function to modify the original variable
directly.
Pass by Reference – Example
• void update(int *a) {
• *a = *a + 10;
• }
• int main() {
• int x = 5;
• update(&x);
• printf("%d", x); // Output: 15
• }
Pass by Reference – Pros and Cons
• Advantages:
• Can modify the original data.
• More efficient for large structures (no need to create copies).
• Disadvantages:
• Syntax is more complex.
• Risk of unintended side effects if not handled carefully.
When to Use Which?
• Use Pass by Value when:
• You don’t want to modify the original data.
• Working with simple data types.
• Use Pass by Reference when:
• You want to modify data inside the function.
• Working with arrays, structures, or large data that would be
inefficient to copy.