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

2 views (last 30 days)
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 ...

Answers (2)

Steven Lord
Steven Lord on 27 Apr 2016
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 on 27 Apr 2016
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.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!