# Examples of MATLAB functions

Some very short, simple functions:

• square.m Calculates the square of a number.
• cube.m Calculates the cube of a number.
• plusone.m Adds one to a number.
• isperfect.m Checks to see if a number is "perfect".
• count_odd.sci Counts the number of odd numbers within a given range.

Functions showing input/output in MATLAB

• counter.m Prints out numbers in a loop
• echo_number.m Prompts user for a number, then echoes it.
• write_file.m Creates a file on disk and fills with input from the user.
• read_file.m Reads information from a file on disk, printing it to the screen.
• plot_from_file.m Read data from a file with 4 columns, plots col 4 vs. col 2.

More complex functions, which check for valid input arguments:

• array_min.m Finds the smallest numerical value in a 1-D array.
• array_max.m Finds the largest numerical value in a 1-D array.
• array_extremes.m Finds and returns _both_ the smallest and largest numerical values in a 1-D array. Illustrates how a function can return more than one value.

A function which illustrates feval:

• calculate.m Calls a given function with a given single argument.

A function which illustrates strcmp:

• guess_letter.m Plays a guessing game with the user.

Example of how one can split a single big program into a main loop plus subroutines

• silly_pi.m Calculate pi by successive approximations to the area of a circle. No subroutines, all one big program.
• silly_pi_2.m Same program, now with main loop and a single subroutine (area_inside), all in one source code file.
• silly_pi_main.m Main program only, requires external area_inside.m file
• area_inside.m Subroutine only.