Vous êtes sur la page 1sur 10

# Ring Documentation, Release 1.

## GetChar() GetChar() # End of line

# the previous two lines can be replaced with the next line
# Give Option

if Option = 1
see "Enter your name : " give cName
see "Hello " + cName
else
bye
ok
End

## 23.3 Input() Function

We can get input from the keyboard using the Input() function
Syntax:
Input(nCount) ---> string

The function will wait until nCount characters (at least) are read
Example:
See "Enter message (30 characters) : " cMsg = input(30)
See "Message : " + cMsg

CHAPTER

TWENTYFOUR

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

• Define functions
• Call functions
• Declare parameters
• Send parameters
• Main Function
• Variables Scope
• Return Value
• Recursion

## To define new function

Syntax:
func <function_name> [parameters]
Block of statements

## Note: No keyword is required to end the function definition.

Example:
func hello
see "Hello from function" + nl

## 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:

203
Ring Documentation, Release 1.7

hello()

func hello
see "Hello from function" + nl

Example:
first() second()

## 24.3 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:
func sum x,y
see x+y+nl

## 24.4 Send Parameters

To send parameters to function, type the parameters inside () after the function name
Syntax:
funcname(parameters)

Example:
/* output
** 8
** 3000
*/

sum(3,5) sum(1000,2000)

## 24.5 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:

## 24.3. Declare parameters 204

Ring Documentation, Release 1.7

# 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

## 24.6 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 func-
tions (before any function) are global variables.
Inside any function we can access the variables defined inside this function beside the global variables.
Example:
# 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.

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

Syntax:
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:

## 24.6. Variables Scope 205

Ring Documentation, Release 1.7

if novalue() = NULL
See "the function doesn't return a value" + nl
ok

func novalue

24.8 Recursion

The Ring programming language support Recursion and the function can call itself using different parameters.
Example:
see fact(5) # output = 120

CHAPTER

TWENTYFIVE

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

• Define functions
• Call functions
• Declare parameters
• Send parameters
• Main Function
• Variables Scope
• Return Value
• Recursion

## To define new function

Syntax:
def <function_name> [parameters]
Block of statements
[end]

## Note: the keyword ‘end’ is optional.

Example:
def hello
put "Hello from function" + nl
end

## 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:

207
Ring Documentation, Release 1.7

hello()

def hello
put "Hello from function" + nl
end

Example:
first() second()

## 25.3 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:
def sum x,y
put x+y+nl
end

## 25.4 Send Parameters

To send parameters to function, type the parameters inside () after the function name
Syntax:
funcname(parameters)

Example:
/* output
** 8
** 3000
*/

sum(3,5) sum(1000,2000)

## 25.5 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:

## 25.3. Declare parameters 208

Ring Documentation, Release 1.7

# this program will print the hello world message first then execute the main function

## put "Hello World!" + nl

def main
put "Message from the main function" + nl
end

## 25.6 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 func-
tions (before any function) are global variables.
Inside any function we can access the variables defined inside this function beside the global variables.
Example:
# the program will print numbers from 10 to 1

x = 10 # x is a global variable.

def main
for t = 1 to 10 # t is a local variable
mycounter() # call function
end
end

def mycounter
put x + nl # print the global variable value
x-- # decrement
end

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.

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

Syntax:
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:

## 25.6. Variables Scope 209

Ring Documentation, Release 1.7

if novalue() = NULL
put "the function doesn't return a value" + nl
end

def novalue

25.8 Recursion

The Ring programming language support Recursion and the function can call itself using different parameters.
Example:
put fact(5) # output = 120

CHAPTER

TWENTYSIX

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

• Define functions
• Call functions
• Declare parameters
• Send parameters
• Main Function
• Variables Scope
• Return Value
• Recursion

## To define new function

Syntax:
func <function_name> [parameters] ['{']
Block of statements
['}']

Example: