1
E7: INTRODUCTION TO COMPUTER
ROGRAMMING FOR SCIENTISTS AND
PROGRAMMING FOR SCIENTISTS AND
ENGINEERS
Lecture Outline
•
The concept of a function
ATLAB functions
•
MATLAB functions
•
User defined functions
opyright 2007 Horowitz Packard This work is licensed under the Creative Commons Attribution- hare
E7 L6
Copyright 2007, Horowitz, Packard. This work is licensed under the Creative Commons Attribution Share
Alike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/
or send a
letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
2
The concept of a function
rule that assigns to each value of the
put
A rule that assigns to each value of the input
,
a corresponding output
value.
Symbolically:
{ } { }
12
,
,...,
(
,
,...,
)
nm
yy
y
xx
x
=
FUNCTION
input
utput
E7 L6
output
3
The concept of a function
ictorially:
Pictorially:
x
1
y
1
x
2
y
2
FUNCTION
.
.
x
n
y
.
.
Input/output data can be of various types, e.g., numbers,
m
E7 L6
pp
y
p
,
g
,
,
strings, arrays, etc.
4
FUNCTIONS in programming
set of commands that execute a “task”
at can be
• A set of commands that execute a task that can be
reuse in several instances.
• These commands are written as a function
• Functions are use to break down a
large task into a
ollection of smaller tasks
collection of smaller tasks.
unctions
re also called
ubroutines methods
•
Functions
are also called
subroutines, methods
,
etc.
E7 L6