Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.
Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.
For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.
It makes sense to start learning by reading and watching videos about fundamentals and how things work.
Data Science and Machine Learning - 16 wks
Full-Stack Software Developer - 16w
Search from all Lessons
Curated list of small interactive and incremental exercises you can take to get better at any coding skill.
Curated section of projects to build while learning with simple instructions, videos, solutions, and more.
Guides on different topics related to the technologies that we teach in our courses
Mastering the use of functions is one of the 5 fundamental skills of building algorithms:
Basically, a function is a bunch of code that you can run anytime whenever you decide. For example:
By reviewing this code, we can obtain the following conclusions:
defwe have to write the name we want for the function (in this case, "multiply").
,. We can pick the name the parameters are going to have (in this case,
b), but they must always be in the same order.
:, that way the computer will know we are about to start coding the insides of the function (the function algorithm).
From now on, I have the function
multiply available on my code, and I can re-use it as many times as I need to multiply two values like this:
📺 Click here to watch a short video explaining functions (9min)
This function calculates the cost of organizing a party with the following criteria:
Nonebut you should override this and always return something useful. In this example, we want to return the result of a & b multiplication.
The whole idea is to have a library of hundreds of functions and use them as we please. You declare all your functions, and later you start using and re-using them all the time.
Coding is very abstract, and it happens a lot that you have no idea what you wrote yesterday. Before functions existed, algorithms were this huge never-ending series of lines of code where developers would have a hard time and get lost. It is hard for your brain to follow a procedure/algorithm of great length; the more lines of code, the more abstract it becomes.
By using functions, you have the following advantages:
If you think about it, functions are the equivalent of books. They store knowledge and ways of doing things, and in future developments, you just re-use them instead of having to figure everything out all over again.
All functions must start and end somewhere, that is called the scope of the function. You can delimit the boundaries by using the correct indentation after the colon
: like this:
☝ Any variables that you declare inside the function will not be available outside of it.
☝ It is very important to remember that once you use the
returnstatement the function will stop executing. If there is any code after that statement, it will never be executed.
If your function is going to be a one line function, you can use the "lambda" trick to be more agile. You have to use the reserved word
Lambda is ideal for cases in which you have little functions, you will learn to love it because it makes your code faster and shorter, specially when you are working with lists.
The only way to use (a.k.a: call) a function is to use parenthesis like this:
Please remember to assign the function whatever parameters it should receive. In our example, the multiply function was declared asking for two numbers to multiply. Play with the following example as you like:
You can combine functions however you want and have chained calls like this:
The following code has 3 functions declared:
As you can see, the function names are pretty specific about what the functions do, as well as the parameters assigned to them.
Other important things to notice:
get_averageis to get the average value on a given array. It knows nothing else, and that is great! By separating your code into little functions, you can focus on one thing at a time.