Created
January 3, 2020 00:24
-
-
Save jfrites/b49a54b28d8ab583e71d1a0407735cfa to your computer and use it in GitHub Desktop.
Functions Examples and Samples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//instructions for function will only run when you invoke it | |
function imAFunction () { | |
console.log("hello world"); | |
} | |
//invoking a function | |
imAFunction(); | |
//----------------- | |
// parameters & arguements the example bellow prints the console.log statement with each name | |
function hello(name) { | |
console.log("hello " + name); | |
} | |
hello("Jason"); | |
hello("David"); | |
// defined a new variable (should use the parameter defined in the function majority of the time) | |
let thing = "world"; | |
// passed new variable into hello to make it print Hello world | |
hello (thing); | |
//multiple parameters passed into a function | |
function firstLast(first_name, last_name){ | |
console.log("hello " + first_name + " " + last_name); | |
} | |
let first = "Gaby"; | |
let last = "Frites"; | |
firstLast(first, last); | |
//----------------------------- | |
// return sends the value back to the place where you called it | |
function plus(a, b) { | |
let sum = a +b; | |
return sum; | |
} | |
function multiply(a,b) { | |
return a * b; | |
} | |
console.log(plus(10,5)); | |
let x = 3 + plus(10,5); | |
console.log(x); | |
let y = multiply(plus(10,5), 2); | |
console.log(y); | |
//modulous % will equal 0 if it has no remainder otherwise will return the remainder | |
function isEven(x){ | |
if (x % 2 === 0) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
let num = 9; | |
if (isEven(num)){ | |
console.log(num + " is Even"); | |
} else { | |
console.log(num + " is Odd"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment