Download presentation
Presentation is loading. Please wait.
1
General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009
2
Lecture Overview How does our program work? More on Variables More on Expressions More on Functions M-files - Scripts versus functions
3
How does our program work? CPU Disk Memory Putting it together
4
Variables var = expression x = 2 * 2 comment = ‘This is a string’ area = circleArea(5)
5
Variables var = expression x = 2 * 2 comment = ‘This is a string’ area = circleArea(5) What type of data is stored in each variable?
6
Expressions Data + Operator ◦ 2 + 2 ◦ circleArea(5)
7
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?
8
Expressions Data + Operator ◦ 2 + 2 ◦ circleArea(5) Nested Expressions ◦ circleArea(5) + circleArea(3)
9
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
10
Functions In MATLAB, the first line of a function takes the following form: function = ( )
11
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
12
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);
13
Functions Break down problems into subproblems ◦ Decomposition How big/small should a function be?
14
Problem Solving MP3 player ◦ Store song lists, store playlists, import/export songs, shuffle play, repeat play, etc.
15
Bank ATM Software What are inputs and outputs? Inputs: Output:
16
Bank ATM Software What functions might we need?
17
Bank ATM Software What functions might we need? Break it down even more?
18
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
19
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.