0% found this document useful (0 votes)
8 views

Lecture 15 Functions Part2 Overloading

Uploaded by

sana zainab awan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lecture 15 Functions Part2 Overloading

Uploaded by

sana zainab awan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Lecture 15

Passing arguments by value & reference, Function


Overloading
Introduction
• In programming, the terms "pass by value" and "pass by reference" refer
to different ways that arguments can be passed to functions.
• These mechanisms affect how changes to the variables in the function
affect their original counterparts.
Pass by Value

• 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

• Different Parameter Lists: Overloaded functions must differ in the number or


type of parameters. They can have different data types, different numbers of
parameters, or both.
• Return Type: The return type of the functions can be the same or different, but
functions cannot be overloaded solely on the basis of different return types. The
function signature (name and parameters) must differ.
• Scope: Function overloading is resolved at compile time (static binding), which
means the compiler determines the appropriate function to use based on the
function signature.
Static Binding
• Static binding, also known as early binding, refers to the process where
the function to be called is determined at compile time, rather than at
runtime.
• This is common in scenarios like function overloading, where the decision
about which function variant to execute is made based on the arguments
provided when the code is compiled.
Static Binding
• 1. Compile-Time Decision
• Static binding involves linking a function call to a specific function during the
compilation of the program. This means the compiler knows exactly which
function to call before the program runs.
• 2. Efficiency
• Because the decisions about which functions to call are made during compilation,
the program can run faster at runtime. There's no need to figure out the details of
the function call while the program is executing, which speeds up the execution.
Static Binding
• 3. Uses
• Static binding is typically used for functions that are not expected to
change once compiled, like overloaded functions and functions with non-
virtual status in classes.
• 4. Limitations
• Static binding does not offer flexibility if you need to decide function calls
based on conditions only known during program execution. In such cases,
dynamic binding is used.

You might also like