views:

110

answers:

5

I have a function, we'll call it funcA that returns an object. I want to pass that object to funcB. Can I do this?

funcB(funcA());

so that funcA() is called first and the results are passed to funcB()?

+5  A: 

Yes, yes you can.

Ken Browning
+3  A: 

I don't see any problem with this, have you tried any sample?

It should work fine.

If you have faced any specific issue please post it.

If you want to pass the funcA as a parameter to funB then you have to call funcB as funcB(funcA)

Arun P Johny
A: 

Yes. The parameters to the function are expressions which are evaluated before the expression value is passed to the function. An expression can of course be a function call.

DVK
A: 

Yes, this should work as you want. funcB(funcA()) calls funcA() and passes the result to funcB()

Perhaps you were concerned that it would pass the function funcA to funcB()? For that, the syntax would be funcB(funcA)

Tim Goodman
+3  A: 

If it helps, Yisroel, you can think of the object returned "replaces" the function in-place. So, after funcA() is executed, it is "replaced" by the returned value and passed into funcB().

Generally, though, you wouldn't want to do nesting functions like this for more than 3 levels. If it starts to become unreadable, store the object into a variable and passing this variable into the next function can make your code much more readable.

So the answer is yes, as everyone else pointed out.

Xavier Ho
actually, doing this is a code smell and though I have done it before, I would recommend against doing this at all, let alone for 3 levels (?!)
Darko Z
Sometimes it can be readable. i.e. Python 3: `print(sum(len(getLines()))`
Xavier Ho
Darko, why would you recommend against this?
Yisroel