CISC106-010, Fall 2007, Practice Questions                 9/28/2007

 

Instructions: To get most out of this exercize first do the questions by yourself. Then click here to check your answers.

 

(1) Assume we have a funtion M-file named areaOfPizza.m. The content of the file is as below.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

 

function area = areaOfPizza(diameter)

% areaOfPizza computes the area of pizza, given diameter

%

% consumes:

%     diameter: number, diameter of the pizza in inches

% produces:

%     area: number, area of the pizza in square inches

%    

% example

%     >> areaOfPizza(12)

%     ans = 113.10

 

% let’s calculate the radius first 

  radius = diameter / 2;

 

% then let’s find the area

  area = pi * radius ^ 2;

 

  return;

 

  end % function

 

Answer the following questions about this function.

a)     Which line is the function prototype?

b)    Looking at the function prototype, identify the following

a.     Name of the function

b.    Name of the output variable(s)

c.     Name of the input variable(s)

d.    Name of the formal parameter(s)

a)     Which lines form the H1-comment?

b)    What happens when the below MATLAB statement is executed

>> help areaOfPizza

c)     Which lines form the function body?

d)    Give the names of the variables that are local  (i.e., are only visible) inside the function.

e)     If we have a 12 inch pizza, how do we learn the area of this pizza using this function? (i.e., write the MATLAB statement to calculate the area of pizza using this function)

>> 

f)     If we call the function as

>> areaOfPizza(14);

What is the actual parameter(s) in this function call?

(2)  Assume there is a function M-file named slope.m. The content of the file is as below. There are 3 errors on this file (function implementation). Identify these errors.

Function output = slope[x1,y1,x2,y2]

% slope calculate slope of a line given two points on that line

%

% consumes: x1, y1   the coordinates of the first point

%           x2, y2   the coordinates of the second point

%

% produces: result, the slope of the line

%

  result = (y2 - y1) / (x2 - x1);

  return;

end

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Answers

(1)

(a) The first line

 

function area = areaOfPizza(diameter)

 

is the function prototype.

 

(b)

a.   areaOfPizza

b.  area

c.  diameter

d.    diameter (Formal paramaters are the input parameters in the function prototype)

 

(c) Lines 2-11. Note that H1-comment ends with the first blank line (line 12).

 

(d) The H1-comment of the function is printed to the screen without the % signs.

 

(e) Lines 2-21. Note that function body includes the H1-comment as well.

 

(f)    area, diameter, radius. Note that input (diameter) and output variables (area) are also local to the function.

 

(g)   Just call the function as

>> areaOfPizza(12)

 

(h)   Actual paramaters happen when you call the function. In this case we have only one actual parameter which is 14. Value of the actual parameter will be the value of the formal paramater when we call the function. That is, function will be executed by assigning value 14 to the formal paramater diameter.

 

 

(2) 

1

2

3

4

5

6

7

8

9

10

11

Function output = slope[x1,y1,x2,y2]

% slope calculate slope of a line given two points on that line

%

% consumes: x1, y1   the coordinates of the first point

%           x2, y2   the coordinates of the second point

%

% produces: result, the slope of the line

%

  result = (y2 - y1) / (x2 - x1);

  return;

end

 

o    Error 1:

Line 1: The keyword Function must be function (i.e., start with small f). Note that MATLAB is case-sensitive!!

 

o    Error 2:

Line 1: instead of bracets [] , you must use regular paranthesis() to write the input variables in a function prototype. That is, line 1 should be as follows slope(x1,y1,x2,y2).

 

o    Error 3:

The output variable  output is not assigned a value within the function body. To fix this,

 

·         you can change the names of the output variable output in the function prototype in line 1 to variable result. 

·         or change the line 9 to

 

result = (y2 - y1) / (x2 - x1);

 

o    Error 4:

Line 7 should be

 

     % produces: output, the slope of the line

 

However, this error is not crucial. The function still executes, because line 7 is simply a comment (Note that any line that starts with the sign % in an M-file is a comment for MATLAB). MATLAB does not try making sense of the comments or does not execute comments.