0

Is it possible to call a function as a function argument? I'm trying to get the value of the bar function and pass it to the foo function.

If I try to set the function to a variable

e.g.

$foobar = bar($x)

it will execute the function where ever the variable is found.

e.g.

function bar($x) {
    $x = 1;
    return 1;
}
function foo(x) {
    $x += $x;
    echo $x;
}

foo(bar($x));
4
  • 4
    Have you tried converting the pseudo-code to actual PHP code and running it? Commented Feb 12, 2011 at 20:53
  • This looks like phpreboot syntax, not php. Commented Feb 12, 2011 at 20:54
  • $ before the name of the variable? Commented Feb 12, 2011 at 20:56
  • What makes you think it shouldn't work? And how is that in any way related to functional programming? Commented Feb 12, 2011 at 21:02

2 Answers 2

4

Yes, you can do this in PHP, and virtually all high level programming languages. bar($x) will be evaluated, and its return value will be passed as the argument to foo().

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. I made it 10 years ago. PHP is pretty old. ;)
I made a similar avatar a while back, but the delimiters were in red, I used <?php and I didn't have a function call in the middle. Fun stuff!
Dan what languages would you recommend?
Recommend for what? I recommend programming in whatever language you know best.
0

x should be $x, other than that there's nothing wrong with your code. The answer is 'yes, you can use a function call as an argument in another function. It's pretty standard stuff.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.