What’s Coding Without Functions?

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?

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

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

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

Aspiring Programmer.