Introductory Programming Fall 2005 For today, you should have: 1) Read Chapter 3 2) Prepared for a quiz Today: 1) functions! For next time you should: 1) Finish/Review Chapter 2, especially functions and workspaces! 2) Prepare for a quiz on functions. 3) Read Chapter 3 Scripts, part two ----------------- Open update.m from last time, or type in something like: froma = round(0.03 * a); fromb = round(0.05 * b); a = a - froma + fromb; b = b - fromb + froma; if statements ------------- n = n + 1 if n < 10 update for loops --------- for i=1:10 update end fprintf ------- Formatted printing (ugly feature!) fprintf('%f\t%f\n', a, b) From scripts to functions ------------------------- Create an m-file named square.m and put the following lines in it function y = square (x) % SQUARE Computes x raised to the second power. y = x^2 Now type help square square(3) z = square(n+1) Whole lotta new vocabulary! The first line of a function is the prototype: it specifies the name of the function, and the input arguments (in this case x) output arguments (in this case y) The second line is a comment that documents this function. The third line assigns a value to an output argument, which is special. The value of the output argument is the "return value" from this function. When the function completes, the return value replaces the function call.