[PYTHON] return statement

return The role of return is to return "value = argument" to the caller of the function. The "return statement" returns the execution result of a normal function to the caller. When processing reaches the return statement, the function execution is complete and control returns to the caller. If you specify data as the return value of return, you can return the data to the caller.

Difference between return and print When used at a command prompt, etc., the result looks the same whether you use return or print, but it's a completely different thing. Since return has the advantage that the return value can be used in other functions, it is used in cases where the return value is used in other functions instead of using it for output.

return: Returns a value and terminates the function print: Print to the console

Pattern with no return value

Function definition def hello(): return

Function call hello()

Example def hello(name): print ( 'Hello', name) return //以下は実行されない print ('Good evening', name)

hello ('Hatamoto')

Pattern with a return value

Function definition def number(): return 0

Function call a= number()

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

c = number(1, 2) print(c)

def add_number(a, b): return (a + b, a - b)

d, e = add_number(1, 2) print(d, e)

break break is often used as a set with an if statement in the iterative processing of a for statement or while statement. Basic description method using break   while conditional expression: Branch the condition to interrupt processing if conditional expression: break Iterative processing executed by while statement

Difference between break and continue Often compared to break is continue. continue is also used in loop processing, but the major difference from break is that break exits the loop processing block, whereas continue does not exit the loop processing, but returns to the beginning of the loop processing to perform processing. It will be a continuous function.

Description method to exit the double loop Basic description method when exiting multiple loops such as double loops for variable in range (): for variable in range (): if Conditional expression to get out: break break

Return in loop return is used to return a value from a function. After returning the value with return, the processing of the subsequent functions ends, so if you use it in a loop, the entire function ends, so the loop processing ends.

Recommended Posts

return statement
while statement
Python if statement
Write statement buffering
[PyQt] Guidance statement
For Else statement
Python exec statement
[Python] if statement
Python assert statement