Lecture 15 Functions Part2 Overloading
Lecture 15 Functions Part2 Overloading
• Pass by value means that a copy of the actual data is passed to the function.
Changes made to the parameter inside the function do not affect the original data.
• Languages: Common in languages like Python (for immutable data types like
integers and strings), C, and Java.
• Behavior: The function works on a copy of the data. If you modify the parameter
inside the function, the original data remains unchanged.
• Use case: This method is generally used when you want to ensure that the original
data should not be altered by the function.
Example
Pass by Reference
• Pass by reference means that instead of passing a copy of the data, a reference to
the original data is passed to the function. This means that any changes made to the
parameter inside the function affect the original data.
• Languages: Common in C++, JavaScript for objects, and Python for mutable types
like lists and dictionaries.
• Behavior: The function can modify the actual data.
• Use case: This is used when you want the function to modify the original data or
when passing a copy of the data would be too memory-intensive (e.g., large
structures or classes).
Example
Common Differences
• 1. Data Modification
• Pass by Value: Changes made to the parameter inside the function do not
affect the original variable. The function operates on a copy of the data.
• Pass by Reference: Changes made to the parameter inside the function
directly modify the original variable. The function operates on the actual
data.
Common Differences
• 2. Memory Usage
• Pass by Value: Each function call copies the data into the function’s
parameter, which increases memory usage, especially with large data
structures.
• Pass by Reference: No new copies of the data are created; only a
reference or pointer to the original data is passed. This is more memory-
efficient.
Common Differences
• 3. Performance
• Pass by Value: Copying data can be computationally expensive, leading
to slower performance when dealing with large data or in recursive calls.
• Pass by Reference: Since there's no data copying, it generally provides
better performance, particularly noticeable with large or complex data
structures.
Common Differences
• 4. Safety and Predictability
• Pass by Value: Safer in terms of not accidentally modifying the original
data. The function's operations do not have side effects on the data outside
of its scope.
• Pass by Reference: Can lead to unintended modifications to the original
data if not carefully managed. It requires careful handling to avoid bugs
related to side effects.
Common Differences
• 5. Default Behavior in Languages
• Pass by Value: The default method in many traditional languages for
basic data types (e.g., C for primitive types, Java for primitive types).
• Pass by Reference: The default in some modern languages for complex
data types (e.g., Python for mutable objects like lists and dictionaries, C++
when using references or pointers).
Function Overloading
• Function overloading is a feature in programming languages that allows
multiple functions to have the same name but different parameters.
• This feature enables you to create several functions that do similar jobs
but handle different types or numbers of arguments.
• Function overloading is part of the polymorphism feature in object-
oriented programming.
Key Principles