0

Why is x =5 and not x =4?

def demo x=4, y=2 puts x**y end demo 5 Above demo is defined as x = 4 and y =2 why does the result is 25 (5*5) and not 16 (4*4)?

17th May 2017, 12:31 PM
Cristina Costa
2 Respostas
+ 8
The definition of the function assumes that the function takes two arguments, with their default values at 4 and 2, respectively. So if you just run the function with no arguments specified, it will take 4 and 2. Meanwhile, you call the function and give it only one parameter. So it assumes the second one at its default value. And so you have x=5 (specified by user), y=2 (default). 5**2 = 25
17th May 2017, 12:54 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
0
because you didn't define x as global so it just took the value you specified at last when calling function. that's it
17th May 2017, 3:20 PM
Biraj Adhikari
Biraj Adhikari - avatar