General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information...
-
date post
19-Dec-2015 -
Category
Documents
-
view
216 -
download
1
Transcript of General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information...
General Computer Science General Computer Science for Engineersfor Engineers
CISC 106CISC 106Lecture 02Lecture 02
James AtlasComputer and Information Sciences
6/10/2009
Lecture OverviewLecture OverviewHow does our program work?
More on VariablesMore on ExpressionsMore on FunctionsM-files
- Scripts versus functions
How does our program work?CPU
Disk
Memory
Putting it together
VariablesVariablesvar = expression
x = 2 * 2comment = ‘This is a string’area = circleArea(5)
VariablesVariablesvar = expression
x = 2 * 2comment = ‘This is a string’area = circleArea(5)
What type of data is stored in each variable?
ExpressionsData + Operator
◦2 + 2◦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?
ExpressionsData + Operator
◦2 + 2◦circleArea(5)
Nested Expressions◦circleArea(5) + circleArea(3)
FunctionsFunctionsAnalogous to mathematical
functions◦f(x) = x + 1
Each function is like a mini-program within the larger program itself
Key to breaking problems down
FunctionsFunctionsIn MATLAB, the first line of a function
takes the following form:function <return value> =
<name>(<arguments>)
Functions cont.Functions cont.The first line of a function:function <return value> =
<name>(<arguments>)
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
FunctionsFunctionsFunctions can call other functions
Like in class on Monday:
function outputValue = ringArea(rad_1, rad_2)
outputValue = circleArea(rad_1) - circleArea(rad_2);
FunctionsFunctionsBreak down problems into
subproblems◦Decomposition
How big/small should a function be?
Problem SolvingProblem SolvingMP3 player
◦Store song lists, store playlists, import/export songs, shuffle play, repeat play, etc.
Bank ATM SoftwareBank ATM SoftwareWhat are inputs and outputs?Inputs:Output:
Bank ATM SoftwareBank ATM SoftwareWhat functions might we need?
Bank ATM SoftwareBank ATM SoftwareWhat functions might we need?
Break it down even more?
M-filesM-filesA 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. functionsScripts vs. functionsFunctions have input and output
parametersScripts are a sequence of commands
Functions are more flexibleFunction files have one function per
file