3.1 · Local, global procedures & procedure collections

Level 3 · Advanced: in-depth WLangage

3.1Local, global procedures & procedure collections

Goal: Factor code into procedures, choose the right scope and organise reusable collections for the trading application.
Estimated time: 11 min · self-paced reading · If you only have 5 min: read “The essentials in 30s” + the diagram.

The essentials in 30 seconds

  • A procedure is a named and reusable block: local to a window, global to the project, or grouped in a collection by theme.
  • Parameters and the return value make a procedure generic; passing by value works on a copy, passing by reference modifies the original.
  • A procedure = one responsibility and an explicit name; global variables are limited and every copy-paste is turned into a procedure.

Running mini-case — Yacine factors his management calculations

Yacine develops the management application of an SME trading electrical equipment in Oran: customers, products, orders, invoices and stock. He copy-pasted the loyalty discount calculation into three screens, and every rate change forces him to correct it three times. He creates CalculeRemiseFidelite and CalculeMontantTTC as global procedures, grouped in the GestionCommerciale collection. Conversely, RéinitialiseSaisie stays a local procedure to the order window, because it is used only there. He also replaces his global variables with parameters and return values. He notes the reflex: duplication = procedure, scope according to use, collection by theme.

Self-contained case for this section: it returns in the course, diagram, script and 48h challenge of section 3.1.

1. Three scopes, three uses

A procedure is a named and reusable block of code: you write it once and call it as many times as needed. Its scope depends on where it is declared. A local procedure belongs to a window or a report: it can be used only in that screen. A global procedure is declared at project level and accessible everywhere: this is where reusable business rules live. A procedure collection groups procedures by theme (commercial calculations, date utilities, access to a service): it is the natural organisation of a project that grows.

2. Parameters, value and reference

A procedure receives parameters — the data it needs — and can return a value: it is then often called a function. Passing by value transmits a copy: the procedure can modify its parameter without touching the original variable. Passing by reference transmits the variable itself: the procedure can modify it. Confusing the two is a classic source of bugs, especially when Yacine believes a total was updated while he was working on a copy.

3. Quality: one responsibility, one name

A good procedure does one single thing and carries an explicit name that describes it: CalculeMontantTTC, ControleCodePostal. It stays short, receives what it needs through parameters and returns its result. Global variables are limited, because any screen can modify them and make behaviour unpredictable. Finally, as soon as Yacine is about to copy-paste code, he turns it into a procedure: it is the most reliable signal.

Common misinterpretation: “A local procedure is visible everywhere; parameters are always modifiable by the procedure; and global variables simplify maintenance”. No, three mistakes. A local procedure can be used only in its window or report: for cross-cutting use, you need a global procedure or a collection. A parameter passed by value is a copy: the procedure does not modify the original, only passing by reference allows it. And global variables, modifiable from anywhere, make the code unpredictable and debugging painful. Yacine prefers parameters, returns and named collections.
Scopes & reuse of procedures Local for a screen, global for the project, collection to organise by theme. Local procedurelimited to one window+Global procedureaccessible everywhere+Procedure collectiongrouped by theme Choose the scope according to use; avoid global variables.
Local for a screen, global for the project, collection to organise by theme.
Text version of the diagram (SEO / screen readers / translation): 1) Yacine, a WinDev developer at an SME trading electrical equipment in Oran, notices that the loyalty discount calculation is copied into three screens of his management application. 2) He turns it into a global procedure CalculeRemiseFidelite, with parameters and a return value, and files it in the GestionCommerciale collection with CalculeMontantTTC; conversely, RéinitialiseSaisie stays local to the order window. 3) He replaces global variables with parameters, chooses an explicit name for each procedure and checks whether passing by value or by reference is needed depending on whether he wants to modify the original. Lesson: a named, well-placed and well-parameterised procedure removes duplication and makes the application maintainable.

🔎 Concrete example — 🔎 Concrete example — Two global procedures, one single calculation

Yacine groups the commercial rules in the GestionCommerciale collection:

// GestionCommerciale collection — global procedures
PROCEDURE CalculeMontantTTC(nMontantHT est un monetaire, nTauxTVA est un reel = 19)
    RENVOIE nMontantHT * (1 + nTauxTVA / 100)

PROCEDURE CalculeRemiseFidelite(nCA est un monetaire, nAnciennete est un entier)
    SI nAnciennete >= 5 ALORS
        RENVOIE nCA * 0.05
    FIN
    RENVOIE 0

// FicheCommande window: explicit call, without global variable
nTTC est un monetaire = CalculeMontantTTC(SAI_MontantHT, 19)
nRemise est un monetaire = CalculeRemiseFidelite(CLI_CA, CLI_Anciennete)

Reading: the rule is written once and called from any screen.

