Functions
Predefined functions are great.
But what is even better is that you can define your own function!
Before we go deeper, we need to realize that we already have one function, main
.
The main function is very important, since it is the first function that gets executed when you click Play.
Now let's define a simple function: greet(name)
.
It will say hello to someone.
Steps:
- add a new function by clicking + on the left pane
- click on
fun1
node and rename it togreet
- click + to add a new parameter and type
name
, select String as parameter type - add a new
Output
and type"Hello " + name
- click
main
to go back to main function - add a new
Call
command and typegreet("Tom")
Run the program, it should display Hello Tom
in the output.
Functions are a powerful tool for abstracting out common code.