Welcome back, lovely people! Today we’re taking a break from our Github For Beginners series (because frankly, it is a lot of steps to write) and we’re switching gears. Let’s talk about something that I think most beginners struggle with… understanding functions. I’m going to assume you’ve read many articles, tried a coding challenge, or followed along with some Udemy course (because haven’t we all). I’m not going to give you tech speak since you are probably overwhelmed with it. But I will explain it like I wish someone had explained it to me. In. Plain. English. Let’s talk about the fundamentals of functions so you can walk a way with a better understanding of functions and how they work.
Before we can understand how a function works we need to understand what it is. We could get technical here but it’s not really necessary. To put it simply, a function does something. It executes a set of steps with whatever information you give it and returns a value or output. It can return different data types and be as simple or as complex as you think to make it. That’s it.
Let’s look at the major components of a function because functions are fundamental. If you think you can get around using them…
Disclaimer: I went back and forth about adding closures to this article because it is important to understand closures when understanding functions. But it deserves its own article.
The best article I’ve ever read about closures is “I never understood Javascript closures.” It might take a minute to wrap your head around but I haven’t heard anyone else explain it better so read it if you’d like!
We have an example below so let’s pick it apart. For this example, I’m using JavaScript but it doesn’t really matter which language you are using because fundamentals don’t change, syntax changes. Are you ready for my super complex function?

Set the scope:
We need to use const or let to set the guidelines for the scope the function will have. Don’t be intimidated by scope. The scope is just where the variables are available for use. So let’s try that sentence again. We need to use const or let to set the guidelines for scope so the function knows where the variables are available for it to use.
The general rule is to use const until you can’t. Using const or let will give it block scope. Using var gives it functional or global scope. You shouldn’t really be using var because there are some caveats, it’s 2022, and you are most likely not working in a legacy codebase but you know to each his own.
We’re not going to dive deep into the differences between const, let, and var because that could take a minute but if you are curious read the article linked in the disclaimer or search on Google. You should find a ton of great articles covering the differences.
Function Name:
Self-explanatory. Try to make it so people have an idea of what the function does just from glancing at the name. But don’t make it too long cause who wants to type that out?
Parameters:
They can be named anything you want, there can be multiples or just one, different data types, other functions, it can get wild.
The placeholders you add here are going to be used by the function in some way and returned. Your function is going to expect that those placeholders be used.

Return:
You can fill a function with all this super cool logic but if you don’t use the keyword return it won’t know to return… well anything. Return makes the logic in the function visible so the values are able to be output.
If you forget to use return you will get a value of undefined when you were expecting actual useful information. So remember to return.

Arguments:
We don’t like to hard code things into a function because the beauty of it is, that if we do it right, we can add many different arguments and not have to change the function itself each time we need to add different values.
As you can see in the example below, I’ve added multiple arguments without having to alter my function at all. The value of the argument will be assigned to the parameter and passed into the function.

Something to keep in mind is that the parameters serve as the placeholder for the arguments. The arguments will be the actual value that gets passed to the parameters.
If you are doing code challenges or writing tests, you’ll get the opportunity to see that even though the arguments give parameters value, the parameters define how many arguments there should be. If my function is expecting a single parameter, then I should be expecting to pass in just one argument.
Example of a Hackerrank challenge where one parameter (an array is expected) and one argument (an array is passed in):


Call The Function:
How many times have you been watching a tutorial and they say, “Let’s call the function”? What does that even mean?!
It means we’re going to write the function name, add the parentheses, and add the arguments inside the parentheses (if there are any). This says, “Run the steps for this specific function and give me the output. Make the magic happen!”.

Conclusion:
And that’s it! That’s a function. Hopefully now you have a better grasp of the fundamentals of functions. So if you come across something like the function below, remember the basics and break it down to really get the sense of what is happening.
// Given an array of integers arr and an integer n, find out a pair of numbers [x, y] from a given array such that x * y = n . If the pair is not found, return null.
const simplePair = (arr, n) => {
}
simplePair([1, 2, 3], 3)
simplePair([1, 2, 3], 6)
simplePair([1, 2, 3], 7)
Want to learn more ways of breaking problems down into easier problems with code? Check out our article here.
Name:
The name gives us a clue that we’re finding pairs.
Parameters:
It takes an array and a number.
Return:
Some way we need to return a pair of numbers but if no pair is found, we return null. So we’ll use return at least twice and some if statements.
Arguments:
We need to give it an array and a number because according to the parameters that’s what it’s expecting.
Call:
Call the function to see what we’re outputting.
Now go practice and don’t be so scared that you take the fun out of functions. Until next time!
Photo by Joan Gamell on Unsplash