C/++/# Use the output of a function in another function

CAF123

Gold Member
Say I have a function func that does a certain task, returning some expression exp. Can I use this expression in another function without having to call func again, which I suppose will redo all the steps needed to derive exp in the first place?

E.g
Code:
 double func() {
...code.....
return exp
}
Now, I want to use exp in another function, but without calling func, like

Code:
 double newfunc(double exp) {
...code .....
}
Thanks! I did not use C++ much before so probably it's a naive question...

Related Programming and Computer Science News on Phys.org

Orodruin

Staff Emeritus
Science Advisor
Homework Helper
Gold Member
2018 Award
If you need the output for something else, you can store it in a variable and call the new function with that variable as the argument. If you don't need it for something else, why don't you just put the first function in the argument?

lomidrevo

Well, it seems that name of your exp variable exists only within the function func(). The function returns only the value of the variable exp, and its name is not relevant anywhere outside the function definition.
Similarly for newfunc, the name of input parameter exp will exists only within this function.

So far, you only defined your two functions and none of them was really called. But if you want to use output of the first function as an input to the second, you can call both of them in single line if you prefer :

C++:
double x;
...
x = newfunc(func());

cosmik debris

Also worth mentioning is that if the function is pure, i.e. does not alter any global variables, then if the function is called a second time with the same input parameters the answer will be the same. An optimising compiler knows this and will not call the function again, so there is no overhead in just using the first function as a parameter to the second.

Cheers

CAF123

Gold Member
If you need the output for something else, you can store it in a variable and call the new function with that variable as the argument. If you don't need it for something else, why don't you just put the first function in the argument?
Yes, the output is to be used in another function. Basically there is an integration routine within ...code.... in func and it will return an expression exp. I wish to somehow call the exp and not func.

Code:
 double func(double z, double w) {
.....code.......
return exp  }
where exp is a function of w only, the ....code.... contains an integration over z.

Now how to call exp for use in another function without reference to func? If I reference func it will redo unnecessarily all the integration....

lomidrevo

so why don't you store the result of func to a variable, so you can re-use it as many times as you wish without calling the function multiple-times? (this is what @Orodruin suggested before):

C++:
double x;
double y;
...
y = func(...provide input parameters...);
...
x = newfunc(y);
...
// here, y contains still the same value, so you can re-use it again...

Now how to call exp
as I said before, exp doesn't exists outside the function definition, so you cannot access it by this name. Unless it is an global variable, you can get it only by executing the function.

This is also an interesting point (and honestly I was not aware of it):
Also worth mentioning is that if the function is pure, i.e. does not alter any global variables, then if the function is called a second time with the same input parameters the answer will be the same. An optimising compiler knows this and will not call the function again, so there is no overhead in just using the first function as a parameter to the second.

CAF123

Gold Member
Thanks @lomidrevo yes that makes sense, my only worry is that everytime I call func it will perform the function task every time and not just use the result. The result is needed to be used multiple times in my code. E.g in one case I want to do a numeric differentiation of the result. If I call func, it will do a numeric integration and then do the numeric differentiation, whereas it would be nice to just call the result of func which is then differentiated. I see this as being important especially if the numeric integration is costly in time, I don't want to needlessly redo the integration every time I want to use the expression.

Mark44

Mentor
From post #1.
CAF123 said:
Say I have a function func that does a certain task, returning some expression exp.
What language are you using?
If you are writing code in C or a language derived from C, a function does not return an expression -- it returns a value, The value could be a number (integer or floating point), an address, a struct or class instance.
It won't return something like "x^2 + 3x," if that's what you're thinking, without a lot of other work.
Thanks @lomidrevo yes that makes sense, my only worry is that everytime I call func it will perform the function task every time and not just use the result.
It's not clear to me what you mean by this. Unless the function is a void function (one that doesn't return a value), it will return a value of some kind.
CAF123 said:
The result is needed to be used multiple times in my code. E.g in one case I want to do a numeric differentiation of the result. If I call func, it will do a numeric integration and then do the numeric differentiation, whereas it would be nice to just call the result of func which is then differentiated.
Let's get the terminology right so that you are thinking the right way. You don't "call the result of a function" -- you call a function, and it returns some value.
CAF123 said:
I see this as being important especially if the numeric integration is costly in time, I don't want to needlessly redo the integration every time I want to use the expression.
If your function needs to do two different things, you need a parameter that controls what happens in the function.

