print(f(4)) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

print(f(4))

def f(x): x=2 return(x-1) print(f(4)) Solution: 1 I get how return (2-1) is 1. But print (f(4))) should convert to (2(4)) so 8? Why was the print command ignored? Thanks for any help👍🏻

6th Jun 2019, 10:48 PM
tristach605
tristach605 - avatar
8 Answers
+ 1
Here "f" is a function, not a variable. It can't take any value. That said, "f(4)" returns 1, and so print(f(4)) prints such value.
6th Jun 2019, 11:16 PM
Diego
Diego - avatar
+ 1
parameter “x” in function “f” will ALWAYS be overwritten by local variable “x” which is 2. Thus, x - 1 is 1. When you pass a value into a function, the value cannot actually be the function.
7th Jun 2019, 1:59 AM
Choe
Choe - avatar
+ 1
Ah! Now I get it! Thanks for the examples Peter, Choe and Diego. You're all the best!👍🏻
11th Jun 2019, 10:44 PM
tristach605
tristach605 - avatar
0
Thanks, Choe and Diego. Why not just say "print (f)"? What purpose does the "4" in "print(f(4))" do?
7th Jun 2019, 2:23 AM
tristach605
tristach605 - avatar
0
It acts as the argument of the function. "print(f)" would print the value of variable "f". Take a look at this example. f = 42 def f(x): return x**2 print(f) # 42 print(f(3)) # 3**2 == 9 print(f(5)) # 5**2 == 25
7th Jun 2019, 2:26 AM
Diego
Diego - avatar
0
tristach605 thats the trick. In this case, it doesnt have any purpose 😃. However, since the function expects an argument, you need to specify or it will result in an error.
7th Jun 2019, 2:28 AM
Choe
Choe - avatar
0
Here, “x” is now optional. def f(x=2): return x - 1 print(f(5)) print(f()) 4 1
7th Jun 2019, 2:42 AM
Choe
Choe - avatar
0
x=2 always changes your parameter value to 2. print is not carried out because it is not part of f(x) function. to makr it part of the functiin you need to move it to the right with tab.
8th Jun 2019, 5:17 PM
Peter Pjecha
Peter Pjecha - avatar