Game Development Reference
In-Depth Information
Use comments wisely and as you see fit. It is quite legitimate to write an entire
program without comments; however, be warned, when you come back to
code several months later you'll be glad you did.
1.5.3 Functions
A function is a block that bundles a number of lines of code together to
form a specific operation. It contains prewritten code that can be reused
over and over again, saving programmers the stress of reinventing the
wheel each time they write a program. Programmers can also write their
own functions.
Whenever the author thinks of functions, she visualizes them as food
processing machines. You put vegetables in, they get processed,
and you get chopped, mashed, or minced vegetables out.
The most common newbie function taught is print() or a derivative thereof.
This function takes input and prints it to the screen. For example,
print(“Hello Unity”);
will print
Hello Unity
on the screen or console.
Functions available to you as a programmer will depend on the programming
language. JavaScript has numerous standard functions but also takes on
added ones depending on the context. For example, when JavaScript is
embedded into the HTML of a webpage,
document.write(“Hello Unity”);
will display the text on the webpage when viewed inside a browser.
To define your own function in JavaScript, the format is that shown in
Listing 1.8 .
Listing 1.8 Code Syntax for an Empty Function
function myFunctionName(input list)
{
}
A function declaration begins with the keyword function, followed by its name
and then a list of input values.
While functions are a programming element usually introduced to new
programmers further down the track, they are fundamental to writing
JavaScript code for Unity and therefore are being explained up front.
 
Search Nedrilad ::




Custom Search