0% found this document useful (0 votes)
6 views5 pages

Pass Arguments in C

Uploaded by

andrew.ng2210
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Pass Arguments in C

Uploaded by

andrew.ng2210
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Passing Arguments in C

Understanding Pass-By-Value and


Pass-By-Reference
Ways to Pass Arguments in C
• 1. Pass-by-Value
• - Default method for most data types (except
arrays).
• - A copy of the variable is passed to the
function.
• - Changes inside the function do not affect
the caller's variable.

• 2. Pass-by-Reference
Why Use Pass-by-Reference?
• 1. Modify Variables in the Caller:
• - Allows functions to change values in the
caller directly.
• - Useful for operations like swapping values.

• 2. Avoid Overhead of Copying Large Data:


• - Reduces memory usage and improves
efficiency.
• - Especially important for large arrays or
How Pass-by-Reference Works
• 1. Use the Address-of Operator (&):
• - Pass the address of a variable to the
function.
• - Example: `swap(&a, &b);`

• 2. Use the Indirection Operator (*):


• - Dereference the pointer to access or
modify the value.
• - Example: `*ptr = 10;`
Summary
• - Pass-by-Value creates a copy of the variable,
and changes do not affect the caller.
• - Pass-by-Reference uses pointers to directly
modify the caller's variable.
• - Useful for efficiency, modifying variables, and
returning multiple values.
• - Use `&` to pass addresses and `*` to modify
values.

You might also like