Hints for Challenge:
- Is
||variables:word||
accessible outside of||functions:goodbye||
? - How is the behavior of
||functions:goodbye||
different from||functions:helloWorld||
? Is||variables:word||
part of that difference?
In previous lessons, ||functions:functions||
have been shown to
have a wide variety of uses, with many different features.
For those reasons,
we will maintain a particular format for ||functions:function||
comments.
This way,
anyone who wants to learn about the function will have a consistent and easy way
in which to identify these features,
without having to go back and forth looking for what the different aspects of the function are.
The first functions discussed in the Intro Lesson were fairly basic; they accepted no input, and did not produce any output.
The ||functions:sayHello||
function is a simple demonstration of this;
whenever it is called, it will ||game:splash||
“hello” to the person playing the game.
To add a comment to a function like this, simply describe in a short sentence what the function is actually doing.
||functions:sayHello||
/**
* Displays a greeting for the player to read.
*/
function sayHello() {
game.splash("hello");
}
sayHello();
Notice that the comment does not go into much detail on the behavior - saying the exact greeting, or how it is done. If someone wants to know these things, they can read the code itself.
Even if the function were changed to display different text in a slightly different way (as done below), the same comment could still be used to describe the function.
/**
* Displays a greeting for the player to read.
*/
function sayHello() {
game.showLongText("Welcome to our new Adventure! You will have a good time here!", DialogLayout.Bottom);
}
sayHello();
||functions:introduction||
function||functions:goodbye||
function||variables:word||
show up in the comment for ||functions:goodbye||
?/**
*
*/
function introduction() {
game.splash("Hi, my name is Bob!");
}
/**
*
*/
function goodbye() {
let word = "Goodbye!";
game.splash(word);
}
To comment on a parameter, start the line with @param
, followed by the
parameter’s name, and then a short description of what the function will do.
For example, a section of a comment describing a parameter count
of type
number
that determines how many times a value will be printed could be commented on as
/**
* Prints "Hello!" to the console a given number of times
* @param count the number of times to print the phrase
*/
||functions:printMessage||
/**
* Prints a message
* @param message the message that will be printed
*/
function printMessage(message: string) {
console.log(message);
}
printMessage("Hello");
printMessage("World");
||functions:printMyValue||
function||functions:goodbye||
function/**
*
*/
function printMyValue(value: number) {
console.log("" + value);
}
/**
*
*/
function moveMySprite(mySprite: Sprite) {
mySprite.x += 20;
}
Return values are incredibly important; if they are not documented properly, then oftentimes the behavior of the function will be entirely unpredictable.
To document return values, start the comment with @returns
,
and then give a short description of what the value returned represents.
Generally, we will put the @returns
annotation after any parameters
for the functions we describe.
/**
* Prints "Hello!" to the console a given number of times
* @param count the number of times to print the phrase
* @returns the number of characters that were printed
*/
/*
* Prints out 5
* @returns the number requested
*/
function highFive(): number {
console.log("5");
return 5;
}
If the function’s only purpose is to return a value,
and nothing else that will affect the person calling it is done,
then you can omit the short description of the function itself
and include only the @returns
.
/*
* @returns the number requested
*/
function lowFive(): number {
return 5;
}
||functions:coinFlip||
function||functions:greetAndBill||
function||functions:sayMyName||
function/**
*
*/
function coinFlip(): boolean {
return Math.percentChance(50);
}
/**
*
*/
function greetAndBill(): string {
game.splash("Have a good day!");
return "Bill";
}
/**
*
*/
function sayMyName(name: string): string {
game.splash("Your name is " + name);
return "my" + name;
}