Difference between revisions of "Functions"
(Created page with "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 i...") |
|||
| Line 21: | Line 21: | ||
<edcode> | <edcode> | ||
// this program makes a function called heya, but we never use it so it never prints anything. | // this program makes a function called heya, but we never use it so it never prints anything. | ||
| − | function heya () { print("hello") } | + | function heya () { print("hello"); } |
</edcode> | </edcode> | ||
| Line 27: | Line 27: | ||
<edcode> | <edcode> | ||
//Make the function heya | //Make the function heya | ||
| − | function heya () { print("hello") } | + | function heya () { print("hello"); } |
//Call the function | //Call the function | ||
| Line 35: | Line 35: | ||
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. | 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. | ||
<edcode> | <edcode> | ||
| − | function greet (personName) { print("hello "+personName) } | + | function greet (personName) { print("hello "+personName); } |
greet("Jimmy"); | greet("Jimmy"); | ||
</edcode> | </edcode> | ||
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". | 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". | ||
Latest revision as of 08:14, 7 May 2015
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".