A User-Defined Function in COBOL 
Author Message
 A User-Defined Function in COBOL

Laments for the lack of user-defined functions in COBOL are a regular
feature of this newsgroup.  However, I will claim that the code below
is a COBOL user-defined function.

This code does a right justified move of the data from one field to
another.  (Something else that is often missed in this newsgroup.)

You execute it with the statement

CALL 'ritejust' USING BY CONTENT   LENGTH OF source-field
                                             source-field
                                   LENGTH OF destin-field
                      BY REFERENCE           destin-field

Notes.

1. The format for the LENGTH OF value varies by compiler.  The format
given here (COMP-5 PIC S9(9)) is for Fujitsu's Power COBOL.  IBM's
COBOL II uses COMP PIC 9(9).  You may have to change the format to
agree with your compiler.

2. The format for link-fields - X(10000) - would appear to violate
memory protection.  However, there is only an error if you try to
access protected memory.  Just defining a field that would violate
protected memory is not an error.  As it is, this format only defines
the maximum field size.

 IDENTIFICATION DIVISION.
 PROGRAM-ID. ritejust.

* This program right justifies data.

* This program accepts four parameters
* - the length of the source field as produced by the
*   LENGTH OF function
* - the source field (up to 1000 characters)
* - the length of the destination field as produced by the
*   LENGTH OF function
* - the destination field (up to 1000 characters)

 DATA DIVISION.
 WORKING-STORAGE SECTION.
 01  sub1                                     COMP   PIC S9(9).

 LINKAGE SECTION.
 01  link-source-length                       COMP-5 PIC S9(9).
 01  link-source-field                               PIC X(1000).
 01  link-destin-length                       COMP-5 PIC S9(9).
 01  link-destin-field         JUST RIGHT            PIC X(1000).

 PROCEDURE DIVISION USING link-source-length
                          link-source-field
                          link-destin-length
                          link-destin-field.
 0000-main.
* Find the last character used in the source field
     PERFORM
       VARYING sub1 FROM link-source-length BY -1
       UNTIL sub1 <= 1 OR
             link-source-field(sub1:1) NOT = SPACE
       CONTINUE
     END-PERFORM
* right justify the data into the destination field
     MOVE link-source-field(1:sub1)
       TO link-destin-field(1:link-destin-length)

     GOBACK
     .

 END PROGRAM ritejust.

--
Ed Milne

Sent via Deja.com http://www.*-*-*.com/
Share what you know. Learn what you don't.



Thu, 21 Feb 2002 03:00:00 GMT  
 A User-Defined Function in COBOL

Quote:

>Laments for the lack of user-defined functions in COBOL are a regular
>feature of this newsgroup.  However, I will claim that the code below
>is a COBOL user-defined function.

The code you posted is a 'procedure', not a 'function'.  The difference
is that a function returns a value but a procedure does not, meaning
that a function can be included in an expression.  For example, the
COBOL intrinsic function 'COS' can be used like this:

  COMPUTE C = (A * A + B * B - 2 * A * B * FUNCTION COS(ANGLE)) ** .5

Such is not the case with a procedure, because the procedure can only
return values indirectly through a data field.
--

Sun Valley Systems    http://personal.bhm.bellsouth.net/~judmc
"For God so loved the world that He gave His only begotten Son, that
whoever believes in Him should not perish but have everlasting life."



Thu, 21 Feb 2002 03:00:00 GMT  
 A User-Defined Function in COBOL


Quote:

> >Laments for the lack of user-defined functions in COBOL are a regular
> >feature of this newsgroup.  However, I will claim that the code below
> >is a COBOL user-defined function.

> The code you posted is a 'procedure', not a 'function'.  The
difference
> is that a function returns a value but a procedure does not, meaning
> that a function can be included in an expression.  For example, the
> COBOL intrinsic function 'COS' can be used like this:

>   COMPUTE C = (A * A + B * B - 2 * A * B * FUNCTION COS(ANGLE)) ** .5

> Such is not the case with a procedure, because the procedure can only
> return values indirectly through a data field.
> --

> Sun Valley Systems    http://personal.bhm.bellsouth.net/~judmc
> "For God so loved the world that He gave His only begotten Son, that
> whoever believes in Him should not perish but have everlasting life."

Not necessarily.  From the Wedopedia

"(1) In programming, a named section of a program that performs a
specific task. In this sense, a function is a type of procedure or
routine. Some programming languages make a distinction between a
function, which returns a value, and a procedure, which performs some
operation but does not return a value.
Most programming languages come with a prewritten set of functions that
are kept in a library. You can also write your own functions to perform
specialized tasks. "

The documentation that I look at tend to use the wider interpretation
of a function.

In any case, the important point is that you can write COBOL code to
perform the same processing on any field.
--
Ed Milne

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.



Thu, 21 Feb 2002 03:00:00 GMT  
 A User-Defined Function in COBOL
: Laments for the lack of user-defined functions in COBOL are a regular
: feature of this newsgroup.  However, I will claim that the code below
: is a COBOL user-defined function.

: CALL 'ritejust' USING BY CONTENT   LENGTH OF source-field
:                                              source-field
:                                    LENGTH OF destin-field
:                       BY REFERENCE           destin-field

It is a sub-routine but is not a 'function', it is user-defined
but it is not a function.

The characteristics of a function include: returning a value
and being able to be used in an expression where the returned
value is substuted for the function call.

For example:

              COMPUTE the-result = 1 + ( 2 * myfunction(mydata) )

If it cannot be used that way it is not a function.

--



Thu, 21 Feb 2002 03:00:00 GMT  
 A User-Defined Function in COBOL

Quote:



