Exercise 21 -- functions can return something

A function return value

Definition: the so-called "return value" refers to the result given to the caller after a function in the program completes an event

Syntax: use the return statement to end the function call and return the operation result

  • When the return statement in the function body has a specified return value, it returns its value
  • When there is no return statement in the function body, the function will implicitly return a None as the return value at the end of the operation. The type is NoneType, which is equivalent to return and return None. Both return None.
  • A function can have multiple return statements, but only one can be executed. If no reutrn statement is executed, return None will also be implicitly called as the return value
  • If the function executes a return statement, the function will immediately return and end the call. Other statements after return will not be executed

3 return value type

  • No matter what type is defined to be returned, return can only return a single value, but the value can have multiple elements.
  • return [1,3,5] means to return a list, which is a list object. 1,3,5 are the elements of the list respectively
  • return 1,3,5 it seems to return multiple values, which are implicitly encapsulated into a tuple by Python

4 function classification

  • No parameter, no return value: this type of function cannot receive parameters and no return value. Generally, this type of function is used for functions similar to the print prompt lamp
  • No parameter, return value: this type of function can not receive parameters, but can return some data. In general, such functions are used to collect data
  • With parameters but no return value: this type of function can receive parameters but not return data. In general, this type of function is used when setting data for some variables without results
  • With parameters and return values: this type of function can not only receive parameters, but also return some data. In general, for applications such as data processing and requiring results, this type of function is used

II. Code

1 ex21.py

def add(a,b):                       # Define a function add,Used to calculate the sum of two numbers
    print(f"ADDING {a} + {b}")      # Print out the operation of the program
    return a + b                    # return a+b Value of

def subtract(a,b):                  # Define a function add,Used to calculate the difference between two numbers
    print(f"SUBTRACTING {a} - {b}") # Print out the operation of the program
    return a - b                    # return a-b Value of

def multiply(a,b):                  # Define a function add,Used to calculate the product of two numbers
    print(f"MUTLIPLING {a} * {b}")   # Print out the operation of the program
    return a * b                    # return a*b Value of

def divide(a,b):                    # Define a function add,Used to calculate the quotient of two numbers
    print(f"DIVIDING {a} / {b}")     # Print out the operation of the program
    return a / b                    # return a/b Value of

print("Let's do some math with just functions") # Print a string to tell the user what they are doing now

age = add(30,5)                     # call add Function and assign the return value of the function to the variable age
height = subtract(78,4)             # call subtract Function and assign the return value of the function to the variable height
weight = multiply(90,2)             # call multiply Function and assign the return value of the function to the variable weight
iq = divide(100,2)                  # call devide Function and assign the return value of the function to the variable iq

print(f"Age:{age},Height:{height},Weight:{weight},IQ:{iq}\n")   # Print calculation results

# A puzzle for the extra credit,type it in anyway.
print("Here is a puzzle.")         # Print a string to remind users of what they are doing now

# 1 call divide Function calculation iq Quotient of and 2
# 2 hold divide The return value of the function as multiply Function, get the product of their two, and return
# 3 hold multiply The return value of the function is passed as a parameter to subtract Functions, calculating height And its difference, and return  
# 4 hold subtract The return value of the function is passed as a parameter to add Functions, calculating aga And its sum, and assign the function return value 1 to the variable what
what = add(age,subtract(height,multiply(weight,divide(iq,2))))

# Print the final calculation results
print("That becomes:",what,"Can you do it by my hand?\n")

another = subtract(height,add(iq,divide(age,multiply(weight,3))))
print(f"{another}\n")

exercise = add(24,subtract(divide(34,100),1023))
print(f"{exercise}\n")

Procedure explanation:

(1) Take the add function as an example:

  • 1. our function is called with two parameters: a and b.
  • 2. we print out what the function does. In this case, it is "ADDING".
  • 3. then we let Python do something in reverse: we return the sum of a + b. You can describe it this way: I add b to a and return their results.
  • 4. Python adds these two numbers. Then, when the function terminates, any line running the function can assign the result of a + b to a variable.

(2) Why does python print formulas or functions "backward"?

  • It is not from the back to the front, it is from the inside out. When you start messing up your code into separate formulas and functions, you'll see how it works. Try to understand that I said "inside out" instead of "backward".

2 operation results

PS E:\3_work\4_python\2_code_python\02_LearnPythonTheHardWay> python ex21.py
Let's do some math with just functions
ADDING 30 + 5
SUBTRACTING 78 - 4
MUTLIPLING 90 * 2
DIVIDING 100 / 2
Age:35,Height:74,Weight:180,IQ:50.0

Here is a puzzle.
DIVIDING 50.0 / 2
MUTLIPLING 180 * 25.0
SUBTRACTING 74 - 4500.0
ADDING 35 + -4426.0
That becomes: -4391.0 Can you do it by my hand?

MUTLIPLING 180 * 3
DIVIDING 35 / 540
ADDING 50.0 + 0.06481481481481481
SUBTRACTING 74 - 50.06481481481482
23.935185185185183

DIVIDING 34 / 100
SUBTRACTING 0.34 - 1023
ADDING 24 + -1022.66
-998.66

Posted by aneuryzma on Mon, 30 May 2022 15:53:32 +0530