Objectives

Students will be able to:

  • understand that a function is a module of code
  • understand that a function can be defined/implemented
  • understand that a function has to be called to be executed
  • understand that a function can have any number of parameters passed into it
  • understand that a function must return something, or it is not truly a function
  • translate program code to pseudocode and vice versa

Functions

An environmental scientist is doing some important work for Planet Earth.

He takes 10 pollution values from a river and performing a complicated calculation on the value:

pollutionIndex = (value+2563)/value3

dangerLevel = (polutionIndex/32.1)2

result = dangerLevel/10+pollutionIndex

He wants to store the results in an array.

He has written a little program using his rather rudimentary Python skills:

#create a global variable to store the processed data

arr = []

for i in range(10):

data = double(input("Enter a value")

#call a function,send it the data and assign the returned value to a variable

x = processValue(data)

#append the data into an array

arr.append(x)

What does the function do?

It takes the data that is passed to it, does some calculations and returns the result!

def processValue(value):

pollutionIndex = (value+2563)/value3

dangerLevel = (pollutionIndex/32.1)2

result = dangerLevel/10+pollutionIndex

return result

Think!

This module is passed some data as a parameter from a computer program.

It processes the data!

It returns something to the computer program when it has finished.

Try writing this program if you are not sure about it. Don't copy and paste. Try to do it yourself.

A module that returns something is called a FUNCTION

How functions work

Tags

module function return identifier parameter pseudocode return typedefinecalldata type