Example: dispcust.4gl (function query_cust)

This function retrieves a row from the customer table and displays it in a form.

Function query_cust:
  1 FUNCTION query_cust() RETURNS ()
  2   DEFINE l_custrec RECORD
  3      cust_num LIKE customer.cust_num,
  4      cust_name LIKE customer.cust_name,
  5      addr LIKE customer.addr,
  6      city LIKE customer.city,
  7      state LIKE customer.state,
  8      zipcode LIKE customer.zipcode,
  9      contact_name LIKE customer.contact_name,
 10      phone LIKE customer.phone
 11      END RECORD 
 12      
 13     SELECT cust_num, cust_name, addr,
 14            city, state,zipcode, 
 15            contact_name, phone
 16       INTO l_custrec.*
 17       FROM customer
 18       WHERE cust_num = 101
 19       
 20   DISPLAY BY NAME l_custrec.*
 21   
 22   MESSAGE SFMT("Customer num %1 retrieved.", l_custrec.cust_num)
 23   
 24 END FUNCTION
Note:
  • Line 1 is the beginning of the function query_cust. No variables are passed to the function. Not values are returned, therefore we specify RETURNS ().
  • Lines 2 thru 11 DEFINE a record l_custrec as LIKE columns in the customer database table, listing each record member separately.
  • Line 13 thru 18 SELECT ... INTO is used, since the statement will retrieve only one row from the database. The SELECT statement lists each column name to be retrieved, rather than using SELECT *. This allows for the possibility that additional columns might be added to a table at a future date. Since the SELECT list retrieves values for all the variables in the program record, in the order listed in the DEFINE statement, the shorthand INTO l_custrec.* can be used.
  • Line 20: The names in the program record l_custrec match the names of screen fields on the form, so DISPLAY BY NAME can be used. l_custrec.* indicates that all of the members of the program record are to be displayed.
  • Lines 22: The SFMT() function is used to build a string that is used by the MESSAGE instruction to display the information to the user.

There are no additional statements in the function, so the program returns to the MENU statement, awaiting the user's next action.