Getting Better The Hard Way
Functions
The things I am not doing well at I hope to study and smooth over to prior to graduating my course. The concept of functions makes sense when saying it out loud but hasn’t quite grasped me in the way that I would want it to so I will be utilizing this blog post to really drive it home. As this is more of a personal blog post there will be some technical information in it.
Functions themselves are essentially a variable within coding that does things that you input. So, a “reusable chunk” of code. The emphasis of this is to lessen your line of code as you can often just reuse your function throughout your program. Ok seems simple enough
First thing you want to do is DEFINE or def your function ensuring your variable name that follows is easy enough to understand.
above I defined my function as “learning” because that is what I am doing here. What next? You have to give your function “Parameters” parameters are the input you define for your function so WHAT AM I LOOKING FOR. That is what I would use in the parameters here. Lets say I am looking for a First Name and a Last Name what would that look like?
My parameters into my “learning” variable would be
learning(first_name, last_name)
I have my parameters !! woot I have told my function what I want to look for EVERYTIME I CALL ON the variable “learning”
However, I need arguments that happen. Arguments are the actual values that your parameters are looking for so the argument for this function would be
learning(‘Jalen’ , ‘Charles)
in my parameters I want to find a first and last name EVERYTIME I call the word learning. So when I call learning(I simply input a first name and Last name) So far our code looks like
as you can see I told my function what I wanted to print prior to running the parameters ‘first_name’ and ‘last_name’. I wanted to say “What’s up” (some flavaaaa)
Great I have written a function that will call first and last names ANY TIME I
- Call on my Variable ‘learning’
- ‘insert my arguments’
- bam
That wasn’t so hard however, there are two types of functions :(
- ) That performs a task, ex. learning(first_name, last_name) :
— — — — print({first_name}, {last_name})
this is performing the task of printing whatever arguments I input
2.) A function that calculates and returns a value
What is the difference you ask? Don’t worry I asked the same thing.
A function that performs a task is LOCKED into that task. So in my first example no matter what I want to do with the first and last name I will not be able to do anything with it other than print the first and Last Name
What if I wanted to store the name that was returned in another variable? That is where the second function comes in. Lets see an example
as you can see here I have basically done the exact same thing as before with the exception of the word “return” vs “print”. Return in the most simple way for my understanding means show me the results. Once you have that result you can store that in a variable like this..
math has been changed to the variable ‘eleven’ with the results of my math function as 11
Getting a tad bit more into functions the reason you may want to store your results as a variable is to save it, put it into an email do math with it etc.. but the result of the function is a value so your limits are limited only by you lets look at this example of what happened when I store my ‘math’ function into variables and then proceed to do “math” with those variables
This is Great!!! Now I have the basics down. Lets get into the Hard Stuff
Iteration
iteration is defined as the repetition of a process over a given amount of values. So, if I had the numbers 3,4,5 and I wanted to iterate the sums of these number + 3 I would get back 6,7,8. now how do I add that process in a function? Working in data science we will sometimes be dealing with an extreme amount of values we wouldn’t go one by one to do a sum of something so we are going to create a function that iterates over all of your points.
lets begin.
same context as before I defined my variable however this time I gave it a PARAMETER of multiple numbers the * in my parameter next to numbers allows me to put in as many numbers as I want.
For any number in the numbers which is my parameter print that number + 2
so whatever numbers I pass into my function as arguments they will automatically be getting added with an additional 2.
For any number in the numbers which is my parameter print that number + 2. (emphasizing) for understanding
Things to note about the above iteration. Keep in mind of where your indentation is. the first indentation is what I want my function to do the second is what I want that particular function to do.
Ahh Take a breath. I now have a better understanding of functions of course they get more complex but understanding the basics and what I am looking at helps out a lot I hope you understand functions a bit better now as well.