general computer science for engineers cisc 106 lecture 02 james atlas computer and information...

19
General Computer Science General Computer Science for Engineers for Engineers CISC 106 CISC 106 Lecture 02 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009

Post on 19-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009

General Computer Science General Computer Science for Engineersfor Engineers

CISC 106CISC 106Lecture 02Lecture 02

James AtlasComputer and Information Sciences

6/10/2009

Page 2: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009

Lecture OverviewLecture OverviewHow does our program work?

More on VariablesMore on ExpressionsMore on FunctionsM-files

-    Scripts versus functions

Page 3: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009

How does our program work?CPU

Disk

Memory

Putting it together

Page 4: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009

VariablesVariablesvar = expression

x = 2 * 2comment = ‘This is a string’area = circleArea(5)

Page 5: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009

VariablesVariablesvar = expression

x = 2 * 2comment = ‘This is a string’area = circleArea(5)

What type of data is stored in each variable?

Page 6: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009

ExpressionsData + Operator

◦2 + 2◦circleArea(5)

Page 7: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009

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?

Page 8: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009

ExpressionsData + Operator

◦2 + 2◦circleArea(5)

Nested Expressions◦circleArea(5) + circleArea(3)

Page 9: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009

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

Page 10: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009

FunctionsFunctionsIn MATLAB, the first line of a function

takes the following form:function <return value> =

<name>(<arguments>)

Page 11: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009

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

Page 12: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009

FunctionsFunctionsFunctions can call other functions

Like in class on Monday:

function outputValue = ringArea(rad_1, rad_2)

outputValue = circleArea(rad_1) - circleArea(rad_2);

Page 13: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009

FunctionsFunctionsBreak down problems into

subproblems◦Decomposition

How big/small should a function be?

Page 14: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009

Problem SolvingProblem SolvingMP3 player

◦Store song lists, store playlists, import/export songs, shuffle play, repeat play, etc.

Page 15: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009

Bank ATM SoftwareBank ATM SoftwareWhat are inputs and outputs?Inputs:Output:

Page 16: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009

Bank ATM SoftwareBank ATM SoftwareWhat functions might we need?

Page 17: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009

Bank ATM SoftwareBank ATM SoftwareWhat functions might we need?

Break it down even more?

Page 18: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009

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

Page 19: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009

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