Functions

What is a Function?

Functions allow you to reuse code

  • Performs a specific task
  • Begins with the keyword function followed by the function name and parentheses ( )
  • Parameters or arguments will be placed inside the parentheses ( )
  • Braces { } follow the parentheses ( )
  • Your instructions (code block) will be inside of the braces { } 
  • The return keyword exits a function
  • You must call or invoke the function for it to run the specific task

The Structure of a Function

  • Keyword function starts the function
  • greeting is the function name
  • phrase is the parameter 
  • return is the output of the function
  • greeting('Aloooooooha') is calling/invoking the function to run the specific task

Parameters and Arguments

  • A parameter is a piece of information (a placeholder) that the function needs
  • An argument is a piece of information that is passed from a function call into the function

Information that the function needs

Return Values

The value that the function returns

Scope

Access to your variables based on location

  • Global variables can be accessed throughout your entire program
  • Local variables can be used only inside the function in which they are declared

pppractice!

Made with Slides.com