Professional Documents
Culture Documents
Functions (Alt Presentation)
Functions (Alt Presentation)
in Python
Purpose of a Function
• break up code into specific tasks
• make large programs easier to read,
write and debug
• create reusable code
How do Functions Work?
• Block of code which only runs when it is called.
• Must be declared with keyword def before they
can be called (above the code that calls it)
• Can have “parameters”/information passed to
them - used as input to be operated on
• May or may not return a value to the main
program
Built-in Functions we Know
• There are many functions that are built into the Python
language. We don’t see the code for them but we can
use them:
– Ex. print(“Hello.”);
• print function: You send it a parameter “Hello” and it figures
out how to display that on the screen.
– Ex. biggestNum = max(2,5);
• max function: You send it 2 parameters (2, 5) and it sends
you back the larger of the 2 items.
• Some specialized functions are called methods - they
belong to an object.
– Ex. myCircle.draw (win);
• .draw method: is an action that can be performed on a
circle object
Functions that return nothing
• Some functions just do a job, and don’t return
any values to the main program.
• In other languages these may be called
procedures:
Ex. A function that displays a bunny.
Function definition
Function contents
tabbed in
Function call
Functions with Parameters
• Some functions require information
(parameters) to do their job.
• That information can be sent in the brackets.
Simon_says();