PC SOFT

WINDEVWEBDEV AND WINDEV MOBILE
ONLINE HELP

Home | Sign in | English UK
  • This lesson will teach you the following concepts
  • Overview
  • Types of procedures
  • Creating and calling a procedure
  • Global procedure
  • Local procedure
  • Internal procedure
  • Calling a procedure
  • Procedure parameters
  • What is a parameter?
  • How to use the parameters?
  • Mandatory or optional parameters?
  • Result of a procedure
  • Practical example: Using a procedure
  • Project used
  • Implementation
  • Creating and using a procedure
  • Conclusion
Appendix 5. The procedures
This lesson will teach you the following concepts
  • Types of procedures
  • Creating and calling a procedure
  • Procedure parameters
  • Result of a procedure
  • Practical example

Estimated time: 1 h
Previous LessonTable of contentsNext Lesson
Overview
A procedure is used to associate an identifier with a code section in order to re-use it. In this lesson, we are going to present the different types of procedures available in WLanguage, their creation mode, how to call them, pass parameters and retrieve a result.
Types of procedures
Three types of procedures are available:
  • Global procedure: can be used in all project the events and processes of the project (declared in a set of procedures).
  • Procedure local to a window, page or mobile window: can be used in all the events and processes that depend on the object in which this procedure was declared.
  • Internal procedure to a process or event: can only be used in the process or event where it was declared.

Note

Scope of procedures
The procedures comply with the scope rules presented for the variables (see "The scope of variables").
Creating and calling a procedure

Global procedure

To create a global procedure, you must:
  1. Create (if necessary) a set of procedures (via the "Project explorer" pane, "Procedures" folder). Give a name to the set of procedures.
  2. Create a global procedure in the set of procedures (via the "Project explorer" pane, "Procedures, Set name" folder). Give a name to the procedure.
  3. Type the code of global procedure. The procedure code has the following format:
    PROCEDURE <Name of global procedure>()

Local procedure

To create a local procedure, you must:
  1. Select the element associated with the procedure (window, page, etc.).
  2. Create a local procedure (via the "Project explorer" pane, expand the element name, "Local procedures" folder).
  3. Give a name to the procedure.
  4. Type the code of local procedure. The procedure code has the following format:
    PROCEDURE <Name of local procedure>()

Internal procedure

To create an internal procedure, type the following code in the requested process:
INTERNAL PROCEDURE <Procedure name>()
<Code of internal procedure>
END

Calling a procedure

To call a procedure, use the procedure name (with the possible parameters that will be passed to it).
<Procedure name>(<Parameter 1>, ..., <Parameter N>)
See Calling a procedure/a function for more details.
Procedure parameters

What is a parameter?

A parameter is a value sent to a procedure during the call to the procedure.
The following example is used to call the Multiply10 procedure and to pass in parameter the value that will be handled in the procedure:
Multiply10(50)
You have the ability to pass from 0 to several values in parameter to a procedure. These values can have any type (as with the variables).
The parameter is specified in the procedure declaration in the format of a variable. For example, for the Multiply10 procedure, the procedure code is:
PROCEDURE Multiply10(P)
P=P*10
P is the parameter expected by the procedure.

Note

To specify the parameter role in the procedure, you have the ability to typecast the parameter in the procedure declaration.
For example, to use numeric values only, you have the ability to declare:
PROCEDURE Multiply10(P is numeric)
In the following example, the Multiplication procedure expects two Integer parameters and returns the multiplication result.
The procedure code is as follows:
PROCEDURE Multiplication(Nb1 is int, Nb2 is int)
RESULT is int
RESULT = Nb1 * Nb2
RESULT RESULT
The code used to call the procedure is as follows:
res is int
res = Multiplication(10, 50)
// Res is equal to 500

How to use the parameters?

By default, passing parameters in WLanguage is performed by reference (or by address). The parameter in the procedure represents (references) the variable passed during the call.
Therefore, when a procedure statement modifies the parameter value, the value of the variable corresponding to this parameter is modified.
Example:
  • The procedure code is as follows:
    PROCEDURE Test_address(P1)
    P1 = P1 * 2
  • The code used to call the procedure is as follows:
    T is int
    T = 12 // T is equal to 12 before the call
    Test_address(T)
    // T is equal to 24 after the call
