How to Remove Document Management Hurdles with X-Docs?
Ā
The Ring programming language version 1.5.1 book - Part 19 of 180
1. Ring Documentation, Release 1.5.1
Example:
load "stdlib.ring"
# print even numbers from 10 to 0
for x = 10 to 0 step -2 {
print("#{x}n")
}
ā¢ For in Loop
Syntax:
for identifier in List/String [step expression] {
Block of statements
}
Example:
load "stdlib.ring"
aList = 1:10 # create list contains numbers from 1 to 10
for x in aList { print("#{x}n") } # print numbers from 1 to 10
Example:
load "stdlib.ring"
aList = 1:10 # create list contains numbers from 1 to 10
# print odd items inside the list
for x in aList step 2 {
print("#{x}n")
}
When we use (For in) we get items by reference.
This means that we can read/edit items inside the loop.
Example:
load "stdlib.ring"
aList = 1:5 # create list contains numbers from 1 to 5
# replace list numbers with strings
for x in aList {
switch x {
case 1 x = "one"
case 2 x = "two"
case 3 x = "three"
case 4 x = "four"
case 5 x = "five"
}
}
print(aList) # print the list items
20.3 Exceptions
20.3. Exceptions 155
3. CHAPTER
TWENTYONE
GETTING INPUT
We can get input from the keyboard using
ā¢ The Give Command
ā¢ The GetChar() Function
ā¢ The Input() Function
21.1 Give Command
Syntax:
Give VariableName
Example:
See "Enter the first number : " Give nNum1
See "Enter the second number : " Give nNum2
See "Sum : " + ( 0 + nNum1 + nNum2 )
Output:
Enter the first number : 3
Enter the second number : 4
Sum : 7
21.2 GetChar() Function
We can get one character from the standard input using the GetChar() function
Syntax:
GetChar() ---> Character
Example:
While True
See "
Main Menu
(1) Say Hello
(2) Exit
"
Option = GetChar()
157
4. Ring Documentation, Release 1.5.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
21.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
21.3. Input() Function 158
5. CHAPTER
TWENTYTWO
FUNCTIONS - FIRST STYLE
In this chapter we are going to learn about the next topics :-
ā¢ Deļ¬ne functions
ā¢ Call functions
ā¢ Declare parameters
ā¢ Send parameters
ā¢ Main Function
ā¢ Variables Scope
ā¢ Return Value
ā¢ Recursion
22.1 Deļ¬ne Functions
To deļ¬ne new function
Syntax:
func <function_name> [parameters]
Block of statements
Note: No keyword is required to end the function deļ¬nition.
Example:
func hello
see "Hello from function" + nl
22.2 Call Functions
To call function without parameters, we type the function name then ()
Tip: We can call the function before the function deļ¬nition and the function code.
Example:
159
6. Ring Documentation, Release 1.5.1
hello()
func hello
see "Hello from function" + nl
Example:
first() second()
func first see "message from the first function" + nl
func second see "message from the second function" + nl
22.3 Declare parameters
To declare the function parameters, after the function name type the list of parameters as a group of identiļ¬ers separated
by comma.
Example:
func sum x,y
see x+y+nl
22.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)
func sum x,y see x+y+nl
22.5 Main Function
Using the Ring programming language, the Main Function is optional, when itās deļ¬ned, it will be executed after the
end of other statements.
if no other statements comes alone, the main function will be the ļ¬rst entry point
Example:
22.3. Declare parameters 160
7. Ring Documentation, Release 1.5.1
# 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
22.6 Variables Scope
The Ring programming language uses lexical scoping to determine the scope of a variable.
Variables deļ¬ned inside functions (including function parameters) are local variables. Variables deļ¬ned outside func-
tions (before any function) are global variables.
Inside any function we can access the variables deļ¬ned 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.
22.7 Return Value
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:
22.6. Variables Scope 161
8. Ring Documentation, Release 1.5.1
if novalue() = NULL
See "the function doesn't return a value" + nl
ok
func novalue
22.8 Recursion
The Ring programming language support Recursion and the function can call itself using different parameters.
Example:
see fact(5) # output = 120
func fact x if x = 0 return 1 else return x * fact(x-1) ok
22.8. Recursion 162
9. CHAPTER
TWENTYTHREE
FUNCTIONS - SECOND STYLE
In this chapter we are going to learn about the next topics :-
ā¢ Deļ¬ne functions
ā¢ Call functions
ā¢ Declare parameters
ā¢ Send parameters
ā¢ Main Function
ā¢ Variables Scope
ā¢ Return Value
ā¢ Recursion
23.1 Deļ¬ne Functions
To deļ¬ne 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
23.2 Call Functions
To call function without parameters, we type the function name then ()
Tip: We can call the function before the function deļ¬nition and the function code.
Example:
163
10. Ring Documentation, Release 1.5.1
hello()
def hello
put "Hello from function" + nl
end
Example:
first() second()
def first put "message from the first function" + nl
def second put "message from the second function" + nl
23.3 Declare parameters
To declare the function parameters, after the function name type the list of parameters as a group of identiļ¬ers separated
by comma.
Example:
def sum x,y
put x+y+nl
end
23.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)
def sum x,y put x+y+nl
23.5 Main Function
Using the Ring programming language, the Main Function is optional, when itās deļ¬ned, it will be executed after the
end of other statements.
if no other statements comes alone, the main function will be the ļ¬rst entry point
Example:
23.3. Declare parameters 164