A first function.

:

Plus x: A first function. ACTIVITY This tool is provided by a third party. Though your activity may be recorded, a page refresh may be needed to fill the banner. Plus x: A first function Assign y with 5 plus x. Ex: If x is 2, then y is 7. If x is 19, then y is 24. Note: Some challenge activities require completing a function (as above), or writing a new function. You can create test calls to the function in the “Code to call your function” window, then press “Run”. Try appending this call to that window: IncreaseValue(9) When done writing and testing your function, press “Submit for Assessment”, which will automatically apply various test calls to your function. Your Function 1 function y = IncreaseValue(x) 2 3 % x: User defined value that is added to 5 4 % Modify the line below to y = 5 + x 5 y = x; 6 7 end Save Reset MATLAB Documentation Code to call your function 1 IncreaseValue(2) CReset
CMALLEPIGE 3.3.1: Plus x: A first function. ACTIVITY This tool is provided by a third party. Though your activity may be recorded, a page refresh may be needed to fill the banner. Plus x: A first function Assign y with 5 plus x. Ex: If x is 2, then y is 7. If x is 19, then y is 24. Note: Some challenge activities require completing a function (as above), or writing a new function. You can create test calls to the function in the “Code to call your function” window, then press “Run”. Try appending this call to that window: IncreaseValue(9) When done writing and testing your function, press “Submit for Assessment”, which will automatically apply various test calls to your function. Your Function 1 function y = IncreaseValue(x) 2 3 % x: User defined value that is added to 5 4 % Modify the line below to y = 5 + x 5 y = x; 6 7 end Save Reset MATLAB Documentation Code to call your function 1 IncreaseValue(2) CReset

The Correct Answer and Explanation is:

function y = IncreaseValue(x)
% IncreaseValue adds 5 to the input value x and returns the result in y.
%
% Usage examples:
%   y = IncreaseValue(2)   % returns 7
%   y = IncreaseValue(19)  % returns 24

y = 5 + x;   % core calculation

end

Explanation
In MATLAB, every function begins with a definition line that declares the output variable(s), the function’s name, and its input argument(s). Here, function y = IncreaseValue(x) states that the routine is called IncreaseValue, it takes one input (x), and it returns one output (y). MATLAB functions live in their own .m files whose file name must match the function name—in this case, IncreaseValue.m—so that MATLAB’s interpreter can locate and execute the code.

The core requirement of the exercise is “assign y with 5 plus x.” That operation is a simple scalar addition, so the body of the function only needs a single executable statement:

y = 5 + x;

When MATLAB reaches this line it evaluates the right-hand side first, adding the constant 5 to the value held in x, then stores the result in the variable y. Because y is also named on the function’s first line, MATLAB automatically returns its final value to the calling workspace once the function finishes executing.

The earlier template code incorrectly set y = x, which simply echoed the input without adding 5. Replacing that line with y = 5 + x satisfies the specification and passes the auto-grader’s hidden tests.

The comment block preceding the calculation serves two purposes. First, it documents intent, making the code easier to read and maintain. Second, MATLAB displays the first contiguous comment block as help text when a user enters help IncreaseValue or doc IncreaseValue, so including concise usage examples is good practice.

You can verify the function by running the recommended test calls in the “Code to call your function” window:

IncreaseValue(2)   % returns 7
IncreaseValue(19)  % returns 24
IncreaseValue(9)   % returns 14

Each output matches the expected result, demonstrating that the function now behaves correctly for arbitrary numeric inputs, whether they are scalars, vectors, or matrices—MATLAB will add 5 element-wise by default.

function y = IncreaseValue(x)
% IncreaseValue adds 5 to the input value x and returns the result in y.
%
% Usage examples:
%   y = IncreaseValue(2)   % returns 7
%   y = IncreaseValue(19)  % returns 24

y = 5 + x;   % core calculation

end
y = 5 + x;

When MATLAB reaches this line it evaluates the right-hand side first, adding the constant 5 to the value held in x, then stores the result in the variable y. Because y is also named on the function’s first line, MATLAB automatically returns its final value to the calling workspace once the function finishes executing.

The earlier template code incorrectly set y = x, which simply echoed the input without adding 5. Replacing that line with y = 5 + x satisfies the specification and passes the auto-grader’s hidden tests.

The comment block preceding the calculation serves two purposes. First, it documents intent, making the code easier to read and maintain. Second, MATLAB displays the first contiguous comment block as help text when a user enters help IncreaseValue or doc IncreaseValue, so including concise usage examples is good practice.

You can verify the function by running the recommended test calls in the “Code to call your function” window:

IncreaseValue(2)   % returns 7
IncreaseValue(19)  % returns 24
IncreaseValue(9)   % returns 14

Each output matches the expected result, demonstrating that the function now behaves correctly for arbitrary numeric inputs, whether they are scalars, vectors, or matrices—MATLAB will add 5 element-wise by default.

Scroll to Top