Q:

Write a function that will receive a variable number of input arguments: the length and width of a rectangle, and possibly also the height of a box that has this rectangle as its base

0

 Write a function that will receive a variable number of input arguments: the length and width of a rectangle, and possibly also the height of a box that has this rectangle as its base. The function should return the rectangle area if just the length and width are passed, or also the volume if the height is also passed.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

boxcalcs.m

function [area, varargout] = boxcalcs(len, wid, varargin)

% Calculates a rectangle area and possibly also a

% height of a box and returns the area and possibly volume

% Format of call: boxcalcs(length, width) or

% boxcalcs(length, width, height)

% Returns area of box and if height is passed, volume

% always return the area

area = len * wid;

n = nargin;

% if the height was passed, return the box volume

if nargin == 3

 varargout{1} = area * varargin{1};

end

end

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now