SoFunction
Updated on 2024-11-13

Return values and types of functions in python in detail

1. Introduction to return values

Real Life Scenario.

I give my son 10 dollars to buy me a pack of cigarettes. In this example, 10 dollars I gave my son, the equivalent of calling the function passed to the parameter, so that my son to buy cigarettes this thing is the ultimate goal is to let him bring back the cigarettes to you and then give you right,, at this time the cigarettes are the return value

Scenarios in Development:

Define a function that accomplishes getting the room temperature, and think about whether the result should be given to the caller, who only has this return value to be able to make appropriate adjustments to the current temperature

To summarize:

The so-called "return value" is the result that a function gives to the caller after it has done something in a program.

2. Functions with return values

To return the result to the caller in a function, you need to use return in the function

Example.

def add2num(a, b):
	c = a+b
    return c

or

def add2num(a, b):
    return a+b

3. Save the return value of the function

In the example of "buying cigarettes" at the beginning of this subsection, when your son gives you cigarettes at the end, you must have received them from your son, right? The same is true for programs. If a function returns a piece of data, and you want to use that piece of data, you need to save it!

An example of the return value of a save function is as follows.

#Define Functions
def add2num(a, b):
	return a+b
# Call the function and save the return value of the function in the meantime.
result = add2num(100,98)
#Because result already holds the return value of add2num, you can use it next
print (result)

Results.

198

4. Four types of functions

Functions can be combined with each other according to whether they have parameters or not, and whether they have a return value or not, and there are 4 types in total

  • No parameters, no return value
  • No parameters and backtracking
  • With parameters, no return value
  • with parameters and a return value

1. Functions with no parameters and no return value

This type of function, can not receive parameters, there is no return value, in general, the print cue light similar functions, the use of such functions

def printMenu():
    print('--------------------------')
    print(' xx Shabu Shabu Ordering System ')
    print('')
    print(' 1. Lamb shabu-shabu')
    print(' 2. Beef shabu-shabu ')
    print(' 3. Pork shabu-shabu ')
    print('--------------------------')

Results.

2. Functions with no parameters and a return value

This type of function, can not receive parameters, but can return a certain data, in general, like the collection of data, with this type of function

# Get the temperature
def getTemperature():
    # Here's some processing to get the temperature
    # For simplicity's sake, let's first simulate the return of a piece of data
	return 24
# I created a Python learning exchange group: 725638078
temperature = getTemperature()
print('The current temperature is :%d'%temperature)

Results.

Current temperature: 24

3. Functions with parameters and no return value

This type of function, can receive parameters, but can not return data, in general, for some variables to set the data without the results, use this type of function

4. Functions with parameters and return values

This type of function, not only can receive parameters, but also can return a certain data, in general, like data processing and need the results of the application, with this type of function

 # Calculate the cumulative sum of 1~num
def calculateNum(num):
    result = 0
    i = 1
    while i<=num:
        result = result + i
        i+=1
    return result
result = calculateNum(100)
print('The cumulative sum of 1~100 is :%d'%result)

Results.

Cumulative sum of 1 to 100: 5050

5. Summary

Functions can be combined with each other according to whether there are parameters and return values When defining functions, they are designed according to the actual functional requirements, so different developers write different types of functions.

5. Can we return multiple values in python?

>>> def divid(a, b):
...     shang = a//b
...     yushu = a%b 
...     return shang, yushu
...
>>> sh, yu = divid(5, 2)
>>> sh
5
>>> yu
1

Essentially the tuple is utilized

The above is the return value of the function in python and the type of detailed explanation of the details, more information about the return value of python function please pay attention to my other related articles!