Functions

From CodeStuff
Jump to: navigation, search

Most programming languages have functions of some sort. The essence of a function is it is a bundle of program behaviour so your program can do that behaviour when you want it to.

There are a lot of functions available to you. print, drawLine, fillCircle, getMousePosition, and keyIsDown are all functions that have been supplied already. You can also make your own functions.

In javascript the simplest possible function is

  function () {}

This function has no name, and it does nothing with nothing. Functions with no name are called Anonymous functions.

To give a function a name you just put the name after the word function.

 function useless () {}

This function is called useless. It lives up to it's name because it still does nothing with nothing.

What a function does when you call upon it to perform is the code between the { } brackets.

 function heya () { print("hello") }

This function is called heya and when called it does print("hello").


To get a function to actually do something you have to call it to tell it to do its job.

When you call a function you can give a function something to work with. You attach names to these things by placing them in the ( ) brackets when you make the function.

This makes a function called greet and it uses personName to refer to whatever it was given to work with. and when we call it we give it the text "Jimmy".