Home  >  Article  >  Software Tutorial  >  Operation steps for one function to call another function in Matlab

Operation steps for one function to call another function in Matlab

WBOY
WBOYOriginal
2024-06-01 17:41:14660browse

In programming, function calling is the key to achieving modular programming. In Matlab, a function can call another function, thereby achieving code reuse and task decomposition. This article will introduce in detail the steps of function calling in Matlab, including: Function declaration: Define the function to be called Function calling: Use the function name and parameters to call the function Parameter passing: Pass the parameters to the called function Return value: Get the function of the called function Return value Through this guide, you can master the basic principles of function calling in Matlab and easily implement modular programming.

First create a new m file function Untitled2() in Matlab, Untitled2() is the main function, then add a sub-function count(i) below the function, and call the sub-function in the main function. The code is as follows:

% Main function

function [ sum ] = Untitled2()

i=10.

sum=count(i).

end

% Subfunction

function [sum] = count(i)

sum=0.

for k=1:i

sum=sum+i.

end

end

Operation steps for one function to call another function in Matlab

Then enter [Untitled2()] in the command line window and call the main function to get the returned sum value result is 100. Note here that the main function can call the sub-function, but the main function cannot be called in the sub-function, as shown in the figure:

Operation steps for one function to call another function in Matlab

Method 2: Two m-file function calls

First create two m-file functions in Matlab, one Untitled2() function and one count(i) function. Call the count(i) function directly in the Untitled2() function, as shown in the figure:

Operation steps for one function to call another function in Matlab

You can take a look at the content of the count(i) function, as shown in the figure:

Operation steps for one function to call another function in Matlab

Enter Untitled2() in the command line window and press Enter to get With the same result, the advantage of this writing method is that the two functions can call each other, and there is no difference between the main function and the sub-function, as shown in the figure:

Operation steps for one function to call another function in Matlab

Enter count( in the command line window 16), you can also see the results directly, as shown in the figure:

Operation steps for one function to call another function in Matlab

The above is the detailed content of Operation steps for one function to call another function in Matlab. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn