4D Blog

Home Product Stop press! Accessing parameters not being passed is … possible

Stop press! Accessing parameters not being passed is … possible

October 4, 2021

Product

A method or function can have optional parameters. Sometimes the function is called with two parameters, and other times with three parameters. We’ve all dreamed of accessing the parameters without counting the parameters, checking if the parameter has been passed, or simply not having to check if the variable associated with the parameter has been initialized. This could make the code much simpler, and much more readable, without all the “If” or “Count parameters” lines.

Well, dream no more, and switch to 4D v19 R3! Managing method and function parameters has never been easier.

Access the parameters

Now, for parameters you have declared in a function or method’s prototype, if the parameter is not passed during the call, the variable is instantiated to the empty value. Even in compiled mode, you no longer have a runtime error for this. On this documentation page, you have a table with the correspondence for each type of variable.

In the following example, the function uses the $param1 and $param2 parameters without a check. If, when calling the function, the $param1 and/or $param2 parameters are not passed, 4D instantiates the text variable to an empty string:

// "concate" function class
Function concate ($param1 : Text ; $param2 : Text)
 Alert($param1+" "+$param2)

// "method1" method
$class.concate("Hello";" world") // Display: "Hello world"
$class.concate("Hello") // Display: "Hello "
$class.concate() // Display: " "

Be careful. In order for 4D to instantiate your variable, it needs to know the type of that variable. If you use the ${i} notation and you haven’t passed and declared the parameter, you will always get an execution error in compiled mode.

Initialize the parameters

We’ve just seen that if a parameter is not passed, its associated variable is instantiated to the empty value. So now you can use the Choose command to directly define a value if the parameter is not passed.

Here is an example with a method and the #DECLARE instruction:

// "myAlert" method
#DECLARE($param1 : Text ; $param2 : Text)
$param1:=Choose(Count parameters>=1 ; $param1 ; "Hello")
$param2:=Choose(Count parameters>=2 ; $param2 ; "world")
Alert($param1+" "+$param2)

// "method1" method
myAlert() // Display: "Hello world"

What about the components?

To benefit from this simplification, the method called and the calling method must be recompiled. So in the case of a method shared between the host base and a component, both projects need to be recompiled.

But, how about compiled components, whose sources I don’t have? Don’t worry, you can still use them as before. No need to change your code.

What’s next?

This new feature gives you plenty of ideas for your methods and functions. Feel free to share your tips on the forum.

Discuss

Tags Language, Programming, v19 R3, v20

Latest related posts

  • May 20, 2025

    Automatic Refresh of OEM Build Licenses

  • May 16, 2025

    New class to handle incoming TCP connections

  • April 29, 2025

    Discover your AI-powered writing assistant in 4D Write Pro

Vanessa Talbot
Vanessa Talbot
• Product Owner •Vanessa Talbot joined 4D Program team in June, 2014. As a Product Owner, she is in charge of writing the user stories then translating it to functional specifications. Her role is also to make sure that the feature implementation delivered is meeting the customer need.Since her arrival, she has worked to define key features in 4D. She has worked on most of preemptive multi-threading new features and also on a very complex subject: the new architecture for engined application. Vanessa has a degree from Telecom Saint-Etienne. She began her career at the Criminal Research Institute as a developer for the audiovisual department. She has also worked in media and medical fields as expert in technical support, production as well as documenting new features.
  • Deutsch
  • Français
  • English
  • Português
  • Čeština
  • Español
  • Italiano
  • 日本語

Categories

Browse categories

  • 4D View Pro
  • 4D Write Pro
  • 4D for Mobile
  • Email
  • Development Mode
  • 4D Language
  • ORDA
  • User Interface / GUI
  • Qodly Studio
  • Server
  • Maintenance
  • Deployment
  • 4D Tutorials
  • Generic
  • 4D Summit sessions and other online videos

Tags

4D-Analyzer 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R8 20 R9 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Formula Google Listbox Logs Mail Network Objects ORDA PDF Pictures Preemptive Programming Qodly Studio REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor

Tags

4D-Analyzer 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R8 20 R9 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Formula Google Listbox Logs Mail Network Objects ORDA PDF Pictures Preemptive Programming Qodly Studio REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor
Subscribe to 4D Newsletter

© 2025 4D SAS - All rights reserved
Terms & Conditions | Legal Notices | Data Policy | Cookie Policy | Contact us | Write for us


Subscribe to 4D Newsletter

* Your privacy is very important to us. Please click here to view our Policy

Contact us

Got a question, suggestion or just want to get in touch with the 4D bloggers? Drop us a line!

* Your privacy is very important to us. Please click here to view our Policy