How does the functions in python works when we call them that is please explain me the following working of generating outputs | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How does the functions in python works when we call them that is please explain me the following working of generating outputs

def add (x,y, z): Print (x+y+z) def prod (x,y,z): return x*y*z a=add (6,16,26) b=prod (2,3,6) print (a,b) Output: None 36 def change (p,q=30): p=p+q q=p-q print(p,"#",q) return(p) r=150 s=100 r=change(r,s) print(r,"#",s) s= change(s) Output: 150#50 150#100 100#70 I want to ask that in both above codes variables are are containing the function call but in 1st one the "add" function does not print any thing while function have print there while in 2nd the fiunction call is still assigned to a variable but still printing a value how does these two outputs are coming please explain Is this is because of return if yes then how

26th May 2020, 12:00 PM
Vijay Gunwant
Vijay Gunwant - avatar
3 Answers
+ 2
Sandra Meyer this is a question of my school book in the first case i thought of the same output as generated by your code but in book it is written like that
26th May 2020, 1:26 PM
Vijay Gunwant
Vijay Gunwant - avatar
+ 1
Though it is not explicitly defined in Python, each method has a return value. Returning nothing is the so called 'void'. Your method add does not return anything, it is just printing something. You in fact assign 'void' to 'a' - which results in "Undefined"-output. Your method prod has a return value, that's why b can be printed correctly. https://code.sololearn.com/cL00hyx59xat/?ref=app
26th May 2020, 1:21 PM
Sandra Meyer
Sandra Meyer - avatar
0
Take a look at the project I posted, it's explained there very clear and everything works as expected.
26th May 2020, 1:31 PM
Sandra Meyer
Sandra Meyer - avatar