PC SOFT

WINDEVWEBDEV AND WINDEV MOBILE
ONLINE HELP

Home | Sign in | English UK
New WINDEV 25 feature!
  • 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
Lesson 2.4. The procedures
This lesson will teach you the following concepts
  • Types of procedures.
  • Creating and calling a procedure.
  • Procedure parameters.
  • Procedure result.
  • Practical example.
Lesson duration

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).
  • Local procedure in 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 in a process or event: can only be used in the process or event where it was declared.

Remark

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 required 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 the value that will be handled in the procedure as parameter:
Multiply10(50)
You can pass 0 or several values as parameters to a procedure. These values can be of any type (as with variables).
The parameter is specified in the procedure declaration in the format of a variable. For example, for the Multiply10 procedure, the WLanguage procedure code is:
PROCEDURE Multiply10(P)
P=P*10
P is the parameter expected by the procedure.

Remark

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 result of multiplication.
The WLanguage code of the procedure is:
PROCEDURE Multiplication(Nb1 is int, Nb2 is int)
MyResult is int
MyResult = Nb1 * Nb2
RESULT MyResult
The WLanguage code used to call the procedure is:
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 statement of the procedure modifies the parameter value, the value of the variable corresponding to this parameter is modified.
Example:
  • The WLanguage code of the procedure is:
    PROCEDURE Test_Reference(P1)
    P1 = P1 * 2
  • The WLanguage code used to call the procedure is:
    T is int
    T = 12 // T is equal to 12 before the call
    Test_Reference(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 WLanguage code of the procedure is:
    PROCEDURE Test_value(LOCAL P1)
    // Local indicates that the parameter will be passed by value
    P1 = P1 * 2
  • The WLanguage code used to call the procedure is:
    T is int
    T = 12 // T is equal to 12
    Test_value(T)
    // T does not change

Remark

In the same procedure, some parameters can be passed by reference 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.

Remark

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 uses 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)
MyResult is int
MyResult = Nb1 * Nb2
RESULT MyResult
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 window, 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 control used to calculate and display the value IOT of the amount BT entered.
The calculation result will be displayed in the "Price IOT" control.
This window is as follows:
Using a procedure

Project used

To handle the different concepts of this lesson, we are going to create different windows. These windows will be created in the "WD My first windows" project.
  • Open the "WD My First Windows" project if necessary.
    To do so, in WINDEV's home page (Ctrl + <), click "Tutorial" and select "My first windows (Exercise)".

Implementation

  • Create a new blank window:
    1. Click Create an element among the quick access buttons. The element creation window appears: click "Window" then "Window". The window creation wizard starts.
    2. Select "Blank" and the "Phoenix" skin template.
    3. Validate. The window is automatically created in the editor. The window for saving an element is displayed.
    4. Specify the window title: "Procedures". The window name ("WIN_Procedures") is automatically proposed.
    5. Click "OK" to validate the information shown in the save window.
  • 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. In the search box at the top of the list, enter "Currency". Select the "Currency Edit" control and position the control in the window.
    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 edit 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. In the search box at the top of the list, enter "Currency". Select the "Currency Edit" control and position the control in the window.
    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 "UI" tab and choose "Read-only" for the initial status.
    4. Validate.
    5. Save the window.
  • 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 location where the control must be created (next to "Price BT").
    2. The Combo Box control creation wizard is displayed. This wizard allows you to easily define the main characteristics of the control.
    3. Select "Fill the combo box by programming or enter a list of values".
      Combo Box control creation assistant
    4. Go to the next step of the wizard.
    5. Keep the default options. Display the next steps.
    6. In the "Initial content" step, type the list of possible VAT values:
      • 5.5
      • Press Enter.
      • 10
      • Press Enter.
      • 20.
        Combo Box control creation assistant
    7. Go to the next step of the wizard and give a name (COMBO_VAT) and a caption (VAT) to the control.
      Combo Box control creation assistant
    8. Validate.
  • To create the "Calculate" Button control:
    1. On the "Creation" pane, in the "Usual controls" group, click Create a Button control.
    2. Click the location where the control will be created (below the Combo Box control for example).
    3. Click the control that was just created. The text displayed in the control 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 control.
    1. Display the code of "Calculate" control ("Code" from the popup menu).
    2. Write the following WLanguage code in the "Click BTN_Calculate" 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. Close the code window (click X at the top right corner of code editor).
    4. Run the window test ( Run the test of a window among the quick access buttons). Give a price BT. Select the different values in the Combo Box control and click "Calculate".
      Window test
Our window 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 window to go back to the code editor.

Creating and using a procedure

  • To create the procedure for calculating the amount IOT:
    1. Click the window background.
    2. Display the events associated with the window (F2).
    3. In the code editor, on the "Code" pane in the ribbon, in the "Procedures" group, expand "New" and select "New local procedure".
    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 window is automatically created and its code is displayed in the code editor.
    6. Write the following WLanguage code:
      PROCEDURE Calc_IOT(PriceBT, VATRate)
      cyIOT is currency
      cyIOT = PriceBT * (1 + VATRate/100)
      RESULT cyIOT
      Let's take a look at this WLanguage 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 window (click X at the top right corner of code editor).

Remark

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 control.
    1. Select the "Calculate" Button control.
    2. Display the events associated with the Button control (press F2, for example).
    3. In the "Click" event, replace the existing code with 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 to the procedure: the price BT and the VAT rate. The result returned by the procedure is assigned to the EDT_PriceIOT control.
    4. Close the code window (click X at the top right corner of code editor).
  • Let's now run the window test:
    1. Run the window test ( Run the test of a window among the quick access buttons). Give a price BT. Select the different values in the Combo Box control and click "Calculate".
    2. The amount IOT is displayed.
    3. Close the test window.
Conclusion
This section allowed you to get familiar with the main concepts of WLanguage programming in WINDEV. Several other features are available. You will discover many of them 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 25
Comments
Click [Add] to post a comment