- Defining a function:

- Calling a function:

578 views (last 30 days)

Show older comments

How do you call a function within a function like

%filename: calculateA.m

function calculateA(arg1, arg2)

%calculations

function calculateB(arg1)

%calculations

end

%calculations

end

where you're trying to call calculateB(arg1) from say, the command window?

Stephen23
on 22 Jan 2019

Edited: Stephen23
on 22 Jan 2019

@Sterling Baird: your question shows some confusion. You ask "How do I call a function within another function?", but your example shows functions being defined. Calling a function and defining a function are two totally different things:

- Defining a function:

function y = myfun(x)

y = sin(x);

end

- Calling a function:

out = myfun(0.1);

Which of these do you actually want to ask about?

David Goodmanson
on 21 Jan 2019

Edited: David Goodmanson
on 22 Jan 2019

[ MODIFIED to use the terminology 'nested functions']

Hi Sterling,

You can define nested functions within other functions as in the following example.

function z = xsixth(x)

z = xsquare(x).^3;

function a = xsquare(b)

a = b.^2

end

end

Here the nested function xsquare is local to the function xsixth, and calling xsquare from the command line results in an error. If for some reason you need the output of (in this example) xsquare, you can either define it as a separate function and no longer a nested function or do something like the following

function [z y] = xsixth(x)

z = xsquare(x).^3;

y = xsquare(x);

function a = xsquare(b)

a = b.^2

end

end

with the extra output y pulling out the result.

Stephen23
on 22 Jan 2019

Edited: Stephen23
on 22 Jan 2019

The most useful feature of nested functions was not mentioned in this answer: nested functions can access variables in the main function's workspace:

function z = xsixth(x)

a = [];

xsquare(x);

z = a.^3;

function xsquare(b)

a = b.^2

end

end

Local functions are not nested within another function, but are written in the same file:

The different function types are explained in the documentation:

The MATLAB documentation does not use the terminology "subfunction".

Jim Riggs
on 21 Jan 2019

Your question is asking how to CALL a function from within another function, but your sample code is trying to DEFINE a function within another function. This you can't do. You define the functions in separate files:

%filename calculateA.m

function calculateA(arg1, arg2)

%calculations

..

[] = calculateB(arg) % you may call a function within a function simply by referencing it

% file calculateB.m must be in the Matlab path

end

separate file:

%filename calculateB.m

function calculateB(arg1)

% calculations

...

end

David Goodmanson
on 23 Jan 2019

Yushuo
on 5 Jul 2022

You can just call directly witin one function, for example

disp(hahaha(5));

function A=hello(x)

A= 5*x;

end

function b=hahaha(tv)

b=hello(tv);

end

result will be 5

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!