Please tell us more about what you are trying to do, including the language you intend to use to implement this.

Roberto Teso

so why don't you store the result of func to a variable, so you can re-use it as many times as you wish without calling the function multiple-times? (this is what @Orodruin suggested before):

C++:
double x;
double y;
...
y = func(...provide input parameters...);
...
x = newfunc(y);
...
// here, y contains still the same value, so you can re-use it again...
I think this is also a best practice for code maintenance and readability, putting a func() directly as an input in another func() could be difficult to spot in future code revision.

CAF123

Gold Member
From post #1.What language are you using?
If you are writing code in C or a language derived from C, a function does not return an expression -- it returns a value, The value could be a number (integer or floating point), an address, a struct or class instance.
It won't return something like "x^2 + 3x," if that's what you're thinking, without a lot of other work.
It's not clear to me what you mean by this. Unless the function is a void function (one that doesn't return a value), it will return a value of some kind.
Let's get the terminology right so that you are thinking the right way. You don't "call the result of a function" -- you call a function, and it returns some value.

If your function needs to do two different things, you need a parameter that controls what happens in the function.

Please tell us more about what you are trying to do, including the language you intend to use to implement this.
Thanks @Mark44 Basically what I'm trying to do is the following: Function A does a nasty integral and returns a value and then I'd like to perform a derivative of this value via Function B.

So, I have a function $C(x,z)$ which is integrated over x in Function A. I assume (probably this is where I go wrong) that Function A returns a value, which is something like $\tilde{C}(z)$. I then want to use this $\tilde{C}(z)$in another function to perform a derivative with respect to z of it. Is it clear?

I'm using C++

Mark44

Mentor
Thanks @Mark44 Basically what I'm trying to do is the following: Function A does a nasty integral and returns a value and then I'd like to perform a derivative of this value via Function B.
Unless there's way more than you have told us, this makes no sense.
Function A performs an integration and returns a value. If you're talking about a definite integral, the value returned will be a number. When function B differentiates this number, it will return 0, since the derivative of a constant is zero. I don't think that's what you have in mind, though, and I suspect that the scope of the problem you're trying to solve is beyond your abilities, based on what you have posted. I mean no offense by this.

Are you thinking that your integration function will do symbolic integration? E.g., that if the input function is say, $x^2$, the output function will be $\frac{x^3}3$? (Omitting the constant of integration...)
This is a much more difficult problem than performing numeric integration, and even that has difficulties because you have to have code somewhere that represents the function itself that you will be integrating.
CAF123 said:
So, I have a function $C(x,z)$ which is integrated over x in Function A. I assume (probably this is where I go wrong) that Function A returns a value, which is something like $\tilde{C}(z)$. I then want to use this $\tilde{C}(z)$in another function to perform a derivative with respect to z of it. Is it clear?

I'm using C++
Not really. Yes, function A will return a value, but it will be a number, not something like $\tilde{C}(z)$, whatever that means.

CAF123

