DOORS DXL Function

DOORS DXL Function

In this series of articles, I am going to explain to you about IBM DOORS DXL scripting from scratch. So, if you are a beginner in DXL scripting, hopefully, this article would help you. This is part-6 of the DOORS DXL tutorial.

So, if you are looking for a starting point to learn DXL scripting, you are on the right page! I know, we have the DXL reference manual and it’s a bit concise and difficult to grasp for beginners, especially if you do not have any previous experience in other programming languages.

In this article, we will mainly discuss the DOORS DXL function.

What is the function?

The function is a set of/block of code that performs repetitive tasks in any programming language. DOORS DXL also supports the function. In this article, we will see several examples of functions in DXL programming.

DOORS DXL Function Syntax

Here is a simple example of the DXL function. I have just defined a function called “myPrint()” to print the value of “i”. Here is the syntax of the DXL function:

return_Type function_Name (Parameter -1, Parameter – 2, Parameter – 3, ……) {
statement-1
statement-2
…………..
}

DOORS DXL Function Definition

Here is a couple of examples to show how to define the DXL function:

int myPrint() {
………..
}

bool myPrint(int i, int j) {
…………..
}

void myPrint(int i, real r) {
………….
}

In the following example, the return type of the function is “void”, which indicates that the function does not return any value.

Let us now look at the example.

// www.TheCloudStrap.Com

/************************************************************************
* $FILENAME: d6.0.dxl
* $DESCRIPTION: DXL function.
*
* NOTICE: Copyright www.TheCloudStrap.Com. All rights reserved.
* Software comes without any warranties and guarantees, is provided 
* as is and is not supported. Use this software at your own risk. 
* Authors resume no liabilities.
* 
* Contact: admin {at} TheCloudStrap.com
************************************************************************/

void myPrint(int i){
	print("The value of i is = " i "\n")
}

/***********************************************
  Main Code
***********************************************/
int a = 20

myPrint(a)

a = 30
myPrint(a)

a = 40
myPrint(a)
DOORS DXL Function 6.0

DOORS DXL Function – Parameter Passing

There are two methods of passing parameters in the DXL function.

  1. Pass by value
  2. Pass by reference

Please note that there is no concept of “Pass by address”. DXL does not support pointer.

Key Points:
1. Ampersand (&) is used for Pass by reference.
2. DOORS built-in types and arrays are always passed by reference. Therefore, Ampersand(&) is not required in such cases.

DOORS DXL Function – Pass by value

Here is another example code to demonstrate the pass-by-value mechanism. As you can see in the output, the values get swapped in the swap() function but do not reflect in the main() function.

// www.TheCloudStrap.Com

/************************************************************************
* $FILENAME: d6.1.dxl
* $DESCRIPTION: Swap two integers in DXL.
*		Pass by value does not work. 
*
* NOTICE: Copyright www.TheCloudStrap.Com. All rights reserved.
* Software comes without any warranties and guarantees, is provided 
* as is and is not supported. Use this software at your own risk. 
* Authors resume no liabilities.
* 
* Contact: admin {at} TheCloudStrap.com
************************************************************************/

void swap(int i, int j){
	int temp

	temp = i 
	i = j
	j = temp
}

/***********************************************
  Main Code
***********************************************/
int a = 20, b = 30

print("Before swap: \n")
print("a = " a "\t" "b = " b "\n")

swap(a, b)

print("After swap: \n")
print("a = " a "\t" "b = " b "\n")
DOORS DXL Function 6.1

DOORS DXL Function – Pass by reference

Here is an example of a pass-by-reference in the DXL function. In this example, I am passing one of the parameters by reference and another by value.

// www.TheCloudStrap.Com

/************************************************************************
* $FILENAME: d6.1.dxl
* $DESCRIPTION: Function parameter passing in DXL.
*
* NOTICE: Copyright www.TheCloudStrap.Com. All rights reserved.
* Software comes without any warranties and guarantees, is provided 
* as is and is not supported. Use this software at your own risk. 
* Authors resume no liabilities.
* 
* Contact: admin {at} TheCloudStrap.com
************************************************************************/

int addition(int i, int &j){
	i = i + 100
	j = j + 100

	return i
}

/***********************************************
  Main Code
***********************************************/
int a = 50, b = 50, res = 0

res = addition(a, b)

print(res "\n")
print(a   "\n")
print(b   "\n")

DOORS DXL Function 6.2

Swap Two variables – Pass by reference

This is the example code to swap two variables using a pass-by reference mechanism.

// www.TheCloudStrap.Com

/************************************************************************
* $FILENAME: d6.3.dxl
* $DESCRIPTION: Swap two integers in DXL.
*		Pass by reference works.
*
* NOTICE: Copyright www.TheCloudStrap.Com. All rights reserved.
* Software comes without any warranties and guarantees, is provided 
* as is and is not supported. Use this software at your own risk. 
* Authors resume no liabilities.
* 
* Contact: admin {at} TheCloudStrap.com
************************************************************************/

void swap(int &i, int &j){
	int temp

	temp = i 
	i = j
	j = temp
}

/***********************************************
  Main Code
***********************************************/
int a = 20, b = 30

