Question

In: Computer Science

Im a but confused as to what the difference between print and return in terms of...

Im a but confused as to what the difference between print and return in terms of functions.
I wrote the function:

def myfun (a,b):
return a**b
myfun(5,2)

but nothing gets printed out and i dont understand because i have done this before in other functions and it works.

Solutions

Expert Solution

Answer:

print, as the name suggests will print the answer/output on the terminal screen or it would be used to check some values of various parameters throughout the program

whereas, return function is used to come out of a function by returning a value from where it was called.Every function will have a return value. If there is no return function then it will return none.

For e.g., in the above program,

def myfun(a,b):

     return a*b

myfun(5,2)

This program will not print anything because you are returning the result of the 2 numbers back to the called function directly,i.e., you are not storing the returned value in a variable.When returning a certain variable from a function, we need to have a place to store the returned variable.

So, you could write the third line as v=myfun(5,2).

This will store the returned value in v and then you can print v.

Otherwise you can also print value directly by calling it inside the print function like this:

print(myfun(5,2))


Related Solutions

Please explain, Im so confused! A. What is the vapor pressure in atm of a solution...
Please explain, Im so confused! A. What is the vapor pressure in atm of a solution at 25 oC produced by dissolving 138.4 g of dextrose (C6H12O6) in 376.4 g of water? Vapor pressure of water at 25 oC = 0.0313 atm B. An 18.3 g of an unknown non-ionizing solute is added to 88.8 g of water. If the boiling point of the solution is 101.4, what is the molar mass of the solute? C. How many moles of...
What is the difference between the expected rate of return and the required rate of return?...
What is the difference between the expected rate of return and the required rate of return? What does it mean if they are different for a particular asset at a particular point in time?
What is the difference between the expected rate of return and the required rate of return?...
What is the difference between the expected rate of return and the required rate of return? What does it mean if they are different for a particular asset at a particular point in time? please a new and different answer. Thank you
What is the difference between virus, worms and trojan in terms of?
What is the difference between virus, worms and trojan in terms of?What does it do?How does it spread to another computer?Does it infect the file?Does there need to be user action for it to be spread?It can be attached to.
what is the difference between a triglyceride and a lipid? why are they not equivalent terms?
what is the difference between a triglyceride and a lipid? why are they not equivalent terms?
BALANCING: Na2SO3 (aq) + H2SO4(aq) PLEASE explain your answer. im fine w balancing, im just confused...
BALANCING: Na2SO3 (aq) + H2SO4(aq) PLEASE explain your answer. im fine w balancing, im just confused with trying to find the reaction formula. i know the answer, i need to know HOW you got it (ive looked everywhere and cant find an explanation for why you get the H20 and the SO2 in the answer)
What is the difference between a monopoly industry and a competitive petroleum industry in terms of...
What is the difference between a monopoly industry and a competitive petroleum industry in terms of output and price in a static and dynamic sense?
What is the difference between a bitmap image and a vector image? In terms of processing...
What is the difference between a bitmap image and a vector image? In terms of processing speed, which is going to be a better image format? When would the other format be better to use? Why should you use the PNG format instead of GIF? What are HEIC and HEVC formats and what are the advantages and disadvantages of them? What does it mean for a compressed image to have artifacts? How do you ensure your photos and images never...
Explain the major difference between a between-subjects ANOVA and repeated-measures ANOVA in terms of what is...
Explain the major difference between a between-subjects ANOVA and repeated-measures ANOVA in terms of what is being partioned.
Explain the difference, both in terms of structure and in terms of properties, between a saturated...
Explain the difference, both in terms of structure and in terms of properties, between a saturated fat and an unsaturated fat.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT