Hi, I would like to do a function that I can use many times in my code, like if I code in C language. I want to say that my function that 2 arguments (2 int for example, event if there is no type in matlab). Is it possible ? Thank you

1 次查看(过去 30 天)
for example I code : function Y = moncode(X, Y) I would like to use this function many times in my code but my variable won't be everytime X, and Y ...

回答(2 个)

Steven Lord
Steven Lord 2016-4-27
That's fine. The variable names used inside a function don't need to agree with the names of the variables passed into the function as inputs; in fact, the inputs to the function don't even need to be named variables!
abracadabra = 37;
why(abracadabra) % This works
why(37) % This works too
Ideally, users of a function shouldn't know or care what the developer of the function wrote inside the function, and that includes variable name choice. You may be curious, but you shouldn't NEED to know to use the function.
Is the name of the variable that contains the input value inside why.m named abracadabra? No.
Does that matter to why? No.

Jan
Jan 2016-4-27
User-defined functions follow the same rules as built-in Matlab functions. See:
sin(2.1)
X = 2.1
sin(X)
Y = X;
sin(Y)
The sin function does not care about the name of the variable, because it processes the value only.

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by