To avoid modifying the value of the variable corresponding to the parameter, the parameters must be passed by value. Passing parameters by value allows you to handle a copy of parameter value. If the procedure code modifies the variable value, the value of the variable corresponding to the parameter is not modified.
To force a parameter to be passed by value to a procedure, the LOCAL keyword must be used in front of the parameter name in the procedure declaration. This keyword indicates that the following parameter will not be modified by the procedure.
Example:
  • The procedure code is as follows:
    PROCEDURE Test_value(LOCAL P1)
    // Local indicates that the parameter will be passed by value
    P1 = P1 * 2
  • The code used to call the procedure is as follows:
    T is int
    T = 12 // T is equal to 12
    Test_value(T)
    // T does not change

Note

In the same procedure, some parameters can be passed by address while other parameters can be passed by value. All you have to do is used the LOCAL keyword in front of each parameter passed by value.

Mandatory or optional parameters?

The parameters received in the procedure can be mandatory or optional parameters. A mandatory parameter must be filled during the call to the procedure while an optional parameter can be omitted: in this case, it will take the default value defined when declaring the procedure parameters.

Note

When declaring a procedure, the optional parameters are the last parameters (they are always specified after all mandatory parameters).
In the following example, the Multiplication procedure is using an optional parameter, Nb2. This optional parameter is indicated after the mandatory parameters, by specifying its default value. In this example, the default value of optional parameter is set to 10.
PROCEDURE Multiplication(Nb1 is int, Nb2 is int=10)
RESULT is int
RESULT = Nb1 * Nb2
RESULT RESULT
The code used to call the procedure is as follows:
res is int
res = Multiplication(6)
// Res is equal to 60
In this example, the second parameter was not specified. Therefore, its default value will be used.
Result of a procedure
The procedures can return one or more results. The result can be typecasted. The RESULT keyword must be used to return a value.
See RESULT statement for more details.
Practical example: Using a procedure
In a new page, we are now going to:
  • Create two numeric edit controls containing the value BT and the value IOT.
  • Create a Combo Box control used to choose the VAT rate.
  • Create a button used to calculate and display the value IOT corresponding to the amount BT.
The calculation result will be displayed in the "Price IOT" control.
This page is as follows:

Project used

To handle the different concepts of this lesson, we are going to create different pages. These pages will be created in the "My_First_Pages" project.
  • Open the "My_First_Pages" project if necessary.
    To do so, in the home window, click "Tutorial" and select the project named "My first pages (Exercise)".
    Tip: if the home window is not displayed, on the "Home" pane, in the "Online help" group, expand "Tutorial" and select "My first pages (Exercise)".

Implementation

  • Create a new blank page:
    1. Click on among the quick access buttons. The window for creating a new element is displayed: click "Page" then "Page". The wizard for page creation is displayed.
    2. Select "Blank page" and "Simple layout".
    3. Validate. The page is automatically created in the editor. The window for saving an element is displayed.
    4. Specify the page title: "Procedures". The page name ("PAGE_Procedures") is automatically proposed.
    5. Validate the save window (green button).
  • To create the edit control corresponding to the price BT:
    1. On the "Creation" pane, in the "Usual controls" group, expand "Edit" (click the arrow). The list of available edit controls is displayed. Select the "Currency" control and position the control in the page.
    2. Right-click the control and select "Description".
    3. In the description window:
      • Type the control name: "EDT_PriceBT".
      • Type the caption: "Price BT".
    4. Validate.
  • To create the control where the result will be displayed:
    1. On the "Creation" pane, in the "Usual controls" group, expand "Edit" (click the arrow). The list of available edit controls is displayed. Select the "Currency" control and position the control in the page (below the edit control displaying the price BT for example).
    2. Type the control information: right-click the control and select "Description".
      • Specify the control name: "EDT_PriceIOT".
      • Modify the caption to "Price IOT".
    3. The result displayed in this control must not be modifiable. Click the "GUI" tab and choose "Read-only" for the initial status.
    4. Validate.
    5. Save the page.
  • To create the Combo Box control for VAT selection:
    1. On the "Creation" pane, in the "Usual controls" group, click "Combo box" then click the position where the control will be created in the page (beside the "Price BT" control).
    2. The wizard for creating a Combo Box control is displayed. This wizard is used to define the main control characteristics.
    3. Select "I want to fill the combo box myself".
    4. Display the next screen.
    5. Keep the default options. Display the next screen.
    6. In the screen named "Initial content", type the list of possible VAT values:
      • 5.5
      • Press the Enter key.
      • 10
      • Press the Enter key.
      • 20.
    7. Display the next screen: give a name (COMBO_VAT) and a caption (VAT) to the control.
    8. Validate.
  • To create the "Calculate" button:
    1. On the "Creation" pane, in the "Usual controls" group, click .
    2. Click the location where the button will be created (below the Combo Box control for example).
    3. Click the button that was just created. The text displayed in the button becomes editable. Type the caption: "Calculate".
    4. Press Enter to validate the input.
  • The amount IOT will be calculated via the code of the "Calculate" button.
    1. Display the code of "Calculate" control ("Code" from the popup menu).
    2. Type the following code in the "Click on BTN_Calculate (server code)" event:
      SWITCH COMBO_VAT..DisplayedValue
      // 5.5 %
      CASE 5.5
      EDT_PriceIOT = EDT_PriceBT * 1.055
      // 10 %
      CASE 10
      EDT_PriceIOT = EDT_PriceBT * 1.1
      // 20 %
      CASE 20
      EDT_PriceIOT = EDT_PriceBT * 1.2
      END

      This code calculates the amount IOT by using the value selected in the Combo Box control (returned by ..DisplayedValue).
    3. Run the page test ( among the quick access buttons). Give a price BT. Select the different values in the Combo Box control and click the "Calculate" button.