Gold Member
Unless there's way more than you have told us, this makes no sense.
Function A performs an integration and returns a value. If you're talking about a definite integral, the value returned will be a number. When function B differentiates this number, it will return 0, since the derivative of a constant is zero. I don't think that's what you have in mind, though, and I suspect that the scope of the problem you're trying to solve is beyond your abilities, based on what you have posted. I mean no offense by this.
Let $C(x,z) = xz$ and perform the definite integral with respect to x. $$\int_{0}^1 xz dx = \frac{z}{2}.$$ Now I want to differentiate with respect to z. I get 1/2. The function C(x,z) in my case is a complicated convolution and what I want to do is to evaluate the integral over x for many different values of z and tabulate the results. Then I want to differentiate wrt z of c(z) and evaluate for many different values of z and tabulate the results. I propose to use gsl adaptive integration routines and gsl numerical differentiation for these tasks.

Mark44

Mentor
Let $C(x,z) = xz$ and perform the definite integral with respect to x. $$\int_{0}^1 xz dx = \frac{z}{2}.$$
What I've been saying is that a function will return a number, not an expression such as $\frac z 2$.
I'm oversimplifying here somewhat about what a function returns, but there is a lot of machinery that you would need to write, including parsing the input function ($xz$), finding an antiderivative, and evaluating it to produce an expression. This is pretty complicated stuff, and as I said already, I don't think you have the background to be able to tackle it.
CAF123 said:
Now I want to differentiate with respect to z. I get 1/2. The function C(x,z) in my case is a complicated convolution and what I want to do is to evaluate the integral over x for many different values of z and tabulate the results. Then I want to differentiate wrt z of c(z) and evaluate for many different values of z and tabulate the results. I propose to use gsl adaptive integration routines and gsl numerical differentiation for these tasks.
Apparently GSL is Gnu Scientific Library, which I haven't used. From a quick check of the documentation, the GSL adaptive integration routines are numeric integration techniques, meaning that if you supply a function and interval endpoints, they will return a numerical approximation to the integral. IOW, a number.

CAF123

Gold Member
What I've been saying is that a function will return a number, not an expression such as $\frac z 2$.
I'm oversimplifying here somewhat about what a function returns, but there is a lot of machinery that you would need to write, including parsing the input function ($xz$), finding an antiderivative, and evaluating it to produce an expression. This is pretty complicated stuff, and as I said already, I don't think you have the background to be able to tackle it.
But if I treat z as a constant in the integration what will it return? Similar to mathematica's numeric integration via ?NumericQ.

What background is needed? Indeed I haven't used C++ much before but am familiar with mathematica and other specialised physics tools. Basically I've been dumped a load of C++ code and have to incorporate these tasks into the code. IMO, the best way to proceed is to take a course in C++ but it appears I've jumped into the deep end and expected to get the result without doing so.

Mark44

Mentor
But if I treat z as a constant in the integration what will it return? Similar to mathematica's numeric integration via ?NumericQ.
Well, that's the thing. Your code would have to parse the expression $xz$ and would need many rules that specify how to find an antiderivative, not to mention rules that can separate out one symbol (x) from another (z) and deal with them separately.
CAF123 said:
What background is needed? Indeed I haven't used C++ much before but am familiar with mathematica and other specialised physics tools. Basically I've been dumped a load of C++ code and have to incorporate these tasks into the code. IMO, the best way to proceed is to take a course in C++ but it appears I've jumped into the deep end and expected to get the result without doing so.
Being familiar with mathematica or other applications that do symbolic integration or differentiation is almost no help if you're handed a bunch of C++ code, especially if you haven't used C++ much in the past.

Two suggestions:
1. Talk to whoever assigned this work to you, and explain that you are unclear on what you are expected to do. Does the person who assigned this work to you have any idea of what all is involved? Would this person be able to do the work himself/herself? If so, ask for guidance, explaining your background. If this person wouldn't be able to do this work, it's unreasonable IMO for you to be saddled with it.
2. It's possible that Mathematica provides "hooks" into its symbolic integration/differentiation routines provided via DLLs (dynamic link libraries). If so, and I really don't know if they do, someone knowledgeable in C++ could write a program that references the relevant DLLs to use the functions in these DLLs.

Want to reply to this thread?

"Use the output of a function in another function"

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving