General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009
Lecture Overview How does our program work? More on Variables More on Expressions More on Functions M-files - Scripts versus functions
How does our program work? CPU Disk Memory Putting it together
Variables var = expression x = 2 * 2 comment = ‘This is a string’ area = circleArea(5)
Variables var = expression x = 2 * 2 comment = ‘This is a string’ area = circleArea(5) What type of data is stored in each variable?
Expressions Data + Operator ◦ ◦ circleArea(5)
Our ringArea(rad1, rad2) function out=ringArea(radius1, radius2) area1 = circleArea(radius1); area2 = circleArea(radius2); out = area1 - area2; Can you think of a simpler way to write this?
Expressions Data + Operator ◦ ◦ circleArea(5) Nested Expressions ◦ circleArea(5) + circleArea(3)
Functions Analogous to mathematical functions ◦ f(x) = x + 1 Each function is like a mini-program within the larger program itself Key to breaking problems down
Functions In MATLAB, the first line of a function takes the following form: function = ( )
Functions cont. The first line of a function: function = ( ) The return value can be a number, a string, a matrix, etc. Arguments ◦ Can be a list of zero or more variables ◦ Can also be numbers, strings, matrices
Functions Functions can call other functions Like in class on Monday: function outputValue = ringArea(rad_1, rad_2) outputValue = circleArea(rad_1) - circleArea(rad_2);
Functions Break down problems into subproblems ◦ Decomposition How big/small should a function be?
Problem Solving MP3 player ◦ Store song lists, store playlists, import/export songs, shuffle play, repeat play, etc.
Bank ATM Software What are inputs and outputs? Inputs: Output:
Bank ATM Software What functions might we need?
Bank ATM Software What functions might we need? Break it down even more?
M-files A script file ◦ Store commands in ◦ Running a script file (m-file) A function file ◦ Special type of m-file ◦ Contains a function name, arguments, etc., and implementation
Scripts vs. functions Functions have input and output parameters Scripts are a sequence of commands Functions are more flexible Function files have one function per file