+ 2

I need help in python !

I dont understand the return function, can you explain me what return means (in Python) ?

1st Jan 2018, 1:14 PM
andrebtw
andrebtw - avatar
6 odpowiedzi
+ 3
#Return is the result of a function. And is given back to the piece of code that called the function. This is an example, newyear() is the function: def newyear(): return 2018 #You call the function like this: x = newyear() print(x) # this will print 2018
1st Jan 2018, 1:25 PM
Paul
Paul - avatar
+ 3
it just returns a value/values of the function you call
1st Jan 2018, 1:28 PM
D_Stark
D_Stark - avatar
+ 2
Thanks a lot men ;)
1st Jan 2018, 1:28 PM
andrebtw
andrebtw - avatar
+ 2
(i dont actually know python but im gonna try to explain) for example when you define function (i guess function defines are like that) def function_name(parameter) //codes for example if you define function that is taking a parameter and returning the increased value, def increase(x) //x is parameter, increase is function name x += 2 //increase by two and equal it to x return x //then return x if you do this increase(x) is going to equal to x's increased value i mean when you are coding number = input("enter a number: ") //then print increase function print(increase(number)) for example if user enters 4 output is going to be 6
1st Jan 2018, 1:30 PM
Mustafa K.
Mustafa K. - avatar
+ 2
A function must have a Type and return value a return just return a value so you need to print it if you need it to show def max(x, y):<--start function if x >=y: return x else: return y <--end function print(max(4, 7)) <--- max is a function made up ^ so here 4 taken as x and 7 as y if x >=y: -> 4>=7 False so condtion not taken return x -> this return will be ignored else: -> As the first is false this will be true return y -> it reurn y=7 so max(4,7) have have result 7 and its just printed Note One return in a true condition return the value and the code not continue to execute
1st Jan 2018, 1:30 PM
Elie Douaihy
Elie Douaihy - avatar
+ 1
omg while i was typing this comment, he understood what is return 😃😃
1st Jan 2018, 1:31 PM
Mustafa K.
Mustafa K. - avatar