Site Loader
What is a function?
defining functions A function is an activity or purpose natural to or intended for a person or thing. dance function Oops. Wrong function! In JavaScript functions can be described the same way. Functions are intended for a specific thing and typically are created to perform the same task a few times.
There Are Functions All Over
In the real world functions are things that help us perform our day to day tasks. If you take a look at out bodies, our legs have the function of helping us walk. Our mouths have the function of helping up talk. Our ears have the function of helping up hear, etc. In the programming world a function is a chunk of code that will help our program perform specific tasks, typically more than once You probably call you parents or friends multiple times a day. Do you type their phone number in each time and hit the call button every time you call them or do you save their number and just press call when you want to talk to them. Hopefully the latter. This is what the tasks of calling someone would look like using a function versus not using a function.
Type in the Phone Number Each Time and Hit the Call Button Every Time
Let’s say you call someone 3 times in the day. This is what you would have to do each time if you don’t have a function.
function main() {

// Call 1:

Type 9
Type 7
Type 3
Type 1
Type 2
Type 3
Type 1
Type 2
Type 3
Type 4
Press call


// Call 2:

Type 9
Type 7
Type 3
Type 1
Type 2
Type 3
Type 1
Type 2
Type 3
Type 4
Press call

// Call 3:

Type 9
Type 7
Type 3
Type 1
Type 2
Type 3
Type 1
Type 2
Type 3
Type 4
Press call

}
This what you would have to do each time if you use functions.
Find the Saved Number and Hit Call
function callMom() {

Type 9
Type 7
Type 3
Type 1
Type 2
Type 3
Type 1
Type 2
Type 3
Type 4

}

function main() {
Find the person you want to call

// Call 1:

callMom()

// Call 2:

callMom()

// Call 3:

callMom()

}
You’re doing the same thing in both scenarios, but having a function makes your life easier especially if you have to do something more than once. There’s less repetitive code, it’s easier to read, and which is something you aim for as a good programmer
Defining a function
To define a function you use the keyword(special word) function followed by a name of the function, parenthesis and a block {} /section that contains the code.
//Version 1
function callMom() {
console.log("MOOOOMMM");
}

//Version 2 - The function is entered as a string ' <THIS IS NOW A STRING> '
const callMom= new Function('console.log("MOOOOMMM");')

//Version 3 - The function is entered as a string ' <THIS IS NOW A STRING> '
const callMom = function() {
console.log("MOOOOMMM");
};

These are the basics of defining functions. Functions can have return values and parameters/arguments. Think of this as inputs. We’ll take a deeper looking at function arguments and parameters in another post. If you like this article or would like a specific topic explained drop a comment below. I would love to hear your feedback!      

Vania Nettleford

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts More From Author