| New Reply |
[Javascript?] Using a function as an argument for another function |
Share Thread | Thread Tools |
| Sep19-12, 07:50 AM | #1 |
|
|
[Javascript?] Using a function as an argument for another functionCode:
var square = function (x) {
return x * x;
};
var cube = function (x) {
return square * x;
};
|
| Sep19-12, 10:08 AM | #2 |
|
Mentor
|
Code:
var cube = function (x) {
return x * square(x);
};
|
| Sep19-12, 01:19 PM | #3 |
|
Recognitions:
|
In C or C++, you can use pointers to functions as arguments:
Code:
#include <stdio.h>
int f1xf2(int (*pf1)(int), int (*pf2)(int), int v){return(pf1(v) * pf2(v));}
int returnv(int v){return(v);};
int square(int v){return(f1xf2(returnv, returnv, v));}
int cube(int v){return(f1xf2(returnv, square, v));}
int quad(int v){return(f1xf2(square, square, v));}
int main(int argc, char ** argv)
{
printf("square 2 = %3d\n", square(2));
printf("cube 3 = %3d\n", cube(3));
printf("quad 4 = %3d\n", quad(4));
return(0);
}
|
| Sep19-12, 08:18 PM | #4 |
|
|
[Javascript?] Using a function as an argument for another function
I am confused...it always bugs me when people do not state what language they are talking about...sure, if I knew, I would recognize it; but often times I don't and I can still help, if I knew what to google.
So, the last post is C, sure...what's the first one, too? or is it matlab? |
| Sep20-12, 12:42 AM | #5 |
|
Mentor
|
Looks like JavaScript to me.
|
| Sep21-12, 05:43 AM | #6 |
|
|
Sorry, it's JavaScript
|
| Sep24-12, 03:05 PM | #7 |
|
|
Once again, sorry. This time for abandoning my own topic. Like I said the programming language is JavaScript. And yes, thanks for pointing out, that the title is misleading: the function is indeed calling another function, not using it as an argument.
Returning to the problem. "When we call the square function as a part of the cube function, we still have to define the parameter associated with the square function" - a hint provided says thus. But how to write this assignment? I have never assigned a parameter for a function in the body of another function. Any hints? Should I assign a parameter the same way as a variable? |
| Sep24-12, 03:09 PM | #8 |
|
|
These kind of simple questions is something you don't need a forum for...you can play around with the language and its compiler/interpreter and test stuff at will...it does not cost any money and you are not going to break anything...play around, experiment!
...if, on the other hand, you have a major logic problem or a bug that you cannot find...then, the forum could help. |
| Sep24-12, 03:19 PM | #9 |
|
Recognitions:
|
|
| Sep24-12, 03:37 PM | #10 |
|
|
Using a function as an argument to another function would work like this:
Code:
var square = function(x) { return x * x; }
var cube = function(f,x) { return x * f(x); }
cube(square,2);
|
| Sep24-12, 09:09 PM | #11 |
|
|
Code:
var square = function (x) {
return x * x;
};
var cube = function (x) {
return square(x) * x;
};
|
| Sep24-12, 09:23 PM | #12 |
|
Recognitions:
|
I thought the idea was to use functions as parameters to other functions. I'm not sure if this syntax is correct for javascript, but it should look something like this:
Code:
var f1xf2 = function(f1, f2, x) {return f1(x) * f2(x);}
var returnx = function(x) {return x;}
var square = function(x) {return x * x;}
var cube = function(x) {return f1xf2(returnx, square, x);}
|
| Sep25-12, 02:48 AM | #13 |
|
|
|
| New Reply |
| Thread Tools | |
Similar Threads for: [Javascript?] Using a function as an argument for another function
|
||||
| Thread | Forum | Replies | ||
| Calling function with no input argument | General Engineering | 3 | ||
| statistics of sinc function with normal argument | Set Theory, Logic, Probability, Statistics | 1 | ||
| What is the term for the argument in a logarithm function? | General Math | 3 | ||
| Argument Of Transfer Function - What does arg(T(w)) mean in real life | Engineering, Comp Sci, & Technology Homework | 3 | ||
| zeta function for complex argument | Calculus | 1 | ||