SlideShare a Scribd company logo
CHAPTER
TWENTYSEVEN
FUNCTIONS - SECOND STYLE
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
27.1 Define Functions
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
27.2 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:
239
Ring Documentation, Release 1.9
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
27.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
27.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
27.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:
27.3. Declare parameters 240
Ring Documentation, Release 1.9
# 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
27.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.
27.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:
27.6. Variables Scope 241
Ring Documentation, Release 1.9
if novalue() = NULL
put "the function doesn't return a value" + nl
end
def novalue
27.8 Recursion
The Ring programming language support Recursion and the function can call itself using different parameters.
Example:
put fact(5) # output = 120
def fact x if x = 0 return 1 else return x * fact(x-1) end
27.8. Recursion 242
CHAPTER
TWENTYEIGHT
FUNCTIONS - THIRD STYLE
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
28.1 Define Functions
To define new function
Syntax:
func <function_name> [parameters] ['{']
Block of statements
['}']
Example:
load "stdlib.ring"
func hello {
print("Hello from function n")
}
28.2 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:
243
Ring Documentation, Release 1.9
load "stdlib.ring"
hello()
func hello {
print("Hello from function n")
}
Example:
load "stdlib.ring"
first() second()
func first { print("message from the first function n") }
func second { print("message from the second function n") }
28.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:
load "stdlib.ring"
func sum(x,y) {
print(x+y)
}
28.4 Send Parameters
To send parameters to function, type the parameters inside () after the function name
Syntax:
funcname(parameters)
Example:
/* output
** 8
** 3000
*/
load "stdlib.ring"
sum(3,5) sum(1000,2000)
func sum(x,y) { print(x+y) }
28.3. Declare parameters 244
Ring Documentation, Release 1.9
28.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:
# this program will print the hello world message first then execute the main function
load "stdlib.ring"
print("Hello, World! n")
func main {
print("Message from the main function n")
}
28.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
load "stdlib.ring"
x = 10 # x is a global variable.
func main {
for t = 1 to 10 { # t is a local variable
mycounter() # call function
}
}
func mycounter {
print("#{x}n") # 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.
28.7 Return Value
The function can return a value using the Return command.
Syntax:
28.5. Main Function 245
Ring Documentation, Release 1.9
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:
load "stdlib.ring"
if novalue() = NULL {
print("the function doesn't return a valuen")
}
func novalue { }
28.8 Recursion
The Ring programming language support Recursion and the function can call itself using different parameters.
Example:
load "stdlib.ring"
print( fact(5) ) # output = 120
func fact(x) { if x = 0 { return 1 else return x * fact(x-1) } }
28.8. Recursion 246
CHAPTER
TWENTYNINE
PROGRAM STRUCTURE
In this chapter we will learn about using many source code files in the same project.
29.1 Source Code File Sections
Each source code file may contains the next sections (in the same order).
Source Code File Sections
Load Files
Statements and Global Variables
Functions
Packages and Classes
The application maybe one or more of files.
29.2 Using Many Source Code Files
To include another source file in the project, just use the load command.
Syntax:
Load "filename.ring"
Note: The Load command is executed directly by the compiler in the parsing stage
Tip: if you don’t know the file name until the runtime, or you need to use functions to get the file path, just use eval().
Example:
# File : Start.ring
Load "sub.ring"
sayhello("Mahmoud")
# File : sub.ring
func sayhello cName
see "Hello " + cName + nl
247
Ring Documentation, Release 1.9
29.3 Load Package
Using the ‘load’ command we can use many ring source files in the same project
But all of these files will share the same global scope
We have also the “Load Package” command
Using “Load Package” we can load a library (*.ring file) in new global scope
This is very useful to create libraries that avoid conflicts in global variables
Example:
File: loadpackage.ring
x = 100
? "Hello, World!"
load package "testloadpackage.ring"
? x
test()
File: testloadpackage.ring
? "Hello from testloadpackage.ring"
x = 1000
test()
func test
? x
Output:
Hello, World!
Hello from testloadpackage.ring
1000
100
1000
29.3. Load Package 248

More Related Content

PDF
The Ring programming language version 1.8 book - Part 26 of 202
PDF
The Ring programming language version 1.5.3 book - Part 21 of 184
PDF
The Ring programming language version 1.6 book - Part 23 of 189
PDF
The Ring programming language version 1.10 book - Part 29 of 212
PDF
The Ring programming language version 1.5.4 book - Part 21 of 185
PDF
The Ring programming language version 1.7 book - Part 24 of 196
PDF
The Ring programming language version 1.5.2 book - Part 20 of 181
PDF
The Ring programming language version 1.5.1 book - Part 20 of 180
The Ring programming language version 1.8 book - Part 26 of 202
The Ring programming language version 1.5.3 book - Part 21 of 184
The Ring programming language version 1.6 book - Part 23 of 189
The Ring programming language version 1.10 book - Part 29 of 212
The Ring programming language version 1.5.4 book - Part 21 of 185
The Ring programming language version 1.7 book - Part 24 of 196
The Ring programming language version 1.5.2 book - Part 20 of 181
The Ring programming language version 1.5.1 book - Part 20 of 180

What's hot (19)

PDF
The Ring programming language version 1.5.1 book - Part 19 of 180
PPT
POLITEKNIK MALAYSIA
PPTX
functions of C++
PPT
Lecture#6 functions in c++
PPTX
Function in c++
PDF
Functional programming 101
DOC
Functions
PDF
M11 operator overloading and type conversion
PDF
Cpp functions
PPT
FUNCTIONS IN c++ PPT
PPTX
C and C++ functions
PDF
Functional programming java
PPT
User Defined Functions
PPTX
C++ programming function
PPT
Pre defined Functions in C
PPTX
Functions in C++
PPTX
Function C++
PPTX
Call by value
PPT
16717 functions in C++
 
The Ring programming language version 1.5.1 book - Part 19 of 180
POLITEKNIK MALAYSIA
functions of C++
Lecture#6 functions in c++
Function in c++
Functional programming 101
Functions
M11 operator overloading and type conversion
Cpp functions
FUNCTIONS IN c++ PPT
C and C++ functions
Functional programming java
User Defined Functions
C++ programming function
Pre defined Functions in C
Functions in C++
Function C++
Call by value
16717 functions in C++
 
Ad

Similar to The Ring programming language version 1.9 book - Part 28 of 210 (20)

PDF
The Ring programming language version 1.5 book - Part 4 of 31
PDF
The Ring programming language version 1.2 book - Part 11 of 84
PDF
The Ring programming language version 1.8 book - Part 25 of 202
PDF
The Ring programming language version 1.9 book - Part 27 of 210
PDF
The Ring programming language version 1.5.2 book - Part 21 of 181
PDF
Chapter 1. Functions in C++.pdf
PDF
Chapter_1.__Functions_in_C++[1].pdf
PPTX
Functions in Python Programming Language
PPT
functions _
PPTX
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
PPTX
Python_Functions_Unit1.pptx
PDF
The Ring programming language version 1.3 book - Part 13 of 88
DOC
Functions
PDF
PPT
Python programming variables and comment
PPTX
JNTUK python programming python unit 3.pptx
PPT
functions modules and exceptions handlings.ppt
PDF
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
PDF
3-Python Functions.pdf in simple.........
PPT
Powerpoint presentation for Python Functions
The Ring programming language version 1.5 book - Part 4 of 31
The Ring programming language version 1.2 book - Part 11 of 84
The Ring programming language version 1.8 book - Part 25 of 202
The Ring programming language version 1.9 book - Part 27 of 210
The Ring programming language version 1.5.2 book - Part 21 of 181
Chapter 1. Functions in C++.pdf
Chapter_1.__Functions_in_C++[1].pdf
Functions in Python Programming Language
functions _
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
Python_Functions_Unit1.pptx
The Ring programming language version 1.3 book - Part 13 of 88
Functions
Python programming variables and comment
JNTUK python programming python unit 3.pptx
functions modules and exceptions handlings.ppt
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
3-Python Functions.pdf in simple.........
Powerpoint presentation for Python Functions
Ad

More from Mahmoud Samir Fayed (20)

PDF
The Ring programming language version 1.10 book - Part 212 of 212
PDF
The Ring programming language version 1.10 book - Part 211 of 212
PDF
The Ring programming language version 1.10 book - Part 210 of 212
PDF
The Ring programming language version 1.10 book - Part 208 of 212
PDF
The Ring programming language version 1.10 book - Part 207 of 212
PDF
The Ring programming language version 1.10 book - Part 205 of 212
PDF
The Ring programming language version 1.10 book - Part 206 of 212
PDF
The Ring programming language version 1.10 book - Part 204 of 212
PDF
The Ring programming language version 1.10 book - Part 203 of 212
PDF
The Ring programming language version 1.10 book - Part 202 of 212
PDF
The Ring programming language version 1.10 book - Part 201 of 212
PDF
The Ring programming language version 1.10 book - Part 200 of 212
PDF
The Ring programming language version 1.10 book - Part 199 of 212
PDF
The Ring programming language version 1.10 book - Part 198 of 212
PDF
The Ring programming language version 1.10 book - Part 197 of 212
PDF
The Ring programming language version 1.10 book - Part 196 of 212
PDF
The Ring programming language version 1.10 book - Part 195 of 212
PDF
The Ring programming language version 1.10 book - Part 194 of 212
PDF
The Ring programming language version 1.10 book - Part 193 of 212
PDF
The Ring programming language version 1.10 book - Part 192 of 212
The Ring programming language version 1.10 book - Part 212 of 212
The Ring programming language version 1.10 book - Part 211 of 212
The Ring programming language version 1.10 book - Part 210 of 212
The Ring programming language version 1.10 book - Part 208 of 212
The Ring programming language version 1.10 book - Part 207 of 212
The Ring programming language version 1.10 book - Part 205 of 212
The Ring programming language version 1.10 book - Part 206 of 212
The Ring programming language version 1.10 book - Part 204 of 212
The Ring programming language version 1.10 book - Part 203 of 212
The Ring programming language version 1.10 book - Part 202 of 212
The Ring programming language version 1.10 book - Part 201 of 212
The Ring programming language version 1.10 book - Part 200 of 212
The Ring programming language version 1.10 book - Part 199 of 212
The Ring programming language version 1.10 book - Part 198 of 212
The Ring programming language version 1.10 book - Part 197 of 212
The Ring programming language version 1.10 book - Part 196 of 212
The Ring programming language version 1.10 book - Part 195 of 212
The Ring programming language version 1.10 book - Part 194 of 212
The Ring programming language version 1.10 book - Part 193 of 212
The Ring programming language version 1.10 book - Part 192 of 212

Recently uploaded (20)

PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
history of c programming in notes for students .pptx
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Understanding Forklifts - TECH EHS Solution
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
L1 - Introduction to python Backend.pptx
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
System and Network Administration Chapter 2
PPTX
Transform Your Business with a Software ERP System
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
history of c programming in notes for students .pptx
How Creative Agencies Leverage Project Management Software.pdf
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Understanding Forklifts - TECH EHS Solution
How to Choose the Right IT Partner for Your Business in Malaysia
L1 - Introduction to python Backend.pptx
Softaken Excel to vCard Converter Software.pdf
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PTS Company Brochure 2025 (1).pdf.......
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Navsoft: AI-Powered Business Solutions & Custom Software Development
CHAPTER 2 - PM Management and IT Context
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
System and Network Administration Chapter 2
Transform Your Business with a Software ERP System
Which alternative to Crystal Reports is best for small or large businesses.pdf

The Ring programming language version 1.9 book - Part 28 of 210

  • 1. CHAPTER TWENTYSEVEN FUNCTIONS - SECOND STYLE 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 27.1 Define Functions 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 27.2 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: 239
  • 2. Ring Documentation, Release 1.9 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 27.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 27.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 27.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: 27.3. Declare parameters 240
  • 3. Ring Documentation, Release 1.9 # 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 27.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. 27.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: 27.6. Variables Scope 241
  • 4. Ring Documentation, Release 1.9 if novalue() = NULL put "the function doesn't return a value" + nl end def novalue 27.8 Recursion The Ring programming language support Recursion and the function can call itself using different parameters. Example: put fact(5) # output = 120 def fact x if x = 0 return 1 else return x * fact(x-1) end 27.8. Recursion 242
  • 5. CHAPTER TWENTYEIGHT FUNCTIONS - THIRD STYLE 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 28.1 Define Functions To define new function Syntax: func <function_name> [parameters] ['{'] Block of statements ['}'] Example: load "stdlib.ring" func hello { print("Hello from function n") } 28.2 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: 243
  • 6. Ring Documentation, Release 1.9 load "stdlib.ring" hello() func hello { print("Hello from function n") } Example: load "stdlib.ring" first() second() func first { print("message from the first function n") } func second { print("message from the second function n") } 28.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: load "stdlib.ring" func sum(x,y) { print(x+y) } 28.4 Send Parameters To send parameters to function, type the parameters inside () after the function name Syntax: funcname(parameters) Example: /* output ** 8 ** 3000 */ load "stdlib.ring" sum(3,5) sum(1000,2000) func sum(x,y) { print(x+y) } 28.3. Declare parameters 244
  • 7. Ring Documentation, Release 1.9 28.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: # this program will print the hello world message first then execute the main function load "stdlib.ring" print("Hello, World! n") func main { print("Message from the main function n") } 28.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 load "stdlib.ring" x = 10 # x is a global variable. func main { for t = 1 to 10 { # t is a local variable mycounter() # call function } } func mycounter { print("#{x}n") # 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. 28.7 Return Value The function can return a value using the Return command. Syntax: 28.5. Main Function 245
  • 8. Ring Documentation, Release 1.9 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: load "stdlib.ring" if novalue() = NULL { print("the function doesn't return a valuen") } func novalue { } 28.8 Recursion The Ring programming language support Recursion and the function can call itself using different parameters. Example: load "stdlib.ring" print( fact(5) ) # output = 120 func fact(x) { if x = 0 { return 1 else return x * fact(x-1) } } 28.8. Recursion 246
  • 9. CHAPTER TWENTYNINE PROGRAM STRUCTURE In this chapter we will learn about using many source code files in the same project. 29.1 Source Code File Sections Each source code file may contains the next sections (in the same order). Source Code File Sections Load Files Statements and Global Variables Functions Packages and Classes The application maybe one or more of files. 29.2 Using Many Source Code Files To include another source file in the project, just use the load command. Syntax: Load "filename.ring" Note: The Load command is executed directly by the compiler in the parsing stage Tip: if you don’t know the file name until the runtime, or you need to use functions to get the file path, just use eval(). Example: # File : Start.ring Load "sub.ring" sayhello("Mahmoud") # File : sub.ring func sayhello cName see "Hello " + cName + nl 247
  • 10. Ring Documentation, Release 1.9 29.3 Load Package Using the ‘load’ command we can use many ring source files in the same project But all of these files will share the same global scope We have also the “Load Package” command Using “Load Package” we can load a library (*.ring file) in new global scope This is very useful to create libraries that avoid conflicts in global variables Example: File: loadpackage.ring x = 100 ? "Hello, World!" load package "testloadpackage.ring" ? x test() File: testloadpackage.ring ? "Hello from testloadpackage.ring" x = 1000 test() func test ? x Output: Hello, World! Hello from testloadpackage.ring 1000 100 1000 29.3. Load Package 248