In tutorial one, I explained the concept of an object in software programming. If you read tutorial one, you will remember that I broke an object down into 2 parts.
2. Functions (AKA Methods)
An object stores data and functions interact or change the data. Based on these two categories, an object has a current state and behaviors to change its state.
The data of an object, defines its current state.
The functions of an object act as behaviors an object is capable of doing by changing the current state its data.
Real World Functions vs. Traditional f(x) Functions vs.Python Object Functions
This tutorial will be focused on functions in python. First, we will cover what a function in the real world. Then, we will take what we already know from simple middle school mathematics and transfer f(x) functions to python syntax. After that, we will take our understanding of functions and create functions using python. Finally, we will apply what we learned from general python functions to more thoroughly understand their purpose inside of a python object.
Defining Functions in the real world
First off, what is a function? Meaning, how do we as humans use functions in the real wold – not just mathematically or computationally. Let’s start by considering the definition.
A function is an activity or purpose that is natural to or intended for a person or thing.
Okay, that helps us understand the purpose of functions a little better.
What do functions do in the real world:
- Definition 1(Noun): A function is an activity natural to a person or a thing.
Did you catch that? A function is an activity natural to a person or thing. If a person partakes in the activity of running,
we can define this as a functionality of that person. This would mean a function is synonymous with behavior. And behavior just happens to be one of our two characteristics that we said defined a software object. Let’s continue with some more real world examples.
Synonyms for function: purpose, task, use, duty, activity, job, mission, undertaking, obligation, etc…
Use of a function in the real world: Bridges perform the function of providing access across water.
- Function Definition 2(Verb): Work or operate in a particular way
Note this part: To work or operate in a particular way. A function is responsible for a specific purpose and carries out this purpose in a particular way. This definition is extremely close to the mathematical definition, but we can still find real world examples where this makes perfect sense.
Example: Her liver is functioning normally.
In this example, the human liver has a particular job/responsibility to do. A human liver’s main job is to filter the blood coming from the digestive tract, before passing it to the rest of the body. A human’s blood health can be healthier/cleaner at some points in times and unhealthy/contaminated at others points in time. Blood is a characteristic of a human, with a current state that changes over time.
Let’s bring the concept of functions round circle and take a closer look on how you would describe human as an object in computer programming. 1st, we create a python program and make a “human object”. We need to define the characteristics of a human as well as human behavior that changes the current state of these characteristics.
Human Characteristics: skin, hair, blood, teeth, etc…
In computer programming, these human characteristics are represented by data types with specific values. The values can change over time. The current value given to represent the state of a human characteristic is known as the state of the object.
So, to keep it simple, let’s use human characteristic – blood health. We’ll use an integer number to represent blood health. The value of that number is the current state of our human object’s blood health. It can go up or down – healthier or unhealthier. The up or down functionality of blood health is caused by the liver cleaning the blood.
Functions, representing a behavior, are used to change an object’s state. This gives our object functionality, otherwise known as behavior.
Let’s take the following example:
The human body is an “object”.
Blood is represented with a number as a datatype of integer. This value represents the “current state” of the blood cleanliness.
Our Liver is represented by a function. When executed, this function changes the integer number of blood.
Putting it all together
A human object’s blood value needs to be changed from its current state to a new state so the body passes the blood into the liver as input. Thus, the action/behavior defined by our liver function cleans the blood, and then blood is output with a new value.
Our human object’s state(blood) was changed by our objects behavior(liver function).
If you were to continue to expand on our human object, you would create data types for other human characteristics such as skin, hair, teeth, height, location etc… as well as defining functions to give our human other behavior such as running.
Don’t worry if you don’t completely understand this concept quite yet. I just wanted to put the concept of object states and behaviors into your head. But, we’ll definitely go over them in much more depth later on. For now, let’s expand our understanding of functions.
f(x) Functions in Traditional Arithmetic
f(x) = (x + 3)
In this example, the syntax is pretty simple.
1. f() defines a function, which means we have an input and an output.
If we give x a value of 1.
then f(x) = 4
if x = 34
then f(x) = 37
Arithmetic f(x) functions compared to programming functions
data | x
f(x) | Function name with a single input parameter that it will interact with.
(x + 3) | Defines what we do with the data that was input to our function. In this case, a python function would execute x + 3.
Thus, f(x) represents data being input into a function. Above, the traditional mathematical function outputs the data + 3.
Again, In object orient programming an object stores its data and the functions able to interact with this data together. This is what makes it an “object”. The object’s functions change its data structure(aka behavior changes its current state).
One last time….Object’s encapsulate both the data defining what the object is AND the functions that change the data values. This is one of the primary principles of OOP, and we will learn much more about that later on, but for now I want to refocus on functions that are not owned/encapsulated by an object. This means you can call the function from anywhere in your program.
For the remainder of this tutorial, we are only going to talk about functions that are NOT encapsulated by objects, meaning the functions are executable without needing to reference any object. .
Python Functions and an Introduction to Scope
So, now we have a pretty good all-around understanding of what an object does. But, how would one actually implement this idea into python? Glad you asked:) For those who already understand python syntax, feel free to skip below to variables and scope of function variables.
Before I display the python syntax, I want to reiterate an important point. The logic behind a software function does not differ from the logic of a traditional math function.
The only differences are simple syntax rules and understanding how python stores/scopes/returns variables:
1. Syntax We Use in Python
– Python Reserved Keyword def: lets the computer know that we are about to define a function.
– functionName: is the name of our function.
– dataInputParameter: This is our functions input parameter. A function may have 0 or multiple parameters.
– Also notice that our line that defines the function and its parameters is ended by a colon(:). Directly below that line, we define the
purpose of the function. Specifically, how does it interact with the input parameter and what does it do with the output value found after
– In the example directly below, we only comment – showing where the function interacts with the input data but not actually changing anything.
– Python Keyword Return: In this scenario, we simply return the same value we used as our input parameter(# are comments in python).
def functionName( dataInputParameter ):
### How Function Interacts With dataInputParameter ###
Python Functions And The Return Statement
File name: functionExamples.py
Goal: Create a Function that adds 3 to the input data/variable and return the output. Then display output found from function to our screen.
Step 1: Create python function to add 3 to an input parameter.
def addThree( parameter ):
parameter = parameter + 3
Step 2: Instantiate a python variable to store the integer value 4
x = 4
Step 3: Execute Function inputting x as the parameter. We want to create a location in memory so we can store the output of our function’s return value. So we instantiate a new variable,
functionOutput. functionOutput stores the return value of the function.
functionOutput = addThree(x);
Step 4: Output value stored inside of functionOutput. We do this by using functionOutput as the input parameter for python’s predefined print() function
Python Function Scope, Variable Storage, and Variable Types
The code above is pretty simple if you understand some basic python syntax. That being said, there is a lot going on behind the scene that isn’t very complicated and will immediately improve your tech skills if you understand. It all has to do with how python stores data in memory.
Programming languages have several different data types.
Variables are defined with a data type based on what is called the “data type“.
Some data types:
I tried phrasing the explanations in a way that it would help you to understand how the computer stores data inside of variable “types”.
What is a data type, though?
To us, as humans, a data type can be thought of semantically. Basically, humans are able to consider data types by giving different types abstract meaning that we have learned throughout the entirety of our lives. We just intuitively know the difference between a string data type and an integer data type. One makes words and the other can be used to to arithmetic.
We use all of our experiences through out life to give meaning to these data types.
Computers on the other hand distinguish between data types in a much simpler way – size. To a computer, all data types are made of bits. The only thing that distinguishes data types is how many bits each data type holds. Thus, data types simply define variables of different bit sizes. Variables are just containers, and the data type is simply the size of containers.