In the following code
x= (f,n) -> f(n)
...
x( (n) -> n+1 , 5) #parse error
How can I fix the parse error above ?
Thanks
We cannot pass the function as an argument to another function. But we can pass the reference of a function as a parameter by using a function pointer. This process is known as call by reference as the function parameter is passed as a pointer that holds the address of arguments.
You can simply invoke a function by placing parenthesis after its name as shown in the following example. // Generated by CoffeeScript 1.10. 0 (function() { var add; add = function() { var a, b, c; a = 20; b = 30; c = a + b; return console.
Passing a function as an argument to the function is quite similar to the passing variable as an argument to the function. so variables can be returned from a function. The below examples describe passing a function as a parameter to another function.
Functions, like any other object, can be passed as an argument to another function.
A pair of parenthesis would fix this problem, just found the answer on IRC.
x( (n) -> n+1 , 5) #parse error
x ((n) -> n+1) , 5 #fixed
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With