The following programs demonstrate the use of the BASED ON clause to declare types. Both the C and Pascal programs use the BASED ON clause to declare the function, job_name. These programs pass the value of the field JOB_CODE to the function. The function determines the job title associated with the job code and passes the job title back to the calling program. Note that in the C program a program variable, temp_job_name, is required so that the function can return the job title to the calling program. In Pascal, function values are returned to the calling program automatically.
1 – C Example
#include <stdio.h> DATABASE PERS = FILENAME "PERSONNEL"; typedef BASED ON JOBS.JOB_CODE job_code_type; typedef BASED ON JOBS.JOB_TITLE job_title_type; DECLARE_VARIABLE temp_job_name SAME AS JOBS.JOB_TITLE; job_title_type job_name(job) job_code_type job; { /* begin function */ READY PERS; START_TRANSACTION READ_ONLY; FOR FIRST 1 J IN JOBS WITH J.JOB_CODE = job strcpy (temp_job_name, J.JOB_TITLE); END_FOR; COMMIT; FINISH; return temp_job_name; } /* end of function */ main () { printf ("%s\n",job_name("APGM")); }
2 – Pascal Example
program based_on_clause (INPUT,OUTPUT); DATABASE PERS = FILENAME 'PERSONNEL'; type job_code_type = BASED ON JOBS.JOB_CODE; job_title_type = BASED ON JOBS.JOB_TITLE; function job_name (job : JOB_CODE_TYPE ) : JOB_TITLE_TYPE; begin {* function *} READY PERS; START_TRANSACTION READ_ONLY; FOR FIRST 1 J IN JOBS WITH J.JOB_CODE = job job_name := J.JOB_TITLE; END_FOR; COMMIT; FINISH; end; {* function *} begin {* main *} writeln (job_name ('APGM')); end.