Super lost about d.r.y


#1

Hey guys i did the correctly for D.R.Y by looking through these questions and answers. Can somebody explain to me exactly how functions work....im super confused !!!!


#2

Might contain traces of spoilers :smile:
Functions are some sort of sub-programs that aren't directly executed. You just write them somewhere in your code and call them whenever you need them (which also means that you can call them multiple times). So the first and simple approach is to wrap some task in this structure:

var name = function(){
    here goes the code that you want to execute;
}

and start the execution by the use of name() anywhere in your code (for this version after the actual definition ...). But function can do even more than that. As they are some sort of sub program they have their own variable name space so if you declare a variable inside of the function it is only known there and any try to use the variable outside would result in a reference error (local variables). On the other hand you can use any variables in the function that are declared outside of it (global variables). With a small exception when you redeclare variables with the same name e.g.

var test = 3;
var test2 = 4;
var name = function(){
    var test = 5;
    console.log(test); --> prints 5
    console.log(test2); --> prints 4
}
name();
console.log(test); --> prints 3

As you can see this creates 2 variables (one inside and one outside) that are not connected. As this makes the otherwise global variable unaccessible from inside the function it is called shadowing as the outer one is in the shadow of the inner one.

But apart from the extended name space there is even more interesting stuff about functions that is really useful and that has mainly to do with the input and output of the function. So far it was just a collection of commands but with parameters you can make it more interactive because each new function call may differ from the one before:

var name = function(para){
     console.log(para);
}
name("test"); --> prints "test"
name("michael"); --> prints "michael"
name("haxor"); --> prints "haxor"

So what happens is that by using a parameter (just a variable name between the () of the function) you create a local variable that is special in the way that it receives its value from the function call. As you can see the value in the () of the function call (called argument) is assigned to the parameter and so the parameter name can be used as an alias for this value (just like a normal variable). So this covers the input but what about the output? Of course you can use console.log but this outputs to the user via the screen but what about outputting to the program itself?
This is the job of "return". Where the function call starts the sub program return ends it and "returns" back to the main program giving you two neat features, the first is that you can set the return at any place in the function so you can do something like this:

var name = function(para){
  if(isNaN(para)){ //exit here if para is not a number
       return; 
  }
  var result = do interesting calculation;
  return result;
}

as you can see the first return skips the function early in case the input is not correct and the second one is also interesting because it returns a value to the function call meaning that the function call is replaced with this value e.g.

var square = function(x){
    return x*x
}

var squareOfThree = square(3);
console.log(squareOfThree);

So the value computed by the function can be used just like any other regular value.

So I guess that should be enough input for one post and it probably spoils some of the later exercises of this course (sry for this) but I hope it gives you a little insight of what functions are and why they are useful. if you have any questions left feel free to ask.


12. Functions & if / else issue
Help With Functions
Question 9
Why do I need this function? LESSON 10
#3

Thanks a lot for this post! I'm starting to grasp what function does in detail!


#4

any topics for return ?


#5

The last part is about return or what do you mean?