>> >Laments for the lack of user-defined functions in COBOL are a regular
>> >feature of this newsgroup.  However, I will claim that the code below
>> >is a COBOL user-defined function.

>> The code you posted is a 'procedure', not a 'function'.  The difference
>> is that a function returns a value but a procedure does not, meaning
>> that a function can be included in an expression.  For example, the
>> COBOL intrinsic function 'COS' can be used like this:

>>   COMPUTE C = (A * A + B * B - 2 * A * B * FUNCTION COS(ANGLE)) ** .5

>> Such is not the case with a procedure, because the procedure can only
>> return values indirectly through a data field.

>Not necessarily.  From the Wedopedia

>"(1) In programming, a named section of a program that performs a
>specific task. In this sense, a function is a type of procedure or
>routine. Some programming languages make a distinction between a
>function, which returns a value, and a procedure, which performs some
>operation but does not return a value.

It is not a question of how any individual language is implemented,
but a question of technical terminology that is quite non-trivial.

Quote:
>Most programming languages come with a prewritten set of functions that
>are kept in a library. You can also write your own functions to perform
>specialized tasks. "

>The documentation that I look at tend to use the wider interpretation
>of a function.

Unfortunately, the people who write books often don't understand all
the ramifications of what they are writing.  The distinction between
the words 'procedure' and 'function' is very important.  To try and
make them synonyms is a big mistake; else how would we distinguish
between the two concepts without a lot of verbage?

Quote:
>In any case, the important point is that you can write COBOL code to
>perform the same processing on any field.

Yes, I agree. :-)
--

Sun Valley Systems    http://personal.bhm.bellsouth.net/~judmc
"For God so loved the world that He gave His only begotten Son, that
whoever believes in Him should not perish but have everlasting life."


Fri, 22 Feb 2002 03:00:00 GMT  
 A User-Defined Function in COBOL
FYI -

A) Check your current compiler's documentation. Many (but not all) current
COBOL products provide a "RETURNING" or "GIVING" option which allows a
"sub-routine" to return a value to its "invoking" code.

B) In the draft of the next Standard, there are two "enhancements' related
to the specific addition of user-defined functions:
 - Function-ID paragraph (and function proto-types)
 - RETURNING on the PROCEDURE DIVISION header (now called a "marker")

NOTE WELL:
   There is an "incredibly odd" situation in the draft of the next Standard.
The RETURNING phrase can be specified on the EXIT PROGRAM statement (or the
new GoBack statement) which allows a "traditional" COBOL called sub-routine
(aka procedure) to be used as a FUNCTION in *any* application written in any
programming language OTHER than COBOL - which ONLY allows a "function" (with
a function-id paragraph) to be used as an "inline" reference.

--
Bill Klein
    wmklein <at> ix dot netcom dot com



Fri, 22 Feb 2002 03:00:00 GMT  
 A User-Defined Function in COBOL
    I am afraid that I must join the Chorus, and disagree.  The purpose of a
label
is to identify.  Calling a procedure a function, even if you find an
authority that agrees with you serves no usefull purpose.

    Only when the secondary definition serves a purpose, such as what is a
"K", 1024 or 1000, do we have reason for confusion.  Note: 1000 vs 1024 is a
"religious" topic - no comments on that subject are necessary.


Quote:



> > >Laments for the lack of user-defined functions in COBOL are a regular
> > >feature of this newsgroup.  However, I will claim that the code below
> > >is a COBOL user-defined function.

> > The code you posted is a 'procedure', not a 'function'.  The
> difference
> > is that a function returns a value but a procedure does not, meaning
> > that a function can be included in an expression.  For example, the
> > COBOL intrinsic function 'COS' can be used like this:

> >   COMPUTE C = (A * A + B * B - 2 * A * B * FUNCTION COS(ANGLE)) ** .5

> > Such is not the case with a procedure, because the procedure can only
> > return values indirectly through a data field.
> > --

> > Sun Valley Systems    http://personal.bhm.bellsouth.net/~judmc
> > "For God so loved the world that He gave His only begotten Son, that
> > whoever believes in Him should not perish but have everlasting life."

> Not necessarily.  From the Wedopedia

> "(1) In programming, a named section of a program that performs a
> specific task. In this sense, a function is a type of procedure or
> routine. Some programming languages make a distinction between a
> function, which returns a value, and a procedure, which performs some
> operation but does not return a value.
> Most programming languages come with a prewritten set of functions that
> are kept in a library. You can also write your own functions to perform
> specialized tasks. "

> The documentation that I look at tend to use the wider interpretation
> of a function.

> In any case, the important point is that you can write COBOL code to
> perform the same processing on any field.
> --
> Ed Milne

> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.



Fri, 22 Feb 2002 03:00:00 GMT  
 A User-Defined Function in COBOL
On Mon, 6 Sep 1999 14:58:23 -0500, "William M. Klein"

Quote:

>FYI -

>A) Check your current compiler's documentation. Many (but not all) current
>COBOL products provide a "RETURNING" or "GIVING" option which allows a
>"sub-routine" to return a value to its "invoking" code.

FYI - I was suprised NOT to find this feature in 4.0 of Fujitsu COBOL
- I have not checked 4.2.  In 4.0 the "return" is stored in a reserved
word called "PROGRAM-STATUS", and returning is not accepted in a CALL
statement.


Sat, 23 Feb 2002 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. User-defined functions in micro-focus cobol 3.4?

2. Help with user defined function

3. user-defined functions

4. ??? User defined functions in NAWK ???

5. user-defined functions

6. nawk user-defined functions

7. User defined function help

8. User Defined Functions

9. How to pass stems to a user-defined function

10. How to pass stems to a user-defined function

11. user defined Functions

12. User defined Functions

 

 
Powered by phpBB® Forum Software