User-defined Functions in CS1112: Lecture 10, Study notes of Computer Science

A lecture slides from cs1112, a computer science course, focusing on user-defined functions. The slides cover topics such as the benefits of writing user-defined functions, the differences between scripts and functions, and examples of function calls. The slides also include exercises for the students to practice.

Typology: Study notes

Pre 2010

Uploaded on 08/31/2009

koofers-user-xgh
koofers-user-xgh 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS1112 Lecture 10 2008/9/30
Lecture slides 1
September 30, 2008 Lecture 10 2
Previous lecture
User-defined functions
Today’s lecture
User-defined functions
Examples
local memory space
Announcements:
Section in classrooms this week
We return Prelim 1 at the end of lecture. If you don’t get
your paper in lecture, pick it up during consulting hours
at ACCEL Green Rm (Engineering Lib., Carpenter Hall)
starting at 4pm today.
Accessing your functions
For now*, put your related functions and scripts
in the same directory.
dotsInCircles.m
randDouble.m
polar2xy.m
drawColorDot.m
*The path function gives greater flexibility. Not required in CS1112.
MyDirectory
Any script/function that
calls
polar2xy.m
September 30, 2008 Lecture 10 4
dotsInCircles.m
(functions with multiple input parameters)
(functions with a single output parameter)
(functions with multiple output parameters)
(functions with no output parameter)
September 30, 2008 Lecture 10 5
Why write user-defined function?
1. Elevate reasoning by hiding details
2. Facilitate top-down design
3. Software management
4. A function can be independently tested easily
5. Keep a driver program clean by keeping detail
code in functions—separate, non-interacting
files
September 30, 2008 Lecture 10 6
Script vs. Function
A script is executed line-by-
line just as if you are typing it
into the Command Window
The value of a variable in a
script is stored in the Command
Window Workspace
A function has its own private
(local) function workspace
that does not interact with
the workspace of other
functions or the Command
Window workspace
Variables are not shared
between workspaces even if
they have the same name
September 30, 2008 Lecture 10 7
What will be printed?
% Script file
p= -3;
q= absolute(p);
disp(p)
function q = absolute(p)
% q is the absolute value of p
if (p<0)
p= -p;
end
q= p;
A: -3 B: 3 C: error
pf3
pf4

Partial preview of the text

Download User-defined Functions in CS1112: Lecture 10 and more Study notes Computer Science in PDF only on Docsity!

September 30, 2008 Lecture 10 2

„ Previous lecture

„ User-defined functions

„ Today’s lecture

„ User-defined functions „ Examples „ local memory space

„ Announcements:

„ Section in classrooms this week „ We return Prelim 1 at the end of lecture. If you don’t get your paper in lecture, pick it up during consulting hours at ACCEL Green Rm (Engineering Lib., Carpenter Hall) starting at 4pm today.

Accessing your functions

For now*, put your related functions and scripts in the same directory.

dotsInCircles.m

randDouble.m

polar2xy.m

drawColorDot.m

*The path function gives greater flexibility. Not required in CS1112.

MyDirectory

Any script/function that calls polar2xy.m

September 30, 2008 Lecture 10 4

dotsInCircles.m

(functions with multiple input parameters) (functions with a single output parameter) (functions with multiple output parameters) (functions with no output parameter)

September 30, 2008 Lecture 10 5

Why write user-defined function?

  1. Elevate reasoning by hiding details
  2. Facilitate top-down design
  3. Software management
  4. A function can be independently tested easily
  5. Keep a driver program clean by keeping detail code in functions—separate, non-interacting files

September 30, 2008 Lecture 10 6

Script vs. Function

„ A script is executed line-by- line just as if you are typing it into the Command Window „ The value of a variable in a script is stored in the Command Window Workspace

„ A function has its own private (local) function workspace that does not interact with the workspace of other functions or the Command Window workspace „ Variables are not shared between workspaces even if they have the same name

September 30, 2008 Lecture 10 7

What will be printed?

% Script file

p= -3;

q= absolute(p);

disp(p)

function q = absolute(p)

% q is the absolute value of p

if (p<0)

p= -p;

end

q= p;

A: -3 B: 3 C: error

September 30, 2008 Lecture 10 9

What will be printed?

% Script file p= -3; q= absolute(p); disp(p)

function q = absolute(p) % q is the absolute value of p if (p<0) p= -p; end q= p; Command Window Workspace p -

September 30, 2008 Lecture 10 12

What will be printed?

% Script file p= -3; q= absolute(p); disp(p)

function q = absolute(p) % q is the absolute value of p if (p<0) p= -p; end q= p; Command Window Workspace p -

Function absolute’s Workspace p (^) -

September 30, 2008 Lecture 10 14

What will be printed?

% Script file p= -3; q= absolute(p); disp(p)

function q = absolute(p) % q is the absolute value of p if (p<0) p= -p; end q= p; Command Window Workspace p -

Function absolute’s Workspace p (^) -

September 30, 2008 Lecture 10 15

What will be printed?

% Script file p= -3; q= absolute(p); disp(p)

function q = absolute(p) % q is the absolute value of p if (p<0) p= -p; end q= p; Command Window Workspace p -

Function absolute’s Workspace p (^3)

September 30, 2008 Lecture 10 16

What will be printed?

% Script file p= -3; q= absolute(p); disp(p)

function q = absolute(p) % q is the absolute value of p if (p<0) p= -p; end q= p;

Command Window Workspace p -

Function absolute’s Workspace p (^3) q 3

September 30, 2008 Lecture 10 17

What will be printed?

% Script file p= -3; q= absolute(p); disp(p)

function q = absolute(p) % q is the absolute value of p if (p<0) p= -p; end q= p;

Command Window Workspace p -

Function absolute’s Workspace p (^3) q 3

September 30, 2008 Lecture 10 25

x = 1;

y = 3;

x = f(x,y);

y = x;

disp(y)

function y = f(y,x)

x = x+1;

y = x+1;

What is the output?

A: 3 B: 4 C: 5 D: 6 E: 7

September 30, 2008 Lecture 10 26

Subfunction

„ There can be more than one function in an M-file

„ top function is the main function and has the name of

the file

„ remaining functions are subfunctions, accessible only by

the functions in the same m-file

„ Each (sub)function in the file begins with a function

header

„ Keyword end is not necessary at the end of a

(sub)function

September 30, 2008 Lecture 10 28

Array index starts at 1

Let k be the index of vector x, then „ k must be a positive integer „ 1<= k <= length(x) „ To access the k th^ element: x(k)

x 5 .4 .91 -4 -1 7 1 2 3 4 5 6

September 30, 2008 Lecture 10 30

Prelim 1

„ Q1: program trace; boolean expression ☺ „ Q2: common loop patterns ☺ „ Q3: conditional ☺. „ Q4: while-loop; simulation ☺ „ Q5: nested for-loops ☺/

„ Median 90

„ Mean 86.4; Standard Deviation 12.