Creating Your Own Functions
In Review
1. Introduction
You have already used functions that were included with Python. You can also define your own functions and use them yourself or give them to others to use. Functions allow us to "encapsulate" code into nice "reusable" chunks so that you don't have to write the same code over and over again.
2. Celsius to Fahrenheit Conversion
The equation to convert from Celsius to Fahrenheit is: DegreesF=(9/5)*DegreesC+32. While this is not a really complicated function, it would be a pain to remember every time we use it. We can put it into a function to make it easy to have around in the future.
Defining functions in Python is very easy. The code below defines the function and then calls it with some values to make sure it is working properly. Try it yourself and then write the function for converting Fahrenheit to Celsius and test it as well. Why did we need to add the ".0" to the numerical values?
def CelsiusFahrenheit(TheCelsiusValue): TheFahrenheitValue=(9.0/5.0)*TheCelsiusValue+32.0 return(TheFahrenheitValue) DegreesFahrenheit=CelsiusFahrenheit(0) print(DegreesFahrenheit) DegreesFahrenheit=CelsiusFahrenheit(100) print(DegreesFahrenheit)
The "return()" at the end of the function tells Python what value to return as the result of the function. This is why we can put the function call into a "print" statement.
3. Passing Parameters
You can pass as many parameters into a function as you like. When you call a function, the value of the first parameter that is passed in will be assigned to the first parameter in the function, the value of the second parameter will be assigned to the first parameter in the function, and so on. So the order of the parameters is important. However, the names of the parameters does not need to match. The example below is of little value but it allows you to test how parameters are passed. Try it and then add additional parameters until you are comfortable with passing them into your own functions.
# Define the function to concatenate two strings def ConcatenateStrings(FirstParameter,SecondParameter): TheResult=FirstParameter+SecondParameter return(TheResult) # Call the function a few times to see how it works NewValue=ConcatenateStrings("Hello","World") print(NewValue) NewValue=ConcatenateStrings("Hello"," World") print(NewValue)
Notice that the function is defined before it is called. This is required by Python.
4. Stepping "Into" functions
Set a break point in the code above and "step over" to the first line after the function. Next to the "step-over" button, you'll see a "step into" button. Push it now. A red bar should appear at the first line of your function. This shows that you have "jumped" into the function. Now "step-over" until you return from the function. The value you place in the "return()" function call will be the "result" of the function and passed back to the calling code. Sometimes you'll want to "step-over" a function because you believe it is working properly, if you think the function might have a problem "step-into" it.
While you are in a function, check the "Stack Data". You'll see that the variables you are using within your function are in the "locals" section while those outside the function are in the "globals" section. These are two different "scopes" and your function can access the globals but your "locals" will disappear when you leave the function.
Important: As your programs grow in size, it is much easier if you do not use globals within your functions. This will make sure that your functions completely "encapsulate" their operations and are not "dependent" on any other code. This is a part of "Structured Programming".
5. Function headers
Each function should be preceded by a header that contains the purpose of the function, a description of each of the inputs to the function and any restrictions on the values of the inputs, and any outputs from the function. Additional information can include an overview of how the function operates and what other effects the file may have. Below is an example function header.
################################################################# # Converts a Latitude/Longitude pair into a UTM coordinate in a specified UTM Zone. # The UTMZone and South must be provided and can be obtained by calling the functions # below with the Latitude and Longitude. This allows UTM values to get determined # for zones outside the default zone. # # Inputs: # Longitude - Coordinate in the East-West direction from -180 (Grenich England) back to 180 # at the same location # Latitude - Coordinate in the North-South direction from -90 (south pole) to 90 (north pole) # Datum - One of the constants from the properties above # UTMZone - Zone for the desired coordinates. Can be found with GetUTMZoneFromLonLat() below # South - True if in the southern hemisphere, false otherwise. Can be found with GetSouthFromLat() below # # Outputs: # Easting - Horizontal UTM coordinate within the specified zone # Northing - Vertical UTM position given the specified value of South # #################################################################
Additional Resources
Python Documentation: Defining Functions