Function Definitions + Calls

einbahnstrasse.github.io/MTEC1003-slides/slides/functions.tutorial.v01/

MTEC1003 — Media Computation Skills Lab

In this tutorial, we’ll discuss…

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:

First of all, what is a Function?

“Functions are 'self contained' modules of code that accomplish a specific task. Functions usually 'take in' data, process it, and 'return' a result.”

view source

Why use a Function? #1

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

Why use a Function? #2

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.

Why use a Function? #3

We can test a portion of our code,
by isolating it from the rest.
—view source

Why use a Function? #4

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.

Encapsulation

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!

What is an Algorithm?

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?

brush

These came up when we described conditions.

We can use flowcharts to plan and describe

step-by-step procedures

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.

How Do We Use Functions?

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.

Function Calls

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?

Built-In Functions

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,

NEWS FLASH OMG!

You can write your very own functions!

And this is done by first creating a function definition.

Function Definitions

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:

1. Declare Your Function!

For example, in JavaScript:

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: {}

2. Algorithmic Steps

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.

3. Return Statement

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.

4. Function Call

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 !

Let’s See that again… in Python!

Maybe you’ve already guessed it, but…

in Python, the same logic will require
some slight syntactical adjustments…

Declare Your Function!

(Python Style)

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 ;

Indentation is Everything

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…

brush

Parameters vs. Arguments

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…

A Parameter

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

Wait, what?!

An Argument

Okay, haha, not that kind of argument…

An 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!

So, what’s the difference?

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.

More info: parameters vs. arguments

So, to summarize:

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.

Finé

Now, time to do the labs…