Notes Sheet for CISC106, E02, Nov 09 2006

MATLAB vs. C++

  MATLAB C++
comments % comment // comment
string literals 'delimited with single quotes' "delimited with double quotes"
variables don't have to be declared
before they are used
have to be declared before they are used
(e.g. int x;)
choices: double, float, int
input  price = input('please input the price: ');  double price;
 cout << "please input the price: ";
 cin >> price;
output   fprintf('The price is %d\n',price);
or
  disp(['The price is ' num2str(price)]);
 cout << "The price is " << price << endl;
if/else statements

if/else/end is used to delimit blocks

if (condition)
  statement;
else
  statement;
end

{} is used to delimit blocks;
there is no end keyword

if (condition)
{
  statement;
}

else
{
  statement;
}

function
definition

start with the word   function
have output variable(s)
types are not needed on result or arguments
put   end   at the end of function body

function result = nameOfFunction(formalArg1)
%nameOfFunction brief statement of purpose
%rest of H1 comment
  statementsInBodyOfFunction;
  result = someExpression;
  return;
end

The word   function does not appear
there is no output variable
but the output type is required (e.g.   double)
arguments must have types (e.g.   double)
output value follows keyword   return
no keyword   end—use   {} around the body

double nameOfFunction(double formalArg1)
{
  statementsInBodyOfFunction;
  return someExpression;
}