Objectives

Students will be able to:

  • understand that a function definition/signature can include parameters
  • understand that when calling a function, the call must match the parameter list.

Global Variable Solution

def calculateTax():

if salary>20000:

tax = salary * 0.25

if salary>10000:

tax = salary * 0.125

else:

tax = 0

print(f"Salary:${salary} ; Tax: ${tax}")

def main():

global salary

numStaff = int(input("How many staff will you process: "))

for i in range(numStaff):

salary = int(input("Enter a salary: "))

calculateTax()

#program calling the function

main()

Parameter Solution

def calculateTax(salaryData):

if salaryData>20000:

tax = salaryData * 0.25

if salaryData>10000:

tax = salaryData * 0.125

else:

tax = 0

print(f"Salary:${salaryData} ; Tax: ${tax}")

def main():

numSalary = int(input("How many salaries will you enter: "))

for i in range(numSalary):

salary = int(input("Enter a salary: "))

calculateTax(salary) #call a function and give it some data to process

#call main

main()

Intro to parameters

functions sharing data

 

Intro to Parameters

Parameters allow functions to share data.

We can define and function to process parameters eg:

#this function adds two numbers and displays the result

def addTwo(num1, num2):

print(f"{num1} + {num2} = {num1 + num2}")

Then, when needed, we can call the function:

def main():

addTwo(7,2)

addTwo(0,-2)

addTwo(13,4)

#call main

main()

Getting started

Copy the starter code into your IDE and complete the worksheet.

Review

Parameters allow functions to share data. They do this by using parameters.

When Function A passes data to Function B, Function B uses its parameters to grab the data and then it can process the data!

This strategy reduces the need for global variables.

Starter Video

Parameters allow functions to pass data to each other.

This reduces the need for global variables.

Watch the video to help you get started.

Tags

module function return identifier parameter pseudocode return typedefinecalldata type