0
0
MATLABdata~10 mins

Local functions in MATLAB - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a local function named 'square' that returns the square of input 'x'.

MATLAB
function y = main()
    y = square(5);
end

function result = [1](num)
    result = num^2;
end
Drag options to blanks, or click blank then click option'
Apower
Bmultiply
Ccube
Dsquare
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different function name than the one called in main.
Forgetting to define the local function after the main function.
2fill in blank
medium

Complete the code to call the local function 'greet' inside the main function.

MATLAB
function main()
    message = [1]('Alice');
    disp(message);
end

function msg = greet(name)
    msg = ['Hello, ' name '!'];
end
Drag options to blanks, or click blank then click option'
Agreet
Bhello
CsayHello
Dwelcome
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a function name that does not exist.
Using a different case or spelling for the function name.
3fill in blank
hard

Fix the error in the local function definition by completing the function header correctly.

MATLAB
function output = main()
    output = addFive(10);
end

function result = [1](x)
    result = x + 5;
end
Drag options to blanks, or click blank then click option'
AAddfive
BaddFive
Caddfive
Dadd_Five
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect capitalization in the function name.
Adding underscores or changing spelling.
4fill in blank
hard

Fill both blanks to create a local function 'multiply' that multiplies two inputs 'a' and 'b'.

MATLAB
function result = main()
    product = multiply(3, 4);
    disp(product);
end

function [1] = [2](a, b)
    [1] = a * b;
end
Drag options to blanks, or click blank then click option'
Aresult
Boutput
Cmultiply
Dprod
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the function or output variable than in the call.
Mixing up the order of output variable and function name.
5fill in blank
hard

Fill all three blanks to create a local function 'concatStrings' that joins two strings with a space.

MATLAB
function result = main()
    combined = concatStrings('Hello', 'World');
    disp(combined);
end

function [1] = [2](str1, str2)
    [1] = [str1 [3] str2];
end
Drag options to blanks, or click blank then click option'
Aresult
BconcatStrings
C' '
Dstrcat
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong function name or output variable.
Forgetting to add the space between strings.
Using 'strcat' instead of square brackets for concatenation.