0
0
MATLABdata~10 mins

Input and output arguments in MATLAB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Input and output arguments
Function called with inputs
Inputs assigned to input args
Function body executes
Output args assigned values
Function returns outputs
Outputs received by caller
This flow shows how inputs enter a function, the function runs, and outputs are sent back to the caller.
Execution Sample
MATLAB
function [sum, diff] = add_sub(a, b)
  sum = a + b;
  diff = a - b;
end

[x, y] = add_sub(5, 3);
This function takes two inputs, returns their sum and difference, and the caller receives both outputs.
Execution Table
StepActionInput ArgumentsOutput ArgumentsNotes
1Function calleda=5, b=3sum=?, diff=?Inputs assigned to a and b
2Calculate suma=5, b=3sum=8, diff=?sum = 5 + 3 = 8
3Calculate diffa=5, b=3sum=8, diff=2diff = 5 - 3 = 2
4Function endsa=5, b=3sum=8, diff=2Outputs ready to return
5Outputs assignedN/Ax=8, y=2Caller receives outputs
6Execution completeN/Ax=8, y=2Function finished
💡 Function completes after assigning outputs and returning to caller
Variable Tracker
VariableStartAfter Step 2After Step 3Final
aundefined555
bundefined333
sumundefined888
diffundefinedundefined22
xundefinedundefinedundefined8
yundefinedundefinedundefined2
Key Moments - 3 Insights
Why do we assign inputs to variables 'a' and 'b' inside the function?
Because when the function is called (see Step 1 in execution_table), the input values are passed and assigned to the input argument variables 'a' and 'b' so the function can use them.
How does the function return two outputs?
The function assigns values to the output variables 'sum' and 'diff' inside (Steps 2 and 3), then these are returned to the caller and assigned to 'x' and 'y' (Step 5).
What happens if the caller requests fewer outputs than the function provides?
If fewer outputs are requested, MATLAB assigns only those outputs and ignores the rest. This is not shown here but is important to know.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 3, what is the value of 'diff'?
A3
B2
C8
Dundefined
💡 Hint
Check the 'Output Arguments' column at Step 3 in the execution_table.
At which step does the function assign the input values to variables 'a' and 'b'?
AStep 1
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Action' and 'Notes' columns in the execution_table for when inputs are assigned.
If the caller only requested one output, how would the variable_tracker change for 'y'?
A'y' would be 8
B'y' would be 2
C'y' would be undefined or not assigned
D'y' would be equal to 'x'
💡 Hint
Recall that outputs not requested by the caller are not assigned (see key_moments about output count).
Concept Snapshot
MATLAB functions receive inputs as arguments and return outputs.
Syntax: function [out1, out2] = func(in1, in2)
Inputs are assigned to input variables when called.
Outputs are assigned inside and returned to caller.
Caller receives outputs in order requested.
If fewer outputs requested, extra outputs are ignored.
Full Transcript
This visual trace shows how MATLAB functions handle input and output arguments. When the function is called, the input values are assigned to the input variables inside the function. The function then runs its code, calculating outputs by assigning values to output variables. Finally, these outputs are returned to the caller and assigned to variables there. The execution table steps through each action, showing variable values changing. Key moments clarify common confusions about input assignment and output return. The quiz tests understanding of variable values at different steps and the effect of requesting fewer outputs. This helps beginners see clearly how data flows into and out of MATLAB functions.