New course! Every coder should learn Generative AI!
Try a free lesson0
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👍🏻
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.
+ 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.
+ 1
Ah! Now I get it! Thanks for the examples Peter, Choe and Diego. You're all the best!👍🏻
0
Thanks, Choe and Diego.
Why not just say "print (f)"? What purpose does the "4" in "print(f(4))" do?
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
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.
0
Here, “x” is now optional.
def f(x=2):
return x - 1
print(f(5))
print(f())
4
1
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.