image credit: By Wvbailey [Public domain], via Wikimedia Commons
image credit: By Wvbailey [Public domain], via Wikimedia Commons
image credit: By Wvbailey [Public domain], via Wikimedia Commons
Code goes here
image credit: By Wvbailey [Public domain], via Wikimedia Commons
Code goes here
Arguments go in
image credit: By Wvbailey [Public domain], via Wikimedia Commons
Code goes here
Arguments go in
Results come out
To get some output, based on some input
To get some output, based on some input
Using the same method each time
To get some output, based on some input
Using the same method each time
So we have predictable outputs from the same inputs
var result = x * x;
f(x) = x * x
f(x) = x * x
Our function takes x in order to get us the answer we want!
f(x) = x * x
function square(x) {
var result = x * x;
return result;
}
image credit: By Wvbailey [Public domain], via Wikimedia Commons
Code goes here
Arguments go in
Results come out
image credit: By Wvbailey [Public domain], via Wikimedia Commons
Code goes here
Arguments go in
Results come out
console.log("Hello from inside the function!");
Statements
image credit: By Wvbailey [Public domain], via Wikimedia Commons
Code goes here
Arguments go in
Results come out
console.log("Hello from inside the function!");
Statements
var x = 5 + 5;
var y = x + 5;
console.log(y);
image credit: By Wvbailey [Public domain], via Wikimedia Commons
Code goes here
Arguments go in
Results come out
console.log("Hello from inside the function!");
Statements
var x = 5 + 5;
var y = x + 5;
console.log(y);
var slideContent = "Any kind of statement!";
image credit: By Wvbailey [Public domain], via Wikimedia Commons
Code goes here
Arguments go in
Results come out
console.log("Hello from inside the function!");
Statements
var x = 5 + 5;
var y = x + 5;
console.log(y);
var slideContent = "Any kind of statement!";
var result = x * x;
image credit: By Wvbailey [Public domain], via Wikimedia Commons
Code goes here
Arguments go in
Results come out
var result = x * x;
image credit: By Wvbailey [Public domain], via Wikimedia Commons
Code goes here
Arguments go in
Results come out
function f() {}
var f = function() {}
Function Syntax
f
image credit: By Wvbailey [Public domain], via Wikimedia Commons
Code goes here
Arguments go in
Results come out
Keyword
function f() {}
var f = function() {}
f
image credit: By Wvbailey [Public domain], via Wikimedia Commons
Code goes here
Arguments go in
Results come out
f
Name the function
function f() {}
var f = function() {}
image credit: By Wvbailey [Public domain], via Wikimedia Commons
Code goes here
Arguments go in
Results come out
f
Add parentheses, to put our inputs in
function f() {}
var f = function() {}
image credit: By Wvbailey [Public domain], via Wikimedia Commons
Code goes here
Arguments go in
Results come out
f
Add curly braces, to define where it starts and ends
function f() {}
var f = function() {}
image credit: By Wvbailey [Public domain], via Wikimedia Commons
Arguments go in
Results come out
function square() {
var result = x * x;
}
image credit: By Wvbailey [Public domain], via Wikimedia Commons
function sayHello() {
console.log("Hello everyone!");
}
function whatIsToday() {
console.log("Well, today definitely ends in y!");
}
function aUsefulFunction() {
console.log("You don't have to be useful to be valuable");
console.log("but it helps.");
}
image credit: By Wvbailey [Public domain], via Wikimedia Commons
function sayHello() {
console.log("Hello everyone!");
}
function whatIsToday() {
console.log("Well, today definitely ends in y!");
}
function aUsefulFunction() {
console.log("You don't have to be useful to be valuable");
console.log("but it helps.");
}
sayHello()
> "Hello everyone!"
whatIsToday()
> "Well, today definitely ends in y!"
aUsefulFunction()
aUsefulFunction()
aUsefulFunction()
aUsefulFunction()
> "You don't have to be useful to be valuable"
> "but it helps."
> "You don't have to be useful to be valuable"
> "but it helps."
> "You don't have to be useful to be valuable"
> "but it helps."
> "You don't have to be useful to be valuable"
> "but it helps."
image credit: By Wvbailey [Public domain], via Wikimedia Commons
Code goes here
Arguments go in
Results come out
function square() {
var result = x * x;
}
image credit: By Wvbailey [Public domain], via Wikimedia Commons
Code goes here
Arguments go in
Results come out
Parameters
function increment(x) {
x = x + 1;
console.log(x);
}
image credit: By Wvbailey [Public domain], via Wikimedia Commons
Code goes here
Arguments go in
Results come out
Parameters
function increment(x) {
x = x + 1;
console.log(x);
}
Multiple Parameters
function difference(minuend, subtrahend) {
var diff = minuend - subtrahend;
console.log(diff);
}
function difference(minuend, subtrahend) {
var diff = minuend - subtrahend;
console.log(diff);
}
Copy this function:
function difference(minuend, subtrahend) {
var diff = minuend - subtrahend;
console.log(diff);
}
Copy this function:
function difference(minuend, subtrahend) {
var diff = minuend - subtrahend;
console.log(diff);
}
Copy this function:
Run it like so:
difference(5,3);
difference(10,5);
difference(100, 200);
Run it like so:
difference(5,3);
difference(10,5);
difference(100, 200);
Results!
> 2
> 5
> -100
image credit: By Wvbailey [Public domain], via Wikimedia Commons
Arguments go in
Results come out
function square(x) {
var result = x * x;
}
image credit: By Wvbailey [Public domain], via Wikimedia Commons
Arguments go in
Results come out
function square(x) {
var result = x * x;
}
square(5);
square(10);
image credit: By Wvbailey [Public domain], via Wikimedia Commons
Arguments go in
Results come out
function square(x) {
var result = x * x;
}
square(5);
square(10);
> undefined
> undefined
image credit: By Wvbailey [Public domain], via Wikimedia Commons
Code goes here
Results come out
function square(x) {
var result = x * x;
}
image credit: By Wvbailey [Public domain], via Wikimedia Commons
function square(x) {
var result = x * x;
return result;
}
The return keyword
Code goes here
Results come out
function square(x) {
var result = x * x;
}
image credit: By Wvbailey [Public domain], via Wikimedia Commons
function square(x) {
var result = x * x;
return result;
}
The return keyword
Code goes here
Results come out
function square(x) {
var result = x * x;
}
Calling our function
square(5)
square(10)
image credit: By Wvbailey [Public domain], via Wikimedia Commons
function square(x) {
var result = x * x;
return result;
}
The return keyword
Code goes here
Results come out
function square(x) {
var result = x * x;
}
Calling our function
square(5)
square(10)
Results
> 25
> 100