+ 1

Return while defining functions (Python)

Hi, can someone explain me what does Return mean in Python?

13th Jan 2020, 1:50 PM
Prime
Prime - avatar
3 odpowiedzi
+ 6
A function is like an operator. 3 + 5 returns the value 8 That 8 will disappear after you use the operator unless you do something with it, e.g. print it, like print(3 + 5) # prints 8 This is the same as def add_2_nums(a, b): return(a + b) If you call this function without doing anything with the returned value, it disappears, but you see it if, e.g., you print it. print(add_2_nums(3, 5)) # prints 8 Functions are just a useful way to make a combination of operations if you are going to use them more than once (they help to keep your code DRY - Don't Repeat Yourself). They can include other functions you have defined, e.g. def average_2_nums(a, b): return sum_2_nums(a, b) / 2 print(average_2_nums(3, 5)) # prints 4.0 I.e you don't have to print every returned value - you can use it for something else. In real life you would just define the last function as follows, because it uses less code, but you get the idea: def average_2_nums(a, b): return (a + b) / 2
14th Jan 2020, 4:08 AM
David Ashton
David Ashton - avatar
0
return-this element of function(def),if function has only one line,and this line return,function return the text or line you write and stop cycle
13th Jan 2020, 8:39 PM
Hacker Lacker
Hacker Lacker - avatar
0
help mr
25th Jan 2020, 4:30 PM
Alex Mahmud Riaz