An Introduction to Dragon/Lessons/Functions

Functions
In this chapter we are going to learn about the next topics :-


 * Define functions


 * Call functions


 * Declare parameters


 * Send parameters


 * Variables Scope


 * Return Value

Define Functions
To define a new function:

Syntax:

Example:

Call Functions
Tip: We can call the function before the function definition.

Example:

Example:

Declare parameters
To declare the function parameters, write them in parentheses.

Example:

Send Parameters
To send parameters to function, type the parameters inside after the function name

Syntax:

Example:

Variables Scope
The Dragon programming language uses lexical scoping to determine the scope of a variable. Variables defined inside functions (including function parameters) are local variables. Variables defined outside functions (before any function) are global variables.

Inside any function we can access the variables defined inside this function beside the global variables.

Example:

Return Value
The function can return a value using the return command.

Syntax:

Example: