PowerBuilder functions, Events – Part 1

Recently one reader asked me provide some samples on how to call functions in PowerBuilder. First I thought, function is a function, just create it and call it. It’s like any other language, right? Then I realized there are some PB specific nuances, that I take for granted. What if some one was new to PB? Hence this post. I hope you find it useful.

Before we start on PB functions, let us get some terminology straight. A function typically is a sub-program unit that can be called from anywhere within the program. A function typically takes 0 or more arguments and returns a value. When the function does not return a value, we call it a procedure. This is typically marked by “void” type in many languages like C++, Java etc.

The arguments to the function may be passed by value or by reference. Most languages support passing arguments by value. When passed by reference, you will be able to pass the value back in the argument. This is like IN or INOUT parameters in some languages.
Just to be clear, In this discussion, I use parameters and arguments loosely here. Parameter is what you define the function with. Arguments are the values/variables you are passing in.

Oh yeah, in a procedural language like C, you would declare functions by themselves. While in a OO language like C++ or Java, you will declare the functions inside of objects (now they are referred to as Methods). C++ actually supports procedural constructs from C, thus allows for functions to be declared outside the objects as well. They can be called from anywhere and thus are referred to as Global functions.

With those definitions, let us see how functions are created and called. In C, a function is declared and defined as,

Declaration in C/C++:

return_type function_name( parameter list );

Definition of a function in C/C++:

return_type function_name( parameter list ) { body of the function }

Calling a function in C/C++:

If we defined a function,

max(int x, int y);
 /* calling a function to get max value */ ret = max(a, b);

Functions in PowerBuilder
Functions in PowerBuilder are no different. PB itself is based around C++ and thus its terminologies and usage is very similar. But, since PB is an IDE, you will have to define a function through a GUI interface as shown below:

 

 

2016-07-27 13_06_27-Greenshot

As you can see the several parts of the function definition fits the terminology mentioned above. Read the numbered description in the image for the description of the GUI editor for function.

Function Declaration in PB
PowerBuilder actually (forward) declares the function internally when you define a new function. If you edit the source for the w_test_func window, you will see the below lines for declaration and definition:

global type w_test_func from window

…………
// Function (Forward) Declaration

forward prototypes

public subroutine wf_say_hello (string as_message)

end prototypes

// Definition – this is what did in the GUI editor

public subroutine wf_say_hello (string as_message)

MessageBox(“Hello”, as_message)

end subroutine

…………

Well, after we finish entering/editing the function in the GUI editor, this is what PB saves in the code. That’s not very different from C declaration/definition above right? (of course, syntax is loosely based on Basic, but it definitely moved on from it’s basic days!)

Calling a function
In the simplest of usage, a function can be called as below:

// Call the function
wf_say_hello(“Hello Sam”)

If my function were to return a value then I might have,
int li_ret

li_ret = wf_say_hello(“Hello Sam”)

Note, above syntax is for calling the function within the Window object. If you have to access this function from other objects, then you may have to qualify it as,

li_ret = wf_test_func.wf_say_hello(“Hello Sam”)

This is only possible, if the window function (or method) was declared as Public.

A global function is not any different than the one above. Only difference is the access level will always be Public. Also, by convention we prefix the global functions with gf_.

With the introduction of a global function to get my name, I could rewrite my Window function call thus:

String ls_name
ls_name = gf_get_name()
li_ret = wf_say_hello(“Hello” + ls_name)

To be contd…

 

Advertisements

Comments, please?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s