Use the output of a function in another function

In summary: 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.In summary, the conversation discusses the use of a function `func` that returns an expression `exp` and whether or not it is possible to use `exp` in another function without having to call `func` again. It is suggested to either store the output of `func` in a variable or to call both functions in a single line. It is also mentioned that if the function is pure, it may be optimized by the compiler to avoid calling it multiple times. The terminology of calling a function is clarified.
  • #1
CAF123
Gold Member
2,948
88
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...
 
  • Love
Likes twild19
Technology news on Phys.org
  • #2
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?
 
  • Like
Likes symbolipoint
  • #3
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());
 
  • #4
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
 
  • Like
Likes lomidrevo
  • #5
Orodruin said:
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...
 
  • #6
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...
CAF123 said:
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):
cosmik debris said:
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.
 
  • Like
Likes Roberto Teso
  • #7
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.
 
  • #8
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.
CAF123 said:
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.
 
  • #9
lomidrevo said:
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.
 
  • Like
Likes lomidrevo
  • #10
Mark44 said:
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++
 
  • #11
CAF123 said:
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.
 
  • #12
Mark44 said:
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.
 
  • #13
CAF123 said:
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.
 
  • #14
Mark44 said:
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.
 
  • #15
CAF123 said:
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.
 

1. How do I use the output of one function in another function?

To use the output of one function in another function, you can assign the output to a variable and then pass that variable as an argument in the second function. Alternatively, you can directly call the first function inside the second function and use its output.

2. Can the output of a function be used as an input for multiple functions?

Yes, the output of a function can be used as an input for multiple functions. You can assign the output to multiple variables and pass them as arguments in different functions, or you can directly use the output in multiple functions.

3. What happens if the output of the first function is not compatible with the second function?

If the output of the first function is not compatible with the second function, you will get an error. It is important to make sure that the output of one function is compatible with the input of the second function to avoid errors.

4. Can the output of a function be modified before using it in another function?

Yes, the output of a function can be modified before using it in another function. You can store the output in a variable and then modify it using built-in functions or custom code before passing it as an argument in the second function.

5. What are some advantages of using the output of one function in another function?

Using the output of one function in another function can help reduce code duplication and improve code efficiency. It also allows for better organization and modularization of code, making it easier to maintain and update in the future.

Similar threads

  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
11
Views
944
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
6
Views
831
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
2
Views
649
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
2
Views
3K
Back
Top