[Python] Return Statement

Python function is just collection of python codes that perform specific tasks. So we just call python function when we want perform that tasks; However, sometimes when we call the function we're actually  going to get the information back from that function. 

"Return" keyword, which allows python to return the information from the function. 


Let's make this function;  you can actually make your own function with "def" 

def cube(num):
    num*num*num

and run.. 

print(cube(3))

but we got "none" 


We expected it would cube the numbers .. but NOT !



Now, Let's do this

def cube(num):
    return(num*num*num)

Python is going to return whatever value we put inside. so when I call the cube function, and give it "3" python is going to execute the function and return the values. 

print(cube(3))
27

we got the right number !



one more ! you can do like this too 

def cube(num):
    return(num*num*num)


result = cube(5)
print(result)
125



Just for more information! 

It's not able to put something after return statement;  

def cube(num):
    return(num*num*num)
    print("coooo")


result = cube(5)
print(result)

so never reach to the code after return statement. 


Popular posts from this blog

[Python] Dictionary

[Visual Design 2/3]

[JavaScript] For loop , Function