# Thread: In Matlab, how to refer a self-defined function in another self-defined function?

1. ## In Matlab, how to refer a self-defined function in another self-defined function?

Dear all,

I have a question on Matlab: when I define a new function in Matlab, I want to use another function defined by myself previously as an input argument, how can I do that?

To be more specific, suppose I have defined a function, say f(x). Now I am going to define a new function g(x)=f(x)+x and using f(x) as one of the input argument in my definition of g(x).

Is there anybody who can help me on this? Thanks a lot in advance.

2. Originally Posted by analysismath
Dear all,

I have a question on Matlab: when I define a new function in Matlab, I want to use another function defined by myself previously as an input argument, how can I do that?

To be more specific, suppose I have defined a function, say f(x). Now I am going to define a new function g(x)=f(x)+x and using f(x) as one of the input argument in my definition of g(x).

Is there anybody who can help me on this? Thanks a lot in advance.
Define the second function as:

Code:
function rv=g(ff,x)
rv=ff(x)+x;
Then call with:

Code:
y=g(@f,x);
In the calling code @f is the function handle for the function f.

CB

3. Thank you so much! It works perfectly as you said.