Functions

All programming languages come with a set of built in functions. A built-in function is a block of code that does something for you. This way, you don't have to write the block of code yourself.

Functions are invoked by calling their name.

*English*
Do the function that makes the screen black

*Code*
makeScreenBlack()

Functions can also take literals and variables (in this context called arguments or parameters) to modify the function's behavior. For example, if there were a function to change the screen to a specific color, you would need to tell the function what color to change the screen to.

*English*
Make the screen green

*Code*
makeScreen(green)

Some functions return values. That is, they give you a number or string after the function has executed. You can catch the returned value by assigning it to a variable.

*English*
The current level is 5.

*Code*
level = giveMeFive()

This is what is happening. We'll assume that giveMeFive() returns the number 5. First the function executes and returns it's value :

level = 5

And then 5 is assigned to the variable level.

Some functions can return values and take an argument.

*English*
The current level is 7.

*Code*
level = giveMeFourPlus(3)

If giveMeFourPlus() simply adds its argument to 4, it will return 7 ( 7 = 4 + 3 )

Thats it for basic use of built-in functions. When you actually start programming, you will want to check the documentation for the language to find out what functions there are, what arguments they take, and what value they return, if any.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution 2.5 License.