Our page operates properly now. However, instead of using a formula 3 times to calculate the price, a procedure can be used to perform this calculation.
  • Close the test page to go back to the code editor.

Creating and using a procedure

  • To create the procedure for calculating the amount IOT:
    1. Click the page background.
    2. Display the events associated with the page (F2).
    3. In the code editor, on the "Code" pane, in the "Procedures" group, expand "New" and select "New local procedure (Server)".
    4. In the window that is displayed, type the name of local procedure ("Calc_IOT") and validate ("Add" button).
    5. The new procedure local to the page is automatically created and its code is displayed in the code editor.
    6. Type the following code:
      PROCEDURE Calc_IOT(PriceBT, VATRate)
      cyIOT is currency
      cyIOT = PriceBT * (1 + VATRate/100)
      RESULT cyIOT

      Let's study this code:
      • The Calc_IOT procedure expects 2 parameters: the price before tax and the VAT rate.
      • This procedure declares a currency variable. This variable is used to store the calculated price IOT.
      • This procedure calculates the price IOT and returns the calculated value.
    7. Close the code editor.

Note

When creating a procedure, comments are automatically generated BEFORE the procedure code. These comments are mainly used to specify the content of parameters and return value.
It is important to fill these comments. Indeed, they will be automatically displayed in a tooltip when typing the call to the procedure in the code editor.
  • Let's now call the procedure from the calculation button.
    1. Select the "Calculate" button.
    2. Display the button code (F2 key for example).
    3. Replace the existing code by the following code:
      SWITCH COMBO_VAT..DisplayedValue
      // 5.5 %
      CASE 5.5
      EDT_PriceIOT = Calc_IOT(EDT_PriceBT, 5.5)
      // 10 %
      CASE 10
      EDT_PriceIOT = Calc_IOT(EDT_PriceBT, 10)
      // 20 %
      CASE 20
      EDT_PriceIOT = Calc_IOT(EDT_PriceBT, 20)
      END
This code calls the Calc_IOT procedure to calculate the amount IOT. Two parameters are passed in parameter: the price BT and the VAT rate. The result returned by the procedure is assigned to the EDT_Price_IOT control.
  • Let's now run the page test:
    1. Run the page test ( among the quick access buttons). Give a price BT. Select the different values in the Combo Box control and click the "Calculate" button.
    2. The amount BT is displayed.
    3. Close the test page.
Conclusion
This section allowed you to get familiar with the main concepts of WLanguage programming in WEBDEV. Several other features are available. Other ones will be presented later in this tutorial.
WLanguage is a very powerful language that allows you to develop applications that use:
Previous LessonTable of contentsNext Lesson
Minimum version required
  • Version 23
Comments
Click [Add] to post a comment