Functions - Joe Bryan Computer Science Department

Download Report

Transcript Functions - Joe Bryan Computer Science Department

COSC 1223 Computer Science Concepts I
Joe Bryan
What is a function?
 Like a mini-program that performs a specific task
 Two types:
 Built-in functions
 Functions you write
 A function can be called or executed one or many
times within a program
 For this class, Python function names should always
begin with a lower case letter and be named
something that relates to what the function does
Creating Functions
 You have already seen Python built in functions:
 print()
 input()
 There are many more
Function Mechanics
 Functions that you write must be defined
def main() :
 ‘def’ means you are defining a function
 Always included parenthesis () and a colon : in the
definition
 Indention matters – All lines indented after the
definition are included in the function
Defining a function
 The first line tells the computer that the block of code
that follows is to be used together as a function
 Start with: def followed by your function name main
followed by a pair of parentheses () followed by a colon
:
def main() :
block line 1
block line 2
block line 3
Calling Programmer Created
Functions
 Works just like calling a Python internal function
def main() :
print(“Hello World”) # Python internal function
instructions()
# Programmer created function
# instructions() – Prints the instructions on how to use
this program
#
def instructions() :
print(“To use this program, do the following:”)
print(“Blah Blah Blah”)
main()
Parameters and Return Values
 Parameters are values you provide or pass to the
function
 You have already seen this with input() and print()
input(“Please enter a number and press the return key”)
print(“Hello World”)
Receiving Information Through
Parameters
 Sometimes parameters are called “arguments”
 Your function may need the argument to do its work
def displayTextWithEmphasis(message) :
newMessage = “*** “ + message + “***”
print(“newMessage)
Returning Information
 Functions can return information in a return
statement
def main() :
myNum = giveMeFive()
print(“the giveMeFive() function returns: “, myNum)
def giveMeFive() :
return 5
main()
Comments
 You can add comments to your program that help
annotate or explain what the program is doing
 Comments are ignored by the Python interpreter
# Program Author: Lady Gaga
def main() :
myNum = giveMeFive() # Get the number 5
print(myNum)
# giveMeFive() always returns the number 5
def giveMeFive() :
return 5
Exercise #1
 Write a function named printNameAndAge() that
takes the parameters ‘name’ and ‘age’ and prints them
as below. In this example the name and age passed in
would be ‘Lady Gaga’ and 96
Name: Lady Gaga
Age: 96
Exercise #2
 Write a main function that prompts the user for a
name and age and then passes the name and age to
printNameAndAge()
Exercise #3
 When squirrels get together for a party they like to
have cigars. A squirrel party is successful when the
number of cigars is between 40 and 60. Write a
function called squirrelParty(cigarNum) that takes a
number (cigarNum) and returns True if the number of
cigars is between 40 and 60, otherwise it returns False.
Exercise #4
 Write a function called allowedIn(you, date) that takes
two parameters, ‘you’ and ‘date’. The parameter ‘you’ is
the stylishness of your clothes in the range 0 – 10.
‘date’ is the stylishness of your date’s clothes in the
range 0 – 10. The result is your chances of getting a
table. Check the parameters in this order - if you or
your date are 8 or more, then the return result is “yes”,
you get a table, no matter what Otherwise, if either of
you are 2 or less, the result is “no”, you don’t get a table.
Otherwise the return result is “maybe”.
Exercise #5
 Write a main function that asks the user for two
numbers. “Please enter the first number” “Please
enter the second number”.
 Write another function called getSum(firstNum,
secondNum). The getSum() function should add the
two numbers together and return the sum to the main
function.
 The main function should print the result.