0

What's actually happening here?

def square(x): return x*c; def test(func, x): print(func(x)); test(square, 42) 1.This is the procedure given in the problem...I understand that the x has been given the value of "x*x", but is it true that the function "square"has really been passed as an argument to function"tets"? 2.And...what is the"func" in the function"test"? If it's an argument...what about"print(func(x))", which makes it seems like another appointes function??? Really wondering....Thxxxx a lot

19th Sep 2016, 7:55 AM
Lucas2016
4 odpowiedzi
+ 1
Yes, the function square has been passed as an argument to test(). If you want an example of standard function that takes a function as parameter, see map. http://book.pythontips.com/en/latest/map_filter.html By the way, square should return x*x, not x*c (no c here): def square(x): return x*x;
19th Sep 2016, 8:09 AM
Zen
Zen - avatar
+ 1
No, not a reserved word. Any other identifier would have been okay, as for any other parameter. You don't need to do anything special to pass a function as parameter.
19th Sep 2016, 9:14 AM
Zen
Zen - avatar
+ 1
I believe that this will essentially cube x. In other words: 42*42=square and 42*42*x(42)=test
14th Oct 2016, 12:17 AM
Rance Garrett
Rance Garrett - avatar
0
Thx Zen...so "func" is the reserved word for "function" in Python right??? [That x*c is simply a slip...]
19th Sep 2016, 8:58 AM
Lucas2016