Week 7 - Friday CS 113
Last time What did we talk about last time? Software engineering Testing Programming languages
Questions?
Project 2
Functions
Idea of a function Functions allow you to package up some code to run over and over Functions usually take some input (like numbers or text) so that they can be customized Functions often give back an answer (like the square root of a number) Also called methods in some other languages The customizable purple blocks played the role of functions in Scratch
Advantages of functions More modular programming Break a program into separate tasks Each task could be assigned to a different programmer Code reusability Use code over and over Even from other programs Less code duplication Improved readability Each function can do a few, clear tasks
Defining a function Name of 1st argument Function name def name( arg1, … , argn ): statement1 statement2 … statementm Required syntax Name of last argument Code done by function (must be indented)
Simple function example Given two numbers, find the smaller: def smaller(a, b): if a < b: return a else: return b
Value returning functions It is possible to divide functions into two types: Functions that return values Functions that don't return values Functions that do return values give an answer: It's as if the function is replaced by whatever answer is returned x = 3 y = 4 small = smaller(x, y) #small contains 3
Functions that don't return values A function doesn't have to return an answer: This function only prints things to the screen A function that doesn't return anything does some task but doesn't give back an answer def callForHelp(times): for i in range(times): print("Help!")
return statements Like most code in Python, the code inside of a function executes line by line Of course, you are allowed to put if statements and loops inside methods You can also put in return statements A function will stop executing and jump back to wherever it was called from when it hits a return The return statement is where you put the value that will be given back to the caller
Calling functions Defining a function is only half the game You have to call functions to use them Calling a function means giving it the parameters (or arguments) it needs and then waiting for its answer By now, you have done many function calls print() You can call your own functions the same way
Calling functions callForHelp(3) #calls for help 3 times You type the name of the function and then its arguments in parentheses If it's a value returning function, you can store the answer it gives back The arguments you give can be values or variables You have to give the function the right number of arguments or the program will have an error callForHelp(3) #calls for help 3 times result = smaller(9, 2)
Key syntax Every function starts with def Then comes the name of the function Followed by the names of the arguments in parentheses Followed by a colon (:) Like loops and if statements, the code inside a function is indented one level Call the function in other code by typing the name and the correct number of arguments in parentheses
Function example Lets say we want to make a function that will draw three balls stacked on top of each other The bottom one is green, the middle is orange, and the top is red We want the function to take a radius and a position The bottom ball will be drawn at the position with the radius given The middle ball will be above it with half the radius The top ball will be above that with one quarter the radius
Function code Here's the function stacks() which takes a radius and a position and draws the balls def stacks(radius, position): sphere(radius=radius, pos=position, color=color.green) newPosition=vector(position.x, position.y + 3*radius/2,position.z) sphere(radius=radius/2, pos=newPosition, color=color.orange) newPosition=vector(position.x, position.y + 9*radius/4, position.z) sphere(radius=radius/4, pos=newPosition, color=color.red)
Calling the function Now we can call the function several times Each time, it will draw a stack with the size and location we specify Result shown to the right stacks(1, vector(0,0,0)) stacks(.5, vector(2,3,1)) stacks(.2, vector(-2,1,2))
Lab 7
Upcoming
Next time… Moore's law Multicore computers Complex decisions
Reminders Finish Project 2 Reading Python Chapter 7 Due tonight before midnight! Reading Python Chapter 7 Think about what you want to do for your Final Project Proposal due by 3/31