Functions Flashcards
Ways to call a function
the 2 you know
Regular Mode: myFunction()
Fucntion as Object Property: myObject.myFunction()
Ways to declare a function
4 ways to declare a non-method function
3 ways to declare a method
(there are more ways)
DECLARATION 1. Regular function declaration function myFunction(variable) { return variable + 2; }
EXPRESSIONS 2. Function expression let myFunction = function(variable) { return variable + 2; }
3. Arrow function let myFunction = variable1 => { return variable + 2; } (with or without parenthesis for 1 variable) or let myFunction = (variable1, variable2) => { return variable + 2;
- anyonymous
(function() {
code
})()
(function() {
code
}) - CREATES THE FUNCTION
() calls it
~~~
METHODS - as a method
let anObject = {
apple: function() {
}
~~~
2. let anObject = { apple() { console.log('yes'); } } 3. let anObject = { apple: x => { console.log('yes'); } }
How to have a default parameter
How does default parameter interact with myFunction.call()?
myFunction(arg = 41)
myFunction.call() doesn’t allow default parameters for some reason.
How do you ask for input:
- in a browser
- in console
let rlSync = require('readline-sync'); let name = rlSync.question("What's your name?\n");
let name = prompt("What's your name?"); console.log(`Good Morning, ${name}`);
Make a random number generator
let randomizer = (min, max) => { return Math.floor(Math.random() * (max - min + 1) + min); }
console.log(randomizer(2,3))
Write a recursive function to return the factorial of the argument
function factorial(number) { if (number === 1) { return 1; }
return number * factorial(number - 1);
}
Write a recursive function to return the Fibonacci sequence of all the numbers smaller than the argument
function fibonacci(number) { if (number < 2) return number; // 0 if number is 0, 1 if number is 1 return fibonacci(number - 1) + fibonacci(number - 2); }
Write a recursive function to make an array of numbers counting down from the argument (2 ways)
console.log(countdown(4)); function countdown(n) { if (n < 0) { return [] } else { let myArray = countdown(n-1); myArray.unshift(n); return myArray; } }
console.log(countdown(4)); function countdown(inputNum) { if (inputNum <= 0) { return [0]; }
return [inputNum, countdown(inputNum - 1)].flat();
}
What is the order of thought to write a recursive function?
Does it hold true for the 3 recursive functions you know?
- Think about what the repeating thing is
- What is the last thing that needs doing?
- What is something that needs doing only once or otherwise considered for?
What does this log? let apple = 5 function boop(input) { console.log(input) input = 2 console.log(input) } boop(apple) console.log(apple)
Why?
5
2
5
Variables as parameters in functions are locally scoped
Can you use a function to change a variable like: let myfunction = variable => { variable = 123; } ?
Can you use a function to change an object?
No when you function AFunction(apple) { apple = 5; } let pear= 1; AFunction(pear); pear; // 1
the function does this:
apple = pear;
With primitives it’s creating a new value.
With objects however, they point to the same object and can be changes
What is a callback function
A callback function is a function passed into another function as an argument
What is alerted? let userName = 'John';
function showMessage() { userName = "Bob";
let message = 'Hello, ' + userName; alert(message); //1 }
alert( userName ); //2
showMessage();
alert( userName ); //3
// John before the function call // Hello Bob // Bob, the value was modified by the function
What is alerted? let userName = 'John';
function showMessage() { let userName = "Bob";
let message = 'Hello, ' + userName; alert(message); }
showMessage(); //1
alert( userName ); //2
// Hello Bob // John, unchanged, the function created a local version of userName and did not access the outer variable
Can you pass a function as a default parameter?
Yes
What are some ways to have a default value in the event a value is created but undefined
(3 ways other than the typical way)
- inside the function write something like:
if (value === undefined) - text ?? ‘empty’
- text || ‘empty’
What happens with "return;": let aFunc = input => { console.log('asfs'); return; } console.log(aFunc('dfs'));
How can this be used?
asfs
undefined
The empty return returns nothing (undefined)
It can be used to “break” from a function (break only works with loops)
for example the alert won’t show below:
function showMovie(age) { if ( !checkAge(age) ) { return; }
alert( "Showing you the movie" ); // (*) // ... }
What happens with this: { return (2 + 4) }
js assumes a ; after return because it’s a new line
It returns undefined
7How should functions be named and created?
3 considerations
- They are actions, they should be verbs:
Function starting with…
“get…” – return a value,
“calc…” – calculate something,
“create…” – create something,
“check…” – check something and return a boolean, etc.
- Then some noun they act upon or create "getForm" "calcPI" "createAddress" "checkDate"
- one action, one function, If you want a function that does multiple things, create multiple functions and another function that calls those functions.
What happens when
myFunction()
is presented like this: myFunction
?
in browser shows the function code.
in visual studio shows the name of the function.
What happens in the numbered steps?
function sayHi() { // (1) create
alert( “Hello” );
}
let func = sayHi; // (2) copy
func(); // Hello // (3) run the copy (it works)! sayHi(); // Hello // this still works too (why wouldn't it)
Did it copy the function or does it point to it?
The Function Declaration
(1) creates the function and puts it into the variable named sayHi.
Line
(2) copies it into the variable func. Please note again: there are no parentheses after sayHi. If there were, then func = sayHi() would write the result of the call sayHi() into func, not the function sayHi itself.
(3) Now the function can be called as both sayHi() and func(). (a pointer like with objects and arrays)
Both just point to it. Functions are not primitive.
Assign a function to another variable
let func = sayHi;
How do you setup a function as an argument?
function aFunc(arg1) { arg1( ); }
How do you pass an anonymous function as an argument?
ask( "Do you agree?", function() { alert("You agreed."); }, function() { alert("You canceled the execution."); });