Skip to content

Instantly share code, notes, and snippets.

@rogerwschmidt
Last active June 9, 2017 17:47
Show Gist options
  • Select an option

  • Save rogerwschmidt/d4d822dc3c8e12325b83d0eb9f7fa069 to your computer and use it in GitHub Desktop.

Select an option

Save rogerwschmidt/d4d822dc3c8e12325b83d0eb9f7fa069 to your computer and use it in GitHub Desktop.

Introduction to Functions Instructor Notes

Objectives

  • Explain the purpose and function of a Function
  • Write Functions
  • Write Functions with 0, 1, and 2+ parameters
  • Invoke Functions as Expressions

Explain the purpose and function of a Function

Turn to your neighbor and explain why functions are useful. Be prepared to share your answer with the class.

Write Functions

On your desks, write a function that prints out the numbers 0 - 9.

Write Functions with 0, 1, and 2+ parameters

On your desks, write a function named printIfDivisible that takes two parameters:

  • Name first parameters countUpTo.
  • Name second parameter divisor.

Your function should loop from 0 to and include countUpTo, and only prints out numbers in the count if they are evenly divisible by divisor

Example:

printIfDivisible(10,2) // 0, 2, 4, 6, 8, 10

printIfDivisible(10,3) // 0, 3, 6, 9

printIfDivisible(20,5) // 0, 5, 10, 15, 20


On your desks, write a function named sumAll that will take 0 or more arguments, and return the sum of all arguments.

Example:

sumAll() // 0

sumAll(10) // 10

sumAll(1,2,3,4,5,6) // 21

Invoke Functions as Expressions

The following equation is an equation that gets the length of a hypotenuse of a right angle, where a and b and the lenghts of the sides at a right angle.

c = sqrt(a^2 + b^2)

Using the following functions:

  • Math.pow()
  • Math.sqrt()

And the following variables:

var a = 3;
var b = 4;

Write an expression that creates a variable named c and assign the value of the length of the hypotenuse of a right angle with sides if length a and b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment