% This is the MATLAB m-file with filename "square.m" % It defines a matlab function called "square()" which % returns the square of any scalar argument. % % Usage Example: "x = square(2)" sets the variable x to a value of 4. % % Note that comment lines begin with a percent character. % Note that if you type "help square", this first set of contiguous % comment lines will be printed. % % 31 JAN 96 LLW % % Here is the function declaration function answ = square( anynumber ) % "function" is a MATLAB keyword. It declares this file to be a function. % "answ" is a new variable which which we will be returned when this % function terminates. We must assign it a value. % "square" is the name of this new function. The filename is "square.m". % "anynumber" is a new variable containing the argument specified by % the user when they called this function. % % Note: the variable names "answ" and "anynumber" are arbitrary. % Note: the function name "square" by convention agrees with the filename. % % AVOID PREDEFINED MATLAB KEYWORDS when declaring variable and function % names. For example "for", "while", "sqrt", and "pow" are keywords. % we can create and use any sort of local variables, e.g: foo = 2; newfoo = foo + anynumber; % When you put ";" at the end of a line, the line is evaluated silently. % When you omit ";" at the end of a line, the line's result % will be echoed to the screen when the function is evaluated. % Somewhere, we must assign a value to the variable answ, which % will be the return value of the function, e.g.: answ = anynumber * anynumber;