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

67 lines
1.9 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# Function Parameters
2016-04-02 01:22:47 +02:00
How to use parameters to pass info in and out of an function.
2016-03-26 00:47:20 +01:00
### @parent js/function
2016-04-13 17:27:45 +02:00
A [function](/js/function) can have multiple input parameters and/or a single output parameter. The parameters must be one of the supported variable [types](/js/types).
2016-03-26 00:47:20 +01:00
2016-04-13 17:27:45 +02:00
When you first [create a function](/js/function), it looks like this:
2016-03-26 00:47:20 +01:00
```
export function doStuff() {
}
```
### Add a function parameter
1. Open your script (if needed) and then click `script` in the upper-right corner.
2. Under **code** click your function name.
3. Click the function name in your code (this opens the function panel).
4. Click **add input parameter** or **add output parameter**. The parameter is added to your function.
#### Input parameters
2016-04-13 17:27:45 +02:00
The default type for an input parameter is [Number](/reference/types/number):
2016-03-26 00:47:20 +01:00
```
export function oneInput(p: number) {
}
```
2016-04-13 17:27:45 +02:00
To change the default type, click the type ([Number](/reference/types/number) in this case) and change it to [String](/reference/types/string), [Boolean](/reference/types/boolean), or [Image](/reference/image/image). You can add multiple input parameters to a function.
2016-03-26 00:47:20 +01:00
#### Output parameter
2016-04-13 17:27:45 +02:00
the default type for an output parameter is [Number](/reference/types/number):
2016-03-26 00:47:20 +01:00
```
export function output() : number {
let r: number
return 42
return r
}
```
2016-04-13 17:27:45 +02:00
To change the default type, click the type ([Number](/reference/types/number) in this case) and change it to [String](/reference/types/string), [Boolean](/reference/types/boolean), or [Image](/reference/image/image).
2016-03-26 00:47:20 +01:00
### Inputs and output function
The following sample function has two inputs and one output parameter (all are the Number type):
```
export function inputsAndOutput(p: number, q: number) : number {
let r: number
return p + q
return r
}
```
### See also
2016-04-13 17:27:45 +02:00
[call a function](/js/call), [create a function](/js/function), [return](/js/return)
2016-03-26 00:47:20 +01:00