print("Before swap: \n")
print("a = " a "\t" "b = " b "\n")

swap(a, b)

print("After swap: \n")
print("a = " a "\t" "b = " b "\n")
DOORS DXL Function 6.3

DOORS DXL Function Overloading

Overloaded functions have same name but –

  1. The number of arguments is different
  2. Types of parameters are different
  3. Return Type

Here is an example code to illustrate function overloading in DXL.

// www.TheCloudStrap.Com

/************************************************************************
* $FILENAME: d6.4.dxl
* $DESCRIPTION: Function overloading in DXL.
*
* NOTICE: Copyright www.TheCloudStrap.Com. All rights reserved.
* Software comes without any warranties and guarantees, is provided 
* as is and is not supported. Use this software at your own risk. 
* Authors resume no liabilities.
* 
* Contact: admin {at} TheCloudStrap.com
************************************************************************/

int addition(int i, int j){
	int temp
	temp = i + j

	return temp
}

real addition(real f, real g){
	real temp
	temp = f + g

	return temp	
}

int addition(int i, int j, int k){
	int temp
	temp = i + j + k

	return temp
}

real addition(real f, real g, real h){
	real temp
	temp = f + g + h

	return temp	
}

/***********************************************
  Main Code
***********************************************/
int i1 = 10, i2 = 20, i3 = 30, i_res = 0
real r1 = 5.5, r2 = 2.7, r3 = 3.8, r_res = 0

i_res = addition(i1, i2)
print(i_res "\n")

i_res = addition(i1, i2, i3)
print(i_res "\n")

r_res = addition(r1, r2)
print(r_res "\n")

r_res = addition(r1, r2, r3)
print(r_res "\n")
DOORS DXL Function 6.4

DOORS DXL – Operator Function

An operator can be redefined in DXL with::. In the following example code, the multiplication operator has been redefined for string and integer.

// www.TheCloudStrap.Com

/************************************************************************
* $FILENAME: d6.5.dxl
* $DESCRIPTION: Operator function in DXL.
*
* NOTICE: Copyright www.TheCloudStrap.Com. All rights reserved.
* Software comes without any warranties and guarantees, is provided 
* as is and is not supported. Use this software at your own risk. 
* Authors resume no liabilities.
* 
* Contact: admin {at} TheCloudStrap.com
************************************************************************/

string ::*(string str, int n){
	string tempStr = ""
	int i 

	for i in 0 : n-1 do {
		tempStr = tempStr "   " str
	}

	return tempStr
}

/***********************************************
  Main Code
***********************************************/

print("www.TheCloudStrap.Com" * 5)
DOORS DXL Tutorial

DOORS DXL Function Calls

You can call the DXL function without the parenthesis. However, if the function has more than one parameter, you need to call the function with parenthesis.

For example, if the function is defined as:

void print_simple() {} or void print_simple(string s) {}

You can call the function without parenthesis i.e. “print_simple”.

But, if the function receives two or more arguments –

void print_simple(string s1, string s2) {}

You need to mention parenthesis while calling the function. Otherwise, you will get the following DXL error:

-E- DXL: incorrect arguments for function (print_arg_2)
-I- DXL: All done. Errors reported: 1. Warnings reported: 0.

// www.TheCloudStrap.Com

/************************************************************************
* $FILENAME: d6.6.dxl
* $DESCRIPTION: Function calls.
*				
*
* NOTICE: Copyright www.TheCloudStrap.Com. All rights reserved.
* Software comes without any warranties and guarantees, is provided 
* as is and is not supported. Use this software at your own risk. 
* Authors resume no liabilities.
* 
* Contact: admin {at} TheCloudStrap.com
************************************************************************/

void print_simple(){
	print("This is a simple print function.\n")
}

void print_arg(string s){
	print(s)
	print("\n")
}

void print_arg_2(string s1, string s2){
	print(s1 s2)
	print("\n")
}

/***********************************************
  Main Code
***********************************************/
print_simple()				 // Method - 1: With parenthesis
print_simple			        // Method - 2: Without parenthesis
print("----------------\n")


print_arg("Hello World")         // Method - 1: With parenthesis
print_arg "Hello World"	         // Method - 2: Without parenthesis
print("----------------\n")


print_arg("Hello" "World")
print_arg "Hello" "World"         // concatenation has higher precedence than function call.
print("----------------\n")


print_arg_2("Hello ", " World")

// Can not omit parethesis when the number of argument is more than one. 
// You will get the following DXL error: 
// -E- DXL: <Line:57> incorrect arguments for function (print_arg_2)
// -I- DXL: All done. Errors reported: 1. Warnings reported: 0.

// Therefore, the best practice is to always use parenthesis for function call.

//print_arg_2 "Hello ", " World"
DOORS DXL Tutorial

Conclusion

In this article, we have discussed the DOORS DXL functions. We have seen how to define a DOORS DXL function, how to overload, the parameter passing mechanism, and how to call a function in DOORS DXL. I hope this was a helpful article. However, if you have any questions, feel free to comment below.

In the next DOORS DXL tutorial, I will discuss the strings and how to use strings in DXL.

DOORS DXL Function
Scroll to top
error: Content is protected !!