such as the Learn Python
def Add (a,b): Print %d %d "% (A, b ) returna+b
Reply content:
Please look at @ Zhang Xiaofeng's answer first. If the answer is not understood--
The first thing to understand is, what is a function?
The book may say that the function is the module of completion function and so on. In fact, the function is a worker you recruit.
You give him some material, tell him how to use these materials to assemble, then he is responsible for the assembled finished products to you. The material is the function parameter, the finished product is the function output, and how to assemble is the function body code which you write.
For example, this piece of code
defworker(a,b,c): x=a+b y=x*c
The return statement returns the result to the place where it was called and returns the control of the program together.
For example, if there's a sentence somewhere else,
num = add(a, b)
It seems to be invited. Occupy a hole first. Start writing the answer, and then write it up and stick it. The 1 likes! To fix it!
Inadvertently see this good 2 question. Write my superficial opinion.
1.
functionfunction is processing (process)
Parameters(argument), thus obtaining a
return value(return)
1.1, the previous point can also be so understood that the function is a process, the parameter is the raw material, the return value is the finished product.
answer the question:
"What do you do with return a + b here?" ”
Obviously performed the a+b operation, and returned the a+b to this function called Add, so that a+b becomes the result of the Add function.
"What does return mean, and how is it used?" ”
Several elements of a function include: function name, parameter, process, return result, where the process is unnecessary, but the other three are certain (but return does not have to be written, the function can be defined without a return, the function will return none by default), The example that corresponds to you is the function name add, the parameters A and B, and the return result a+b. return meaning you refer to the lawsuit: "I just want a statement (result)" or reference function to understand the function.
As for how to use, (take your add function as an example) a simple summary has the following points:
Normal version: Return a+b
Hand remnant version: Returna+b #报错NameError
Mentally handicapped version: return a #只返回a
Mentally handicapped version 2:return c+d #报错NameError
Wayward version: return 2 #就是这么2
Headstrong version 2:return A-b #你开心就好:)
Empty version: Return #返回一个Noneprint只是单纯的将结构输出至你的显示设备, no other meaning.
Give a return of chestnuts:
def autoparts ():
parts_dict={}
List_of_parts = open (' List_of_parts.txt ', ' R ')
For line in List_of_parts:
K, V = line.split ()
Parts_dict[k] = V
return parts_dict
Why return? If you do not return, then the dictionary you created is dead, and the result is no longer accessible after function is run, because you simply print the results to the output device. If you return this result, then the result will be retained, so you can do something else on the basis of this result, such as:
My_auto_parts=autoparts ()
Print my_auto_parts[' engine ']
When we call autoparts this function again, it will return the value to us, we can store this return value in the variable my_auto_parts, we can after autoparts this function run, Still by my_auto_parts this variable to get the contents of the dictionary, finally we can output the dictionary keyword "engine" target. First, you're defining a function that you want to invoke when you use it.
Sum=add (3,4)
Output
Sum=7
It is visible that return returns the result of the subsequent expression to the sum
The point is to understand that defining a function and then using a function is two steps. Aha, ha! I am in the answer to the first professional question!
Return your def This function in Chinese "return" two words to explain the concept, all TM is talking nonsense, people ask how to understand, you have to translate the English meaning it?