User-defined Functions

We have created a function circle in the file circle.m

To see what it's about we can type

help circle
  [circumference, area] = circle(r)
  Given a radius r, 
  this function returns the circumference 
  and area of a circle.
  If r is a vector, then vectors of corresponding 
  values are returned.

If we just call circle with a scalar argument then the circumference (1st output) is returned

circle(5)
ans =

   31.4159

To get both values we use, e.g.,

[C A] = circle(5)
C =

   31.4159


A =

   78.5398

Vectorized usage:

[C A] = circle([2 4 6])
C =

   12.5664   25.1327   37.6991


A =

   12.5664   50.2655  113.0973

And here's the entire code in circle.m

type circle
function [circumference, area] = circle(r)
% [circumference, area] = circle(r)
% Given a radius r, 
% this function returns the circumference 
% and area of a circle.
% If r is a vector, then vectors of corresponding 
% values are returned.

circumference = 2*pi*r;
area = pi*r.^2;        % .^ so we can handle vector input

% end of function circle