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")  
        return  
  • Key Points:

    • length_of_num is a local variable within num_length.

    • number_str is also a local variable received as a parameter.

    • Local variables are bound by the function's scope and cannot be accessed externally.

    • Using return with no value effectively returns None (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 return statement is typically the last line within the function.

Value Returned to Caller

  • Function Call Steps:

    1. Call the function using a variable as an argument.

    2. The variable captures the returned value from the function.

    3. Inside the function, the parameter is used for operations.

    4. 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 value  
  • In Main Function:

    def main():  
        num = 5  
        num_squared = squared(num)  
        print(num_squared)  
  • Explanation of Execution:

    • num_squared captures the result of squared.

    • value inside squared is local to that function.

    • The returned value from squared is captured and can be printed, resulting in 25.

    • The original num remains 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 average  
  • Main 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 myGrades returns to finalScore.

    • 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.