[Javascript?] Using a function as an argument for another function

Click For Summary

Discussion Overview

The discussion revolves around the use of functions as arguments in JavaScript, specifically focusing on how to correctly implement a function that calls another function. Participants explore various coding approaches and clarify misunderstandings related to function syntax and behavior.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • Some participants propose a function `cube` that should call `square`, but initial attempts are incorrect due to misunderstanding how to pass arguments.
  • One participant corrects another by stating that `cube` should call `square(x)` instead of just `square`.
  • A participant introduces a C/C++ example to illustrate using function pointers, which raises questions about language context.
  • Several participants express confusion about the programming language being discussed, with clarifications confirming it is JavaScript.
  • One participant suggests that using functions as arguments can be done with a syntax like `var cube = function(f,x) { return x * f(x); }`, demonstrating a correct implementation.
  • Another participant acknowledges that the original problem was misunderstood and seeks further clarification on how to assign parameters within functions.
  • Some participants emphasize the importance of experimentation with the language rather than relying solely on forum assistance for simple questions.
  • Multiple participants explore the idea of using functions as parameters, with varying levels of confidence in their syntax and correctness.

Areas of Agreement / Disagreement

There is no consensus on the best approach to using functions as arguments, as participants present multiple methods and interpretations. Some express confidence in their solutions, while others remain uncertain about syntax and implementation.

Contextual Notes

Participants express uncertainty regarding the correct syntax for JavaScript and the specific requirements of the original problem. There are also discussions about the appropriateness of using functions as arguments in this context.

Who May Find This Useful

Readers interested in JavaScript programming, particularly those learning about function usage and argument passing, may find this discussion beneficial.

mindauggas
Messages
127
Reaction score
0
Code:
var square = function (x) {
  return x * x;
};

var cube = function (x) {
  return square * x;
};

This is my shot, its not right. Please indicate or suggest the correct answear. :)
 
Last edited by a moderator:
Technology news on Phys.org


mindauggas said:
Code:
var square = function (x) {
  return x * x;
};

var cube = function (x) {
  return square * x;
};

This is my shot, its not right. Please indicate or suggest the correct answear. :)

No, it's not correct.
Code:
var cube = function (x) {
  return x * square(x);
};
Your thread title is misleading - you aren't using a function as an argument of another function. One function is calling another function. There's a difference.
 
Last edited:


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);
}
 
Last edited:


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?
 


Looks like JavaScript to me.
 
Sorry, it's JavaScript
 
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?
 
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.
 
mindauggas said:
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?

Can you state the original problem/assignment as it is given to you?
 
  • #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);

Though in this context I don't know why you'd want to pass in a function. I don't imagine you'd ever be swapping it out.
 
  • #11
DavidSnider said:
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);

Though in this context I don't know why you'd want to pass in a function. I don't imagine you'd ever be swapping it out.

Thanks, this solved it.

Code:
var square = function (x) {
  return x * x;
};

var cube = function (x) {
  return square(x) * x;
};

The answer to your question: it just for the sake of knowing how to use functions in all/allmost alt the ways legitimate.
 
  • #12
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);}
 
  • #13
rcgldr said:
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);}

The original title probably mislead you, I apologize for this. the title should have stated that one function calls another function :)
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 13 ·
Replies
13
Views
5K