pxt-calliope/olddocs/js/return.md

50 lines
1.1 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# Return
2016-04-02 01:22:47 +02:00
Exit a function.
2016-03-26 00:47:20 +01:00
### @parent js/statement
2016-04-13 17:27:45 +02:00
The return statement exits a [function](/js/function) and returns a value to the code that called the function.
2016-03-26 00:47:20 +01:00
### Touch Develop syntax
return *expression*
### Square function
```
/**
* // return the value x * x
* @param x TODO
*/
export function square(x: number) : number {
let result: number
return x * x
return result
}
```
### The type of *expression*
The type of *expression* should match the declared return type of the function; in the above example, the return type is Number and we see that the return expression `x * x` is a Number since the input parameter `x` is a Number.
### Storing the returned value
The following code calls the `square` function with the number 42 and stores the output parameter in the `result` variable:
```
let result1 = square(42)
```
`result` is the default variable name for the function output, as specified in the function
### Lessons
2016-04-13 17:27:45 +02:00
[transformers](/lessons/transformers)
2016-03-26 00:47:20 +01:00
### See also
2016-04-13 17:27:45 +02:00
[function](/js/function), [calling functions](/js/call), [function parameters](/js/functionparameters)
2016-03-26 00:47:20 +01:00