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

64 lines
1.5 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# Call a Function
2016-04-02 01:22:47 +02:00
How to call a function in your code.
2016-03-26 00:47:20 +01:00
### @parent js/language
2016-04-13 17:27:45 +02:00
Type a function name in your code to call an existing [function](/js/function) in your script.
2016-03-26 00:47:20 +01:00
### Call a function
2016-04-13 17:27:45 +02:00
1. In the Touch Develop editor, click a line of code to open the on-screen [Code Keyboard](/js/editor).
2016-03-26 00:47:20 +01:00
2. Click `code` to see the functions in your script.
2. Click the function that you want to call.
3. Click `store in var` to store the return value in a variable.
### Example: the square function
2016-04-13 17:27:45 +02:00
Here's a function called `square`, with a [Number](/reference/types/number) input parameter:
2016-03-26 00:47:20 +01:00
```
/**
* // returns the square of the input parameter x
* @param x TODO
*/
export function square(x: number) : number {
let result: number
return x * x
return result
}
```
The following code calls the `square` function, passing it an input parameter (`x`), and storing the return value in the `result` variable:
### ~hide
```
let x1 = 2
```
### ~
```
let result1 = square(x1)
```
Or this code, which displays the result of the `square` function (without first storing the value in a variable):
```
basic.showNumber(square(x1), 150)
```
### See all your functions
To see a list of the functions in a script, open the script and then click `script` (in the upper-right corner). All of the functions appear under the **code** heading. Click on a function to open it in the editor.
### See also
2016-04-13 17:27:45 +02:00
[function parameters](/js/functionparameters), [create a function](/js/function), [return statement](/js/return)
2016-03-26 00:47:20 +01:00