Function Arguments in MATLAB

MATLAB Tutorial

🎯 Function Arguments in MATLAB

Function arguments in MATLAB are the inputs and outputs passed to and returned from a function.

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

36

2️⃣ Multiple Input Arguments

🔸 Function


 

🔸 Call


 

Output

25

3️⃣ Multiple Output Arguments

🔸 Function


 

🔸 Call


 

Output

25
15

4️⃣ Function with No Input Arguments

🔸 Function


 

🔸 Call


 

Output

20-May-2026 14:32:10

5️⃣ Function with No Output Arguments

🔸 Function


 

🔸 Call


 

Output

Hello Student

6️⃣ Optional Arguments Using nargin

nargin returns the number of input arguments passed.


 

🔸 Calls


 

Output

5
15

7️⃣ Number of Outputs Using nargout

nargout returns the number of outputs requested.


 

🔸 Call


 

Output

7

📌 Only first output is returned.


8️⃣ Default Values for Arguments


 

🔸 Call


 

Output

3.1416
28.2743

⚠️ 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

  • nargin and nargout add robustness

  • Essential for modular programming

You may also like...