Function Arguments in MATLAB
🎯 Function Arguments in MATLAB
They make functions flexible, reusable, and powerful.
MATLAB is developed by MathWorks.
🔹 What Are Function Arguments?
-
Input arguments → values passed into a function
-
Output arguments → values returned from a function
1️⃣ Single Input Argument
🔸 Function
🔸 Call
Output
2️⃣ Multiple Input Arguments
🔸 Function
🔸 Call
Output
3️⃣ Multiple Output Arguments
🔸 Function
🔸 Call
Output
4️⃣ Function with No Input Arguments
🔸 Function
🔸 Call
Output
5️⃣ Function with No Output Arguments
🔸 Function
🔸 Call
Output
6️⃣ Optional Arguments Using nargin
nargin returns the number of input arguments passed.
🔸 Calls
Output
7️⃣ Number of Outputs Using nargout
nargout returns the number of outputs requested.
🔸 Call
Output
📌 Only first output is returned.
8️⃣ Default Values for Arguments
🔸 Call
Output
⚠️ Important Rules
-
Input arguments are comma-separated
-
Output arguments use square brackets
[ ] -
Order of arguments matters
-
Variables inside functions are local
🎯 Interview Questions: Function Arguments in MATLAB
🔹 Q1. What are function arguments?
Answer:
Values passed to and returned from a function.
🔹 Q2. How do you pass multiple inputs to a function?
Answer:
Using commas: fun(a, b, c).
🔹 Q3. How do you return multiple outputs?
Answer:
Using square brackets: [x, y] = fun(a, b).
🔹 Q4. What does nargin do?
Answer:
Returns the number of input arguments.
🔹 Q5. What does nargout do?
Answer:
Returns the number of output arguments requested.
🔹 Q6. Can MATLAB functions have default arguments?
Answer:
Yes, using nargin.
🔹 Q7. Are function arguments local or global?
Answer:
Local to the function.
✅ Summary
-
Arguments control function flexibility
-
MATLAB supports multiple inputs & outputs
-
narginandnargoutadd robustness -
Essential for modular programming
