Week 6-12 Flashcards
How do pythons built in functions perform?
Functions like print or input, perform some action and we don’t have to worry about how they work. We just need to know what input to give them and what values they provide. They are reusable, we don’t need to type in a bunch of code to print out different things.
What is the first step in implementing a user defined function?
After we have designed our function the first step in implementing it is to define the function. defining the function tells python what to do when you type the function name later on.
How would defining a user defined function look?
What are the parameters of a function?
The variables that a function accepts are called parameters. In the example below, animalName and animalSound are parameters.
What are arguments in a function?
Arguments are specific values like “a cow” and “moo” that are substituted into the parameters.
How would it look to provide arguments to a custom function?
What is the process of creating functions?
Creating functions follows the same pattern that we have already looked at: define, design, implement and test.
In the define phase create an IPO chart.
In the design phase use pseudocode or flow charts.
Some flowchart software uses a special symbol for function calls, in this course we will treat it like any other pseudocode.
Give an example of an IPO chart for a conversion function from feet/inches to centimeters.
Give an example of pseudocode chart for a conversion function from feet/inches to meters/centimeters.
Recall the function and function call:
def imperialToCentimeters( feet, inches ): totalInches = feet * 12 + inches totalCentimeters = totalInches * 2.54 return totalCentimeters
heightInCentimeters = imperialToCentimeters( heightInFeet, heightInInches )
In our program why didn’t we use the variable totalCentimeters in our main program? Why save it into heightInCentimeters?
Because variables defined inside a function can only be used inside that function, similarly you can’t access variables defined outside the function either. This is whats refered to as Variable Scope.
What is a Variable Scope?
Variable scope refers to being able to access a variable from a particular part of code.
Variables defined inside a function can only be used inside that function. Similarly you can’t access variables defined outside the function.
Though there are some exceptions, generally speaking variables can only be accessed within the function they are defined in.
How do you properly debug and trace with functions?
Each function and the main module should be put in their own trace table. I recommend starting with main and then adding the other functions only when they’re first called.
Make a new column in the table each time the function is called since the values don’t carry over from the last time it was called.
in the heightConversion.py example how would you trace the main code section?
in the heightConversion.py example how would you trace the function impoerialToCentimeters?
in the heightConversion.py example how would you trace a second call for the function impoerialToCentimeters?