What is a Function?

Kyle Coberly

Functions

  • Performs tasks or calculates values
  • Are sub-programs
  • Reduce repeated code
  • Accept multiple¬†inputs, generate one output

What is a function?

Functions Perform Tasks

What is a function?

Tasks & Values

login();

changeTextColor("red");

generateByline(firstName, lastName);

What is a function?

Tasks & Values

Functions Calculate Values

var sum = addNumbers(2, 2); // 4

var firstName = "Kyle";
var lastName = "Coberly":
var fullName = joinStrings(firstName, LastName); // "Kyle Coberly"

What is a function?

Subprograms

Functions Are Subprograms

var firstNumber = squareNumber(4);
var secondNumber = squareNumber(5);

console.log(firstNumber + secondNumber);
function squareNumber(numberToSquare){
    var squaredNumber = numberToSquare * numberToSquare;
    return squaredNumber;
}

What is a function?

Reduce repetition

Functions Reduce Repetition

var bingo = 4;
var bango = 6;
var bongo = -1;
var calculatedNumber = 0;

if (bingo > bango){
    calculatedNumber += bingo;
} else {
    calculatedNumber += bango;
}

if (bango > bongo){
    calculatedNumber += bango;
} else {
    calculatedNumber += bongo;
}

if (bongo > bingo){
    calculatedNumber += bongo;
} else {
    calculatedNumber += bingo;
}

console.log(calculatedNumber);

Before

var bingo = 4;
var bango = 6;
var bongo = -1;
var calculatedNumber = 0;

function greaterNumber(firstNumber, secondNumber){
    if (firstNumber > secondNumber){
        return firstNumber;
    } else {
        return secondNumber;
    }
}

calculatedNumber += greaterNumber(bingo, bango);
calculatedNumber += greaterNumber(bango, bongo);
calculatedNumber += greaterNumber(bongo, bingo);

console.log(calculatedNumber);

After

What is a function?

Multiple Inputs, One Output

Functions Combine Multiple Inputs Into One Output

Functions

  • ________ tasks or ________ values
  • Are ____________
  • Reduce ________ ____
  • Accept ____ ______, generate ___ ______

What is a function?

Functions

  • Performs tasks or calculates values
  • Are sub-programs
  • Reduce repeated code
  • Accept multiple¬†inputs, generate one output

What is a function?

What is a Function?

By Kyle Coberly

What is a Function?

Javascript function basics

  • 478
Loading comments...

More from Kyle Coberly