To continue with this content, please log in with your Data Access ID or create a new account.
Cancel Data Access ID
You may not be authorized to see this content. Please contact Data Access Europe for more information.
Cancel Data Access Europe
You are not authorized to see this content.
Cancel Data Access Europe
Next lesson:
Structs and Arrays
Cancel

Learn the Language

Lesson 4: Functions and Procedures

Now that we have seen how to control the flow of your application, let’s see how you can break your code into reusable blocks.

Demonstration

  1. Click PROJECTS NEW > PROJECT > BASIC PROJECT > OK
  2. Name your project, in this case FunctionsAndProcedures` > OK
  3. Clear out the code it generated and replace with:
    Use DfAllEnt.pkg
  4. Add the following line of code:
    Procedure DoTask Integer iNum String sDescription
  5. Add the following line of code:
    Send Info_Box (SFormat(“Performing task: %1, %2”, iNum, sDescription)) “Hello”
  6. Add the following line of code:
    Send DoTask 4 “Something”
  7. The idea behind procedures is that you can call them multiple times, with different parameters. So we can also:
  8. Add the following line of code:
    Send DoTask 12 “Something else”
  9. Click RUN (F5)
  10. Now you should see 2 info boxes pop-up. Click OK on the first box to reveal the second one.
  11. Procedures can also call other functions and procedures.  So if we define a function in the code here, we can call it from within another procedure.

12. We can call this function from within our DoTask Procedure with the Get function.

13. Click RUN (F5) to execute the program.

14. You should now see the info box pop-up saying: task: 20. This is because our defined value was 4 and we multiplied it by 5. The same goes for “something else”, 12*5= 60.

15. We can go through the program step by step, by using the breakpoints. Click next to a line of code to make one.

16. In case of a procedure you can choose to step into the procedure by clicking STEP INTO (F11) on the top.

17. By clicking STEP OVER (F10) you can step over the procedure.

18. Note: On the right side you will find the call stack. The call stack is a panel in the debugger. It shows different procedures and functions that have been calling each other. If you double click on an item in the list, the cursor will jump to that specific line in the code.

19. Let’s extend the example and add another procedure.

20. And again we will be calling this procedure from within the DoTask procedure.

21. Place a breakpoint at the line of code you just created at line 15 and click on RUN (F5), you now step into the procedure. In the locals panel you can see what is being generated at each step.

22. Note: Within DataFlex, by default, variables are passed by value. You can also pass variables by reverence, you just need to define it in the procedure.

23. Change sdescription into an expression

24. Click RUN (F5)

25. Now you see that sDescription has changed.

26. NOTE: you can use this system or two reasons, the first would be if you make use of big chunks of data, the second is to return multiple values from a single function.

27. Another example can be loaded in using the workspace explorer.

28. Workspace explorer > current project > MoreFunctionsAndProcedures

29. Place a breakpoint and click RUN (F5), go step by step through the program with STEP OVER (F10) and explore the code.