Lesson: parameters and return document the dependency; no hidden global variable.

Section vocabulary

Procedure
Named and reusable block of code, written once and called as many times as needed.

Simple example: Yacine calls CalculeMontantTTC from the order window and the invoice window.

What it is not: code copied identically into three different events.

Reflex: duplication to remove → a procedure.

Global procedure
Procedure declared at project level and accessible from all screens and processing.

Simple example: CalculeRemiseFidelite lives in a global collection.

What it is not: a procedure declared in a single window and invisible elsewhere.

Reflex: rule used everywhere → global procedure.

Procedure collection
Project element grouping procedures by theme to organise a project that grows.

Simple example: GestionCommerciale gathers the discount, VAT and shipping calculations.

What it is not: procedures scattered without thematic grouping.

Reflex: organise by theme → a collection.

Passing by value
Transmission of a copy of the parameter: the procedure does not modify the original variable.

Simple example: Yacine passes an amount to a calculation procedure without altering the input.

What it is not: a procedure that directly updates the caller's variable.

Reflex: original intact → passing by value.

Passing by reference
Transmission of the variable itself: the procedure can modify the original value.

Simple example: Yacine has a table of lines filled by a procedure via a reference.

What it is not: a local copy lost at the end of the procedure.

Reflex: modify the original → passing by reference.

In practice — In practice — Structuring your code into procedures

  1. Spot a copy-pasted processing (or one about to be): it is the signal that it must become a procedure, as for Yacine.
  2. Create a global procedure if the rule serves several screens, local if it concerns only one window.
  3. Give it parameters and a return value rather than making it depend on global variables.
  4. Group procedures by theme in collections, with explicit names describing what they do.
Your code is organised into reusable, named and testable building blocks, without duplication — the basis of a maintainable application.

Step-by-step script — factor like Yacine (4 min)

1) Look for a calculation or a check copied into several screens. 2) Decide the scope: global for cross-cutting use, local for a single screen. 3) Write the procedure with its parameters and its return value. 4) File it in a thematic collection with an explicit name. 5) Replace the copies with calls and check that no global variable holds state.

Keep this reflex: if you are about to copy-paste, stop and create a procedure.

Copy-paste template — Yacine's procedures sheet

Signal     : duplication, length, complexity, need to test
Scope      : LOCAL (one window) / GLOBAL (the whole project)
Collection : group by theme (commercial, dates, services)
Parameters : value (copy) / reference (modifies the original)
Quality    : one responsibility, an explicit name, few globals

A discount copied three times

Yacine creates CalculeRemiseFidelite as a global procedure: a single correction now suffices.

A check specific to one screen

Yacine keeps ControleSaisieCommande as a local procedure to the order window.

A total not updated

Yacine thought he was modifying the original: he passes the parameter by reference and fixes the bug.

Procedures in disarray

Yacine groups calculations, dates and exports into three clear collections of the management app.

✓ Best practices

  • Turn every copy-paste into a procedure.
  • Choose the scope according to actual use.
  • Prefer parameters and returns to global variables.

✗ Common pitfalls

  • Putting a cross-cutting rule in a local procedure.
  • Confusing passing by value and by reference.
  • Accumulating global variables modifiable everywhere.

Key takeaways

  • A procedure is a named and reusable block that removes duplication.
  • Local (one window), global (the whole project) or grouped in a collection by theme.
  • Passing by value copies; passing by reference modifies the original.
  • One responsibility per procedure, an explicit name and few global variables.

48h challenge — apply it tomorrow

Like Yacine, open your application and spot a calculation copy-pasted in at least two screens. Turn it into a global procedure with parameters and a return value, then file it in a collection with an explicit name. Finally check whether you need passing by value or by reference, and remove the now useless global variable.

Section exit checklist
  • I can explain the difference between a local and a global procedure.
  • I know how to create a procedure collection by theme.
  • I distinguish passing by value and passing by reference.
Self-assessment 1-5: do I quickly spot the code to factor?Self-assessment 1-5: do I limit global variables?

Frequently asked questions

When should you create a procedure rather than write the code in place?

As soon as the code is used several times, a processing becomes long, or a block deserves a name to be understood. The number one signal remains duplication: if you copy-paste, or if you are about to, turn it into a procedure. A business rule — VAT calculation, loyalty discount, check of a code — will inevitably be used elsewhere: screen, report, import or web service.

Why should you limit global variables?

Because a global variable can be read and modified from anywhere, at any time: the code becomes unpredictable and the bugs very hard to locate. A procedure that depends on a global hides this dependency in its signature. Prefer parameters and return values: the procedure becomes reusable, testable and predictable.

Check your understanding

What signal forces you to turn code into a procedure?

Additional resources (optional, to go further)

Level 3 examFresh, harder questions to test what you've learned