pxt-calliope/docs/reference/js/code.md

32 lines
1.0 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# function
2016-04-02 01:22:47 +02:00
A function with inputs and outputs.
2016-03-26 00:47:20 +01:00
### @parent js/language
To add a **functions** to your script, click the `script` button, then click the `+` `add new function` button
### functions
A **function** takes [inputs](/microbit/actionparameters), runs code and (optionally) returns an output.
TouchDevelop functions are similar to `mathematical functions`. Consider the function that computes the square of `x`: `square(x) = x*x`. In code, it would look like this:
```
export function square(x: number) : number {
let result: number
return x * x
return result
}
```
### private function
An function can be marked as **private** in the properties. A private function is not visible outside a library (if the script is a library)
### documentation
The comment(s) at the beginning of a function used to provide a description of its purpose. This text will show in the help area when the function is called from the code editor. This is particularly useful for [libraries](/microbit/libraries).