Func%ons
How can we use func,ons to break up a program into chunks that are easy to change and understand?
Warmup
• What is be3er, a house with one giant room, or a house with many different rooms? Why?
Why func%ons
• Programs get big and complicated – break them up into smaller pieces – easier to write and fix
• A func%on is a chunk of code that does something (a specific task)
Wri%ng func%ons
• Start with the keyword def (for define) • Name the func%on • Then put parentheses () • Then put a colon : • Example
• Indent all the code in that func%on
Using func%ons
• Func%ons don’t run un%l you “call” them – Use the name of the func%on followed by () – Example – Where have we seen this before?
Where have we seen func%ons before?
• Codingbat – What we wrote on codingbat were func%ons, to be used by other programs
– How did we produce results from these func%ons?
• Code.org – What are the func%ons? – What are the func%on calls?
10.1_myaddress.py
• Make a func%on called printmyaddress() which will print your proper address on three lines. Then call the func%on. Follow the example
10.2_ini%als.py
• Write a func%on to print your ini%als in big le3ers, like this:
• Then call the func%on
Func%on Arguments
• Pieces of informa%on you “pass” to a func%on when you call it
• Go inside the parentheses of the call
• Func%on has to “receive” the arguments • A variable goes inside the parentheses of the func%on define line.
Mul%ple arguments
• Can have many arguments in a func%on, separate them by commas
Func%on Arguments
• Without argument
• With argument: more flexibility!
Func%ons and programs
• Func%ons are separate from programs – “Black box” – Programs don’t know what’s happening in func%ons at all
Returning a Value
Returning a value
• Func%ons can return a value to the program – In the func%on have a return line at the end – This value can be printed, saved to a variable, etc.
– In the example, the func%on calculates the price with tax, puts it in the variable taxtotal, and then returns taxtotal to the program that called it
Local vs. Global variables
• Variables in func%ons are local and exist only while the func%on is being run
– In this example, price, tax_rate, and taxTotal are local variables
• Variables in programs are global and stay in memory throughout the program – Global variables can be used but NOT changed in a func%on
10.3_calculatetax.py
• Write the following func%on
• How can the func%on be called? – Print it
– -‐ it to a variable
10.3_calculatetax.py
• User input
10.4_addtwo.py • This program sums 2 numbers and prints them
• Instead of finding the sum in the program, write a func%on to find the sum – Pass the two numbers num1 and num2 to the func%on – The func%on will add the two numbers – The func%on will return the sum – The program will call the func%on, assign it to the variable numSum
10.5_anyaddress.py
• Make a func%on that will print an address with user input – name, address, street, city, state, or zip in the USA.
• (It needs 6 arguments-‐ collect and pass them individually or as a list)
10.6_change.py
• Write a func%on to calculate the total value of some change—quarters, dimes, nickels, and pennies . The func%on should return the total value of the coins. Then write a program that calls the func%on. The output should look like this when it runs: