


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 4
This page cannot be seen from the preview
Don't miss anything!



September 30, 2008 Lecture 10 2
User-defined functions
User-defined functions Examples local memory space
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?
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?
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
What is the output?
A: 3 B: 4 C: 5 D: 6 E: 7
September 30, 2008 Lecture 10 26
Subfunction
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 ☺/