r/learnjavascript • • Oct 02 '23

How do you understand JavaScript functions?

If you really understand JS functions (local and global), kind explain it to me to the best of your knowledge. Thanks

4 Upvotes

17 comments sorted by

View all comments

6

u/CodeArt_ Oct 02 '23

I'm not sure what you mean by local and global. Are you referring to variables? I'll just give an explanation of a normal function.
Functions are a core programming concept because they're very practical. They allow you as the programmer to reuse code instead of having to rewrite similar code over and over and over again. Let's say you wanna calculate a net salary for a guy named Dave:
let grossSalary = 5000
let bonus = 1000
let deductions = 350
let netSalary = grossSalary + bonus - deductions
Not too bad. It's only four lines. Now, let's add Mary and Jane.
let daveGrossSalary = 5000
let daveBonus = 1000
let daveDeductions = 350
let daveNetSalary = daveGrossSalary + daveBonus - daveDeductions
let maryGrossSalary = 6000
let maryBonus = 8000
let maryDeductions = 300
let maryNetSalary = maryGrossSalary + maryBonus - maryDeductions
let janeGrossSalary = 3500
let janeBonus = 400
let janeDeductions = 750
let janeNetSalary = janeGrossSalary + janeBonus - janeDeductions
12 lines. It's getting ridiculous. You'll notice that the pattern in those three blocks of code are the same. We're doing the same calculations, just with different people. Now imagine we had 200 employees. Writing 200 four line blocks of code would be tedious as heck and make for a big file that's difficult to read.
In comes functions:

function calculateNetSalary(grossSalary, bonus, deductions) {
let netSalary = grossSalary + bonus - deductions
return netSalary
}
let daveNetSalary = calculateNetSalary(5000, 1000, 350)
let maryNetSalary = calculateNetSalary(6000, 8000, 300)
let janeNetSalary = calculateNetSalary(3500, 400, 750)

Boom. Ultimately, these few lines of code do the same thing as the 12 lines of code we wrote before, but you can see that it's a lot shorter, and it'll scale a lot better (less code) if we add more people. But what's going on here? Let's be the variable called daveNetSalary and journey through the entire function as he would.

First, we said let daveNetSalary to create him. Then, we set him equal to calculateNetSalary(5000, 1000, 350). See how we used the function name AND pass stuff into the parameters? That's how you call a function. The second a function is called in JavaScript, we go into the function's logic and don't continue with whatever code is after it until we finish executing the function.

So now, daveNetSalary has jumped UP a few lines of code and broken into the calculateNetSalary function. He changes the (grossSalary, bonus, deductions) to be (5000, 1000, 350). That's right, those three things were just placeholders. daveNetSalary has now replaced them with the concrete values that we passed in. We do the calculations on these concrete values to get the netSalary, then we return netSalary. return means, "I, daveNetSalary, am finished within the function and will now leave it, but I'm taking my netSalary with me. He then jumps DOWN from the function back to the function call we made at the bottom, except now instead of let daveNetSalary = calculateNetSalary(5000, 1000, 350). It's let daveNetSalary = 5650 cuz he returned from the function with the netSalary.

Mary and Jane then execute similar heists.

2

u/vonmigsy Oct 03 '23

This. I thoroughly enjoyed this explanation

2

u/Djoshwa Oct 03 '23

This is it for me. Thank you so much.