FAQ: Functions - Helper Functions

I get it because I just finished scope lesson.

1 Like

HI! I have some questions about functions
it’s the same if I write

function plus1(x){
return 1++;
};
and
plus1= function(x){
return 1++;
};

and what happen when I invoke two functions at the same time
Example
function plus1(x){
return 1++;
};
let square =function(x){
return x * x
};

square(plus1(x))

They actually happen in sequence. First the plus1() is executed and its return value is then passed to the square() function. What’s important is that x is a defined value:

x = 6
console.log(square(plus1(x))

Hi, I continue having problems using functions, Could you help me?

if i write
function multiply(num1,num2){
return num1 * num2;

why I have to write
console.log(multiply(4,5))

it’s not enought using
multiply(5,6);

in what cases I have to use both

Not if you wish to see the result logged. It is only the return value that is actually logged after it’s been computed.

HI Mtf! thanks for all yours aswers!
anyone knows what happend with my code?

lifePhase = age => {
switch (age){
case (age >= 0 && age <= 3):
return ‘baby’;
break;
case (age >= 4 && age <= 12):
return ‘child’;
break;
case (age >= 13 && age <= 19):
return ‘teen’;
break;
case (age >= 20 && age <= 64):
return ‘adult’;
break;
case (age >= 65 && age <= 140):
return ‘senior citizen’;
break;
defaul:
return ‘Yhis is not a valid age’
}
}

// Uncomment the line below when you’re ready to try out your function
console.log(lifePhase(5)) //should print ‘child’

// We encourage you to add more function calls of your own to test your code!
console.log(lifePhase())
console.log(lifePhase(20))
console.log(lifePhase(30))

console

undefined
undefined
undefined
undefined

If we have a relational or logical expression in the case, we need a boolean as the switch’s argument.

switch (true) {

}

break is unreachable after return and they can all be removed from the code.

Now ask yourself, what if the age given is a float? How will you account for the gaps in your conditionals?

Does anyone else feel like helper functions are explained poorly? I didn’t know the parameter of a function could be used as an argument of another function until i came here. That should have been touched on before using it in an example. The word ‘celsius’ isn’t even used in the example explanation?

Parameters are simply local variables, and we can use them any way that suits our function objective, including in other function calls whether they be built-in functions or our own custom functions. In the end, it is just a call. The parameters have no magic sauce about them.

const toLower => str {
    return str.toLowerCase()
}

Above we invoke the String method on the parameter.

const firstOccurrence => (sentence, word) {
    return sentence.indexOf(word);
}

Above we invoke the String method on the first parameter, (haystack) with the second parameter as the argument (needle).

In the example, rows,columns is used for both functions. Why is that? If I change those words to something else in one of the functions, the output is the same: 4000. Is there a reason both are the same or is it supposed to make it easier to understand in the example by keeping the words the same? To me it makes me think they have to be the same for some reason.

I understand that it’s a placeholder variable until a value is inputed. But it seems like even if the words are different, it still inputs the values into the first function when called as the helper function in the second function. And I don’t understand why that is.

You use positional parameters and arguments,

so when you call the function:

 return monitorCount(rows, columns) * 200

the first argument matches with the first parameter, and so on

so you can name the parameters of the helper function what-ever you want (in theory, in reality you want to pick logic variable/parameter names)