CISC106-010, Fall 2007, Quiz #2,
Questions and Responses 9/24/2007
(1) Write a function prototype in MATLAB that will take three numbers and
calculate the multiplication of these three numbers.
(Note: your function prototype
should include a function name, input variables to the function, and output
variables of the function. You are asked only
to write the function prototype for this function not the function body,
which are the statements following the function prototype that actually
implements the function).
Answer
1:
function multip =
multiplyThreeNumbers(number1, number2, number3)
Note that you were asked only to write the statement above, as a
response to this question. The above line, is the function prototype that includes the name of the function (multiplyThreeNumbers), input variables to the
function(number1, number2, number3), and the output variables
of the function (for this function, there is only one output variable, which is
multip).
The complete function can
be written as below into a text file and then is saved as
multiplyThreeNumbers.m (note that, the name of the M-file has to be the same as the name of
the function that is written in the M- file ! But, this is not the case for
script M-files !)
|
Line 1 is the function prototype, line 2-9 forms the function body.
(2) Write a function call in MATLAB to calculate the multiplication of the
following three numbers 10, 211, 45 using your function prototype from question
1 above.
Answer 2:
>> multiplyThreeNumbers(10, 211,
45)
To call a function, you need to write the name of the function and
give the inputs (also called, actual
parameters) to the function. The actual
parameters for the function call
above are 10 , 211, and 45, which will be assigned as the values of the formal parameters or input variables of the function which
are
number1, number2, and
number3,
respectively.
Note that, the name of the
output variable of the function (which is multip in the Answer 1 above)
does not actually appear anywhere when we call the function !
(3) Assume you have a file named subtract.m in the current directory
with the following content.
function result = subtract(x,y) result = x-y; |
What is printed to the
screen when you type the following MABLAB statements in the MATLAB command
prompt?
>> a = 10;
>> b = 2;
>> subtract(10,2);
>> result
Answer 3:
>> result
??? Undefined function or variable 'result'.
This is the case, because, result is the output variable of
the function subtract and is only defined (i.e., visible) within the function subtract. Another way to say this
is the variable result is local to
the function subtract. Therefore, the variable
result is not defined in the current workspace,
eventhough after the function call which is the line
>> subtract(10,2);
For practice questions, click here.
(4) Assume you have a file named subtract.m in the current directory with
the following content.
result = x-y; |
What is printed to the
screen when you type the following MATLAB statements in the MATLAB command
prompt?
>> a = 10;
>> b = 2;
>> subtract
>> result
Answer 4:
In this case, two error messages
will be printed to the screen. First error statement will be printed to the
screen, after line
>> subtract
??? Undefined function or variable 'x'.
Error in ==> subtract at 1
result = x-y;
Note that, this is the case
because x and y are not defined in the current workspace.
The second error message is
printed to the screen after
>> result
??? Undefined function or variable 'result'.
This is the case, because
the script M-file subtract, actually could not calculate a value for
the variable result in the previous line. Therefore, the variable result
is not
created (i.e., defined) in the current workspace.
For practice questions, click here.
(5) Write two
tips/idea/comments/facts you got from the article “Learning: Your First Job” that can help you to really learn (i.e., deeply understand) a subject?
Answer 5:
The article "Learning:
Your First Job" by Robert Leamnson is available as .doc file and .html
file.