What’s Coding Without Functions?

Gedler Jean Pierre
5 min readJun 1, 2021

Journal Entry:

If there’s one thing that is faster than getting an error in Javascript, it most certainly is time. It feels like I just created this blog to showcase my journey into the tech world and here I am learning my second language in code already. If there is one thing I learned about coding and going into different languages, it is, some things stay the same. It is easier to grasp the concepts because you’re familiar with things from the previous language. You know… the small stuff like variables, objects, arrays, etc. Every human language has words but the way we pronounce those words may be different. The same goes for coding languages, same concept just a different way of executing it. I am currently learning Ruby and I am able to pick up on it because of my foundation from Javascript. Guess coding wasn’t as scary after all. If there’s one big topic I want to talk about that will be useful to any aspiring programmer, it is the topic of functions. The reason I say this is because I see that is very abundant in the coding world. Let me better explain…. when I first started learning Javascript, my code wasn’t dynamic until I started using functions. As I move on and begin to explore different languages, I can’t help but notice the use of functions being a core building block.

What is a function?

Can you imagine a world without any functions? We’ll basically just be living to stare into existence until our fate is called. We need functions in order to take care of daily tasks, no matter what we’re doing. The same can be said about coding. We can create our fancy variables, structures, and practically everything we mentioned in the last post, but we need functions to put it all together! Functions give our code life! so, let’s put some life into our code and dig into functions.

A function is an object that contains a sequence of Javascript statements. We can call on functions and execute them multiple times as the point of them are to perform a task. To call a function means to run the independent pieces that make it. To better elaborate, think of functions as an abstraction. What is abstraction? Abstraction is the process of taking things and giving them a collective name. Abstractions that hold work are called functions. So if you made a bunch of variables to help you carry out a task, think of that as just having pieces to the puzzle. Using all the necessary variables in a function will make you finish the puzzle! Let’s take a look at the example below:

const name = "Gedler" 
team = "Knicks"
function aboutMe() { <--
console.log("${name} is going to the ${team} game")
}
aboutMe();// This will return "Gedler is going to the Knicks game"

Notice how I was able to call on global variables that were outside the function. I was able to use these global variables due to using string interpolation, which is the reason for the dollar sign, followed by the brackets. This could a good way of thinking of abstracting; I made two variables for the purpose of containing information of what I planned on doing. I then put the pieces together in a function, so I could call on those variables to perform the tasks of printing out what I was doing. The collective name in this sense is, “aboutMe”, the name of the function.

Syntax/Declaring A Function

Let’s talk about the syntax of a function so we can know the proper way to write it! In javascript, to declare a function you must start off by writing the word function followed by the name you would like to call it. In my example above, I decided to call my function, “aboutMe”. It is recommended that your function names start with a lowercase letter, practically, the same way how you would write your variable names. After declaring the name, you must pass in parenthesis and these parentheses are used to store parameters. We will touch on this later into the post. Lastly, put an open bracket and from there, you want to start applying your logic or using your variables. Once you’re done writing your logic, you put a closing bracket. This is the correct way to open and close your function.

function exampleOne { <-- open bracket, you can start applying logic//apply logic here 
} <-- closing bracket when you're done writing your function.

Parameters/Calling On A Function

In my “aboutMe” function, the variables I used are considered global variables because they weren’t between the opening and closing tags of the function. This is evidence that you can use global variables inside your functions and you’ll be just fine. But what if… you only wanted certain variables to be existent only inside your function? What if you wanted your function to be more dynamic? This is where parameters come into play.

Parameters are locally-scoped variables that are defined inside the function. This means, those variables are only accessible inside your function and you can’t use them anywhere else! Let’s take a look of how we can use parameters

function aboutMeV2 (name , team) {
console.log("${name} is going to the ${team} game")
}

We simply just put the name of the variables inside the parenthesis and use them the same way how we would with any other global variable. However, we have a problem. The parameters we created are undefined as they have no values. How can we pass in the values of the parameters after declaring our function? The answer is to call on the function and pass in the values in the same order you created your parameters. When you pass in values when calling your function, you are sending arguments. Let’s take a look:

function aboutMeV2 (name , team) {
console.log("${name} is going to the ${team} game")
}
aboutMeV2("Gedler" , "Knicks" );
//this will return "Gedler is going to the Knicks game"

To call on a function, you simply just have to call on its name on a new separate line. Even if you have no parameters, you still need to pass in the parenthesis, it’d just be blank in that case. If you made a parameter, you pass in the values in the same order. Let’s see what happens when we swap the arguments. It is an argument because I am passing down a value for the parameter.

function aboutMeV2 (name , team) {
console.log("${name} is going to the ${team} game")
}
aboutMeV2("Knicks" , "Gedler" );
//this will return "Knicks is going to the Gedler game"

This is why it is important to pass in the arguments in the same way you made your parameters so Javascript knows which values are for what.

There are many different ways we can use functions and they’re even different types of functions with how we can write them. It’s all going to come down to what you’re building inside your app. I have provided a resource so you can dig into functions a little more and get your feet wet! https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions

Conclusion

Functions are useful in the coding world. It allows programmers to be abstract and perform different tasks that need to be done. Different languages may have different ways of how they utilize functions but the core features stay the same, especially locally scoped variables. As programmers, we strive to make our code more and more dynamic so functions will be our best friend or nightmare. But we always wake up from nightmares, right? :) Keep coding!

--

--