Lecture-20 Parameter Return 24-25
COMP101 Introduction to Programming 2024-25 Lecture-20
Focus: Parameter Return
Calling Functions – No Value Returned
Function without Return
No arguments or parameters; solely operates on internal data.
Data within the function is local and cannot be accessed outside of it.
Function with Argument but No Return
A copy of the data is passed into the function.
Data remains local and restricted to the function's scope.
Value Not Returned to Caller
Example Function Definition
def num_length(number_str): length_of_num = len(number_str) print("Your number has", length_of_num, "digits in it") returnKey Points:
length_of_numis a local variable withinnum_length.number_stris also a local variable received as a parameter.Local variables are bound by the function's scope and cannot be accessed externally.
Using
returnwith no value effectively returnsNone(it’s good practice to use it).
Calling Functions - Value Returned
Function with Argument and Return:
When a function is called with an argument, the parameter becomes a local variable.
The function can modify and return the value of the local variable.
The
returnstatement is typically the last line within the function.
Value Returned to Caller
Function Call Steps:
Call the function using a variable as an argument.
The variable captures the returned value from the function.
Inside the function, the parameter is used for operations.
Use
return(parameter_name)to send the value back to the caller.
Example of Returning a Value
Function Definition:
def squared(value): value = value * value return valueIn Main Function:
def main(): num = 5 num_squared = squared(num) print(num_squared)Explanation of Execution:
num_squaredcaptures the result ofsquared.valueinsidesquaredis local to that function.The returned value from
squaredis captured and can be printed, resulting in 25.The original
numremains unaffected, demonstrating that a copy of the argument is passed.
Example of Average Calculation
Function Definition:
def myGrades(a, b, c, d): average = (a + b + c + d) / 4 return averageMain Function Calculation:
def main(): ca01, ca02, ca03, ca04 = 9, 20, 34, 45 finalScore = myGrades(ca01, ca02, ca03, ca04) print("Average of scores =", finalScore)Details:
Average calculated within
myGradesreturns tofinalScore.The original grades remain unchanged outside the function.
Passing Arguments - General
Call-by-Value:
A copy of the argument is supplied to the function.
Changes inside the function do not affect the original value.
Call-by-Reference (Path Types):
The function can access the original variable’s memory location.
Original values may be modified within the function, affecting them externally.
Python - How it Passes Arguments
Argument Passing in Python:
Python utilizes assignment for argument passing.
Data types determine whether Python uses call-by-value or call-by-reference methodology.
Immutable vs. Mutable Objects:
Immutable (numbers, strings, tuples): Passed by value.
Mutable (lists, dictionaries): Passed by reference.