========= Functions ========= In this chapter we are going to learn about the next topics :- * Define functions * Call functions * Declare parameters * Send parameters * Main Function * Variables Scope * Program structure * Return Value * Recursion .. index:: pair: Functions; Define Functions Define Functions ================ To define new function Syntax: .. code-block:: none func [parameters] Block of statements .. note:: No keyword is required to end the function definition. Example: .. code-block:: none func hello see "Hello from function" + nl .. index:: pair: Functions; Call Functions Call Functions ============== To call function without parameters, we type the function name then () .. tip:: We can call the function before the function definition and the function code. Example: .. code-block:: none hello() func hello see "Hello from function" + nl Example: .. code-block:: none first() second() func first see "message from the first function" + nl func second see "message from the second function" + nl Declare parameters ================== To declare the function parameters, after the function name type the list of parameters as a group of identifiers separated by comma. Example: .. code-block:: none func sum x,y see x+y+nl Send Parameters =============== To send parameters to function, type the parameters inside () after the function name Syntax: .. code-block:: none funcname(parameters) Example: .. code-block:: none /* output ** 8 ** 3000 */ sum(3,5) sum(1000,2000) func sum x,y see x+y+nl Main Function ============= Using the Ring programming language, the Main Function is optional, when it's defined, it will be executed after the end of other statements. if no other statements comes alone, the main function will be the first `entry point `_ Example: .. code-block:: none # this program will print the hello world message first then execute the main function See "Hello World!" + nl func main see "Message from the main function" + nl Variables Scope =============== The Ring 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: .. code-block:: none # the program will print numbers from 10 to 1 x = 10 # x is a global variable. func main for t = 1 to 10 # t is a local variable mycounter() # call function next func mycounter see x + nl # print the global variable value x-- # decrement .. note:: Using the main function before the for loop declare the t variable as a local variable, It's recommended to use the main functions instead of typing the instructions directly to set the scope of the new variables to local. Program Structure ================= +--------------------------------+ | Source Code File Sections | +================================+ | Load Files | +--------------------------------+ | Statements and Global Variables| +--------------------------------+ | Functions | +--------------------------------+ | Packages and Classes | +--------------------------------+ The application maybe one or more of files. to include another source file in the project, just use the load command. Syntax: .. code-block:: none Load "filename.ring" Example: .. code-block:: none # File : Start.ring Load "sub.ring" sayhello("Mahmoud") .. code-block:: none # File : sub.ring func sayhello cName see "Hello " + cName + nl Return Value ============ The function can return a value using the Return command. Syntax: .. code-block:: none Return [Expression] .. tip:: the Expression after the return command is optional and we can use the return command to end the function execution without returning any value. .. note:: if the function doesn't return explicit value, it will return NULL (empty string = "" ). Example: .. code-block:: none if novalue() = NULL See "the function doesn't return a value" + nl ok func novalue Recursion ========= The Ring programming language support `Recursion `_ and the function can call itself using different parameters. Example: .. code-block:: none see fact(5) # output = 120 func fact x if x = 1 return 1 else return x * fact(x-1) ok