I was wondering whether this is legal to do. Could I have something like:
function funct(a, foo(x)) { ... }
where a
is an array and x
is an integer argument for another function called foo
?
(The idea is to have one function that uses a for loop on the array, and calls that function in the params for every element in the array. The idea is so call this on different functions so elements of two arrays are multiplied and then the sums are added together. For example A[0] * B[0] + A[1] * B[1]
.)
Functions in the functional programming paradigm can be passed to other functions as parameters. These functions are called callbacks. Callback functions can be passed as arguments by directly passing the function's name and not involving them.
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.
You can use addEventListener to pass this to a JavaScript function.
Functions are data, and therefore can be passed around just like other values. This means a function can be passed to another function as an argument. This allows the function being called to use the function argument to carry out its action. This turns out to be extremely useful.
I think this is what you meant.
funct("z", function (x) { return x; }); function funct(a, foo){ foo(a) // this will return a }
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