MTEC1003 — Media Computation Skills Lab
Function Calls + Definitions
Encapsulation
Algorithms
Built-In Functions
Parameters + Arguments
…but in Python, NOT JavaScript!…
…okay fine, we’ll also talk
a little bit about JavaScript too.
There, ya happy?!
So, let’s get started:
“Functions are 'self contained' modules of code that accomplish a specific task. Functions usually 'take in' data, process it, and 'return' a result.”
We can break down a program into sub-steps.
Each sub-step can be its own function.
When any program seems too hard,
just break down the program into sub-steps!
—view source
We can recycle our code instead of constantly
needing to retype it over and over again!
When you think about it,
we benefit similarly from writing a for loop.
We can test a portion of our code,
by isolating it from the rest.
—view source
Finally, remember way back when we learned variables?
Well, as you’ll see in larger programs,
functions will also let you
organize elaborate systems of variables
accessed by different parts of your program.
They’ll be called global and local variables.
Functions reduce our code
by encapsulating many ordered steps
into their own self-contained unit,
allowing us to run complicated routines
by executing one line of code instead of many!
Algorithms are the sets of instructions
executed by our programs.
They can be described apart from any syntax
or language we’re working with…
Remember our lovely flowcharts?
These came up when we described conditions.
We can use flowcharts to plan and describe
that we type out in code.
Some procedures may require a conditional, as we learned.
Conditions involve making a choice,
accounting for multiple possible scenarios, etc.
…while other step-by-step procedures
may require things like:
counting
iterating
generating
What sort of programming have we learned for this?
As you may have guessed,
we can best use a for loop
to accomplish something like this.
But in general,
any step-by-step process
may be captured
inside a function…
That means things like:
for loops,
conditionals,
and other programming statements
can ALL be contained inside your functions.
Because they describe the individual steps
or tasks within your algorithm.
To use a function, that is, to execute it,
we simply call the function
somewhere in our program,
like this…
In JavaScript:
console.log("Hello world!");
In Python:
print("Whoa, I can print something... How useless.")
This is known as a function call.
1. Type the name of the function.
2. Don’t type a space…
3. Type parens: ()
4. Inside the parens, type comma-separated arguments
given as input to your function.
For example, in Python:
my_function_name(argument1, argument2, argument3)
Now, compare this to:
print("Whoa, I can print something... How useless.")
The print function is called by passing it 1 argument.
Here, the argument is a string,
which the function prints to the console.
But your computer doesn’t just “know”
miraculously how to interpret print()
…
How does your computer know
what a function does?
Like console.log()
in JavaScript,
print()
in Python is a built-in function,
meaning it is defined somewhere
inside the Python code base.
What are some built-in functions you’ve already used?
float()
int()
input()
str()
type()
That’s nice & all…
But I’m a creative do0d…
And I write my own functions,
thank you very much…
So,
what if you need to write
a function that isn’t built-in?
Well, first of all,
You can write your very own functions!
And this is done by first creating a function definition.
When writing your own functions,
You first need to define its individual steps.
The syntax is different for various programming languages,
but generally, they all follow a pattern:
function toCelsius(fahrenheit) {
var tempInCelsius = (5 / 9) * (fahrenheit - 32);
return tempInCelsius;
}
Line 1: Type function
to declare a function statement.
Give your function a NAME, e.g. toCelsius
Inside parens ()
type your parameters, e.g. fahrenheit
.
Type curly brackets: {}
On subsequent lines, code the individual steps
of your algorithm inside your curly brackets:
var tempInCelsius = (5 / 9) * (fahrenheit - 32);
Here we’ve defined variable tempInCelsius
and used the parameter fahrenheit
in our calculation.
Write as many steps as you need; here we only need 1.
Often we need a function to return
a value to us:
For example, on the last line of our function definition:
return tempInCelsius;
}
When this function is called within our program,
the value of tempInCelsius
will be returned to the program, at the place where the function call was made.
For example, if we call our function
somewhere in our program, say here:
var tempInStLouis = toCelsius(104);
toCelsius
will return
the value 40
on this line,
which means its resulting value of 40
will be assigned to the variable tempInStLouis
!
Maybe you’ve already guessed it, but…
in Python, the same logic will require
some slight syntactical adjustments…
def toCelsius(fahrenheit):
tempInCelsius = (5 / 9) * (fahrenheit - 32)
return tempInCelsius
Notice anything different?
def
instead of function
colon :
instead of curly brackets {}
and of course, no semicolons ;
Notice any patterns?
Just like when we write for loops and conditionals,
in Python, we don’t use curly brackets for code blocks,
but instead rely heavily on proper indentation.
So, be sure your indenting game is on fleek…
By now, you’ve noticed both of these terms.
def my_function_name(param1, param2, param3):
my_function_name(argument1, argument2, argument3)
You may hear them used interchangeably,
but they mean different things…
In a function definition,
a parameter is a placeholder for input data
listed inside (parens),
and referenced inside of your function.
For example:
def my_function_name(param1, param2):
mult = param1 * param2
return mult
These parameters are simultaneously arguments…
Okay, haha, not that kind of argument…
In a function call,
an argument is a real value for each input data point
also listed inside (parens).
Our function expects these arguments
because they’re defined by the parameters we set!
We create a parameter for each input data point
in our function definition.
Then we pass input values, or arguments, when we make
our function call and thus use or execute our function.
Let’s see examples of both…
First, our function definition includes 2 parameters:
def my_stupid_multiplier(param1, param2):
mult = param1 * param2
return mult
Later, we supply our function call with 2 arguments:
my_stupid_multiplier(4, 8)
Our function knows to expect 2 values
because they were defined by our parameters.
“FUNCTIONS are ‘self contained’ modules of code…
that ‘take in’ data, process it, and ‘return’ a result.”
A PARAMETER is a placeholder for input values
designated in our function definition.
Later, when we execute a function,
we pass it ARGUMENTS
for the values our function expects.
Now, time to do the labs…