Function [output]=F​unctionNam​e(input)

7 ビュー (過去 30 日間)
Lisa Lee
Lisa Lee 2017 年 8 月 12 日
コメント済み: Steven Lord 2020 年 9 月 21 日
I don't understand why sometimes a pair of brackets is used when a function is created. I have seen it in two cases:
1. the output is empty 2. the output is logical
Can someone please help me to clarify the scope of using a pair of brackets for the output when a function is created?

採用された回答

per isakson
per isakson 2017 年 8 月 12 日
編集済み: per isakson 2017 年 8 月 14 日
Go to Create Functions in Files and look for Syntax for Function Definition. It says:
Output arguments (optional)
If your function returns one output, you can specify the output name after the function keyword. [...] If your function returns more than one output, enclose the output names in square brackets.
function [one,two,three] = myFunction(x)
The brackets are optional with zero and one output.
The Matlab template includes the brackets
function [ output_args ] = Untitled( input_args )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
end
I guess, this template encourage the use of brackets with zero and one output.
  2 件のコメント
Jan
Jan 2017 年 8 月 12 日
Explicitly:
function [] = Untitled( input_args )
% is equivalent to:
function Untitled( input_args )
And
function [out] = Untitled( input_args )
% is equivalent to:
function out = Untitled( input_args )
Lisa Lee
Lisa Lee 2017 年 8 月 12 日
Thank you. This helps to clarify things for me.

サインインしてコメントする。

その他の回答 (1 件)

Jaya Agnihotri
Jaya Agnihotri 2020 年 9 月 21 日
function mfunc = minmod((ur-ul)/Delta_x,(ul-ul1)/Delta_x)
mfunc=0;
if sign(v)==sign(w)
mfunc=sign(v).*min(abs(v,w));
end
how to define this function in a right way?
  1 件のコメント
Steven Lord
Steven Lord 2020 年 9 月 21 日
In a function definition the input arguments must be variable names, not expressions.
% define
function mfunc = minmod(v, w)
% Do stuff to define mfunc here
end
In a function call you can pass expressions in and the results of those expressions are assigned to the variables listed in the definition.
% call
theOutput = minmod(1:10, sind(1:10))
% In this particular call, v will be 1:10 and w will be sind(1:10)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by