0

Why is the output 5?

Def f(n=9) Print (n) F(5) What happens to the n=9

16th Oct 2020, 6:20 PM
Curious Ant
Curious Ant - avatar
3 Respostas
+ 4
It simply means that if the function is called without any arguments then the default value of 'n' will be 9. If the argument is passed then the value of 'n' will be replaced by that argument. Example: def f(n=9): print(n) f(5)#prints 5 f()#prints 9
16th Oct 2020, 6:27 PM
Avinesh
Avinesh - avatar
+ 3
n is assigned new value i.e. 5 when you use a parameter in function definition any argument given to it is assigned to that parameter
16th Oct 2020, 6:22 PM
Abhay
Abhay - avatar
+ 3
9 is the default value for n within your function (f). In short your function will print the first argument passed to it at runtime (n); however if no argument is passed at runtime n will be given the default value of 9 - which will then be printed. Since you pass 5 to f at runtime, n=5 which is then printed using the call print(n). If you want to print 9 to the console using your function simply run it with no parameters - i.e f() - and the default behaviour will result in n=9 which will then be printed with the call print(n)
16th Oct 2020, 6:26 PM
James Pink
James Pink - avatar