Learning Python – Functions

Day 9 – 19 of the #100DaysofCode Challenge!

Let’s Talk About It!

Calling Lady Gaga GIF - Find & Share on GIPHY

Functions

I now understand a function as a tool that is used to run a block of code when it is called. If you’re new to Python (like me), this may make more sense further down in this post.

Writing a function

My first task was to learn how to write a function, it was very simple. Defining functions will always begin with the word def followed by the function_name, then parentheses and a colon.

For example:

An indented line of code below the function will perform the relevant operation. I wrote three print statements inside of the function as a part of this greeting.

The print statements will run anytime we call welcome_customers() and will produce this output onto the console:

This was the first big spark of joy I felt doing exercises with Python, it was fun and I played about with writing functions all day.

Write a function, promise you you’ll feel better!

Whitespace

Whitespace has pretty much been the bane of my coding journey and the main reason as to why my code keeps on generating errors.

So what is it you ask?

Thinking Think GIF by Nick Jonas - Find & Share on GIPHY

Basically, the amount of whitespace tells the computer what is and what isn’t part of a function. If a block of code has a dissimilar level of indentation in each line within a function, the computer will generate an error. I may have written that in a way that’s more confusing than it is so here’s an example:

As you can see, the last print statement is outside of the function as it does not have the same level as indentation as the other two print statements. When I try to run the program it will return with an error.

Parameters

Para Para Parameters! I loved learning about Parameters!

Parameters are variables that can be passed into a function.

In this exercise, I used the function example above and inputted the formal parameter, menu_item.

Python assigns menu_item to the parameter data “jungle fries”.

When the function is called the statements will be printed:

Multiple Parameters

A great thing about functions is that they can take in more than one parameter, hence the subheading, multiple parameters!

Now here’s an example of an exercise I completed using numbers!

Positional + Keyword Arguments

I completed an exercise on keyword and positional arguments using my combined knowledge of writing and calling functions and using multiple parameters:

Positional Arguments are arguments that are assigned depending on which position they’re in, in the function call. In the example below, in the function call favourite_place, “Valletta” is assigned to the parameter city as it’s first and “Malta” is assigned to the parameter country because it is second.

Keyword Arguments are used within a function call. The equals sign (=) is used to define what each argument is assigned to.

Returns

A result from a function that can be stored in a variable which can be changed and/or used later by using the keyword return.

So this is where things started to get tricky. I still don’t clearly understand the returned function but I know how to use it, if that makes sense? Maybe I should head over to YouTube University.

I’ve still managed to fish out an example for you:

Tweet me with your favourite song (@MillennialRach)!

Multiple Return Values

I also learned that we can return multiple values!

Here’s an example of a task I completed via Codecademy:

Scope

Variables need to be defined in order for it to be accessed. Variables can be defined outside of a function and inside of a function.

I still struggle with the scope of python variables! My suggestion is that you head over to YouTube university with me and learn more.

I unfortunately still get the occasional NameError: name ‘my_var’ is not defined!

Every day’s a school day!

So that’s the end of my learning! Once I completed these exercises it was time to start the project.

PROJECT – Getting Ready for Physics class via Codecademy

This was my second coding project! Completing the project made me feel like I was making progress with learning how to code and I was no longer an amateur, cheers to that!

The objective of the project was to provide students with functions that will help them calculate some fundamental physical properties.

I began with converting a temperature in Fahrenheit to a temperature in Celsius. I completed this by writing a function, that takes in an input and returns a value in Celsius using an equation provided by codecademy.

I also wrote another function and used the steps above to convert Celcius into Fahrenheit.

Then, I tested the functions, and they didn’t generate any errors, woohoo!! If there’s one thing I’ve learned during my coding journey… TEST YOUR FUNCTION! It will save you so much time and it’s good housekeeping.

At this stage, I’ve been able to use my knowledge of writing functions, parameters, whitespace and returns.

Further down in the project, I; defined more functions, used multiple parameters, print and returned functions and keyword arguments, in order to complete the tasks set by codecademy. Along the way, I made sure to test the functions.

Aaand thennnnn, I completed project!

As simple as it may sound, I did have a few minor challenges along the way. All coders from newbies to very experienced will have regular run-ins with SyntaxError – during this project, I was generating this error due mainly because I kept forgetting to put a colon: at the end of the defined function.

Another stumbling block I ran into was not understanding what the task was asking me to do, which left me stuck.

Below is an example of the task, I had to ask for the solution in the end because terminology such as ‘get the force’, was not used throughout the exercise so it left me confused:

I try not to use the solution button during my learning journey as I want to challenge myself to figure out different ways to complete the task. However, as a newbie, I’m still learning, and if I didn’t figure out a task now, I’ll know better the next time around.

So if you’re getting stuck problem-solving or completing coding challenges/exercises, don’t be too hard on yourself! Beyonce wasn’t built in a day.

Thanks for having a read! For those of you familiar with the movie Good Burger, I hope the references amused you!

If you’ve spotted any errors, you know where to find me, @millennialrach (on Twitter).

Leave a Reply

Your e-mail address will not be published. Required fields are marked *

Social media & sharing icons powered by UltimatelySocial