home—info—lects—labs—exams—hws
—D2L—breeze (snow day)—tutor/PIs
lab03-function-terminology
Calling a function
doubles; 'argument', 'return type'
Review quiz (10min).
calling functions
Recall the examples we tried:
- pow(2,10)
- len("hello"), len("!"), and even len("").
-
What type do of information we give to len?
-
What type of answer does len give back to us?
-
import string
# This loads the entire library of string functions. Only needed once per session.
string.capitalize("hello")
# The name of the function is kind of long, because it includes the library-name at the start: “string.capitalize”
|
- sqrt(2) returns 1.414.
The last example — the square root of two — illustrates one more type
of data that python has:
“floating point numbers”,
which is a number with a decimal-point1.
They commonly called “float”s
or “double”s2.
Updated list:
In Python, we have several types of values:
- integers (“ints”):
e.g. 17,
9999,
-5,
0,
23423423432234234
- floating-point numbers (“double”s),
e.g. -2.34, 6.023e23, 1e9, 4.0
- characters: e.g. 'A', '+',
' ', '\n'.
- strings: e.g. "aloha", "Individual characters, strung together!"
Calling a function is like asking a question.
Well, it's lika person who only answers one very specific question, if you give them an input:
You walk up to (say) len, give them the information (e.g. “"Mississippi"”),
and they'll give you an answer: 11.
We say that len is the function name,
"Mississippi" is the argument we give it,
and 11 is the result.
Since the result is always an int,
we say: “len's return-type is int.”.
Give examples for some other functions we've seen:
-
function name: What a function is called — what you write immediately preceding the open-paren.
-
argument: The particular value provided to a function, when you call it.
-
return type: The type that the function returns as an answer, when called.
Can you name a function we've seen, that requires two arguments?
Day 4: I demo'd the functions below, and mentioned arguments and return-types;
we haven't tried it ourselves, nor discussed function-composition.
-
On google, find and download two pictures:
one of an an aardvark, and one of a echinda.
(Take note of where you are saving the pictures on your local computer —
presumably in folder called "Downloads".)
-
Evaluate the function-call pickAFile().
(Note: this function is not a standard python function; it is provided by JES.)
What type of data are we giving to pickAFile?
What is its return-type?
-
makePicture( "C:/Users/yourUserName/Downloads/yourAardvarkImageFile" )
What type of data are we giving to makePicture?
What is its return-type?
-
makePicture( pickAFile() )
What type of data are we giving to makePicture?
What is its return-type?
-
show( makePicture( "C:/Users/yourUserName/Downloads/yourAardvarkImageFile" ) )
What type of data are we giving to show?
-
show( makePicture( pickAFile() ) )
What type of data are we giving to show?
1
It turns out, inside the computer the decimal-point can “float around”,
hence the name. ↩
2
“double” is short for “double-precision floating-point”.
But these days, single-precision floating-points are hardly ever used, which is why
“double” has become a synonym for floating-point numbers.
↩
home—info—lects—labs—exams—hws
—D2L—breeze (snow day)—tutor/PIs