IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

COBOL User-defined function


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
Vishwamurthy

New User


Joined: 11 Mar 2008
Posts: 57
Location: India

PostPosted: Mon Nov 17, 2008 7:38 pm
Reply with quote

Hi,
I would like to know the syntax for writing a COBOL User-defined function.
The function when called, should return something which I should be able to store into a WORKING-STORAGE variable.

I have searched, opened and found nothing in 158 of the 802 matching topics.

Do reply.

Thank you.
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8700
Location: Dubuque, Iowa, USA

PostPosted: Mon Nov 17, 2008 7:42 pm
Reply with quote

Have you tried searching the COBOL manuals for user-defined function? What results did you get?
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19243
Location: Inside the Matrix

PostPosted: Mon Nov 17, 2008 10:06 pm
Reply with quote

Hello and welcome to the forum,

What functionality do you want to provide?
Back to top
View user's profile Send private message
Vishwamurthy

New User


Joined: 11 Mar 2008
Posts: 57
Location: India

PostPosted: Tue Nov 18, 2008 9:32 am
Reply with quote

Hi,
on the internet, I got to know that there's a Paragraph 'FUNCTION-ID'. I've seen something like 'ROUTINE-ID' for an option in a multiple choice question in a COBOL Quiz (option may be wrong too). But neither of them is a COBOL Keyword. I've searched a few COBOL books to find description only about intrinsic functions.
No COBOL manual speaks about User-defined functions, surprisingly.

Any functionality..
lets say, if I call that User-defined function like
C = FUNCTION ADD1 (A, B), it should throw no errors and C should have some value. If A and B are both numeric, C should be equal to A + B after the statement is run.

Any help in this regard is appreciable.

Thank you.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19243
Location: Inside the Matrix

PostPosted: Tue Nov 18, 2008 10:12 am
Reply with quote

Hello,

To do what you want, i'd suggest you create a common subroutine (funcadd) that is called dynamically (rather than statically).

The CALL would name 2 input fields, 1 output field, and 1 return-status. When invoked, the code would validate the 2 input numbers and if numeric, add them placing the total in the "output" field. Any problems would be noted in the return-status.
Back to top
View user's profile Send private message
Vishwamurthy

New User


Joined: 11 Mar 2008
Posts: 57
Location: India

PostPosted: Tue Nov 18, 2008 10:18 am
Reply with quote

Thanks for the quick reply. Can you show me an example showing how we'll make use of the return-status, which I've never used before. How does the output variable come back to the main routine? is it a part of the argument list being called BY REFERENCE?

Thank you.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19243
Location: Inside the Matrix

PostPosted: Tue Nov 18, 2008 10:38 am
Reply with quote

Hello,

Quote:
Can you show me an example showing how we'll make use of the return-status,
For example, a return-status of zero could mean the function completed successfully, a 1 could mean the first value was not numeric, a 2 if the second value was not numeric and so on for whatever situations need a return-status.

In the called code, there would be a LINKAGE SECTION that describes the 4 fields. In the calling code, 4 similar fields would be defined and used in the CALL:
Code:
   CALL THE-FUNCTION USING FLD1, FLD2, THE-RESULT, RETURN-STATUS.

THE-FUNCTION is a working-storage field that contains the name of the called module.
Back to top
View user's profile Send private message
Vishwamurthy

New User


Joined: 11 Mar 2008
Posts: 57
Location: India

PostPosted: Tue Nov 18, 2008 11:11 am
Reply with quote

Thanks Scherrer.
That was really informative.
How do I implement a function like this: C = FUNCTION MAX1 (A, B) for getting the maximum of A and B, instead of going for a default function, this way: C = FUNCTION MAX (A, B). 'MAX1 (Arg. list)' would be a function defined by me.

Thank you.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19243
Location: Inside the Matrix

PostPosted: Tue Nov 18, 2008 11:18 am
Reply with quote

Hello,

By adding more code to the called function, you could incorporate whatever options/functionality you wanted. If the code was to be made more general porpose, the called code would need to be provided with which operation to perform as part of the calling parameters (add, max, etc).
Back to top
View user's profile Send private message
Vishwamurthy

New User


Joined: 11 Mar 2008
Posts: 57
Location: India

PostPosted: Tue Nov 18, 2008 11:24 am
Reply with quote

Thats true indeed, about CALLing a SUBROUTINE, but USER-DEFINED FUNCTIONS will never be CALLed using the CALL verb. In COBOL, can we create USER-DEFINED FUNCTIONS? to call them using the keyword FUNCTION..

Thank you icon_cool.gif
Back to top
View user's profile Send private message
agkshirsagar

Active Member


Joined: 27 Feb 2007
Posts: 691
Location: Earth

PostPosted: Tue Nov 18, 2008 12:10 pm
Reply with quote

Answer is NO.
Back to top
View user's profile Send private message
Vishwamurthy

New User


Joined: 11 Mar 2008
Posts: 57
Location: India

PostPosted: Tue Nov 18, 2008 1:32 pm
Reply with quote

Thank you. Scherrer for being very active, and AGKSHIRSAGAR for the reply.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19243
Location: Inside the Matrix

PostPosted: Tue Nov 18, 2008 10:50 pm
Reply with quote

Hello,

You're welcome icon_smile.gif

Quote:
can we create USER-DEFINED FUNCTIONS? to call them using the keyword FUNCTION
As was posted - no, but that does not prevent the need from being filled by invoking a subroutine rather than a "function".

The mechanism ("call" versus "function") is not nearly as important as delivering solutions that always work, are easily maintainable, and do not require unreasonable amounts of system resources.
Back to top
View user's profile Send private message
sfazel

New User


Joined: 16 Jan 2014
Posts: 1
Location: us

PostPosted: Thu Aug 07, 2014 11:59 pm
Reply with quote

A function by definition is a region residant object at execution time. A sub-program is a library call therefore it is loaded. Older COBOL used one giant load library to bring everything in.
Back to top
View user's profile Send private message
Bill Woodger

Moderator Emeritus


Joined: 09 Mar 2011
Posts: 7309
Location: Inside the Matrix

PostPosted: Fri Aug 08, 2014 12:49 am
Reply with quote

You got references for any of that?
Back to top
View user's profile Send private message
bam390

New User


Joined: 22 Oct 2016
Posts: 3
Location: Kingsport, TN

PostPosted: Sat Apr 16, 2022 7:18 pm
Reply with quote

I was looking in copies of the IBM manuals for User Defined Functions. I did not see any as of current "6.3".


What got me started was reading a recently purchased used copy of Gary DeWard Brown's Advanced COBOL.


User Defined Functions are coming to IBM Enterprise COBOL 6.4 !

Quote:
Support for user-defined functions to enable you to write your own functions and invoke them like intrinsic functions, improving code modularity and maintainability


Quote:

Support for user-defined functions to write your own functions and invoke them like intrinsic functions, improving code modularity and maintainability

Write your own functions using the new Enterprise COBOL construct, the user-defined function definition, and invoke them like intrinsic functions. As with many popular programming languages, COBOL 6.4 supports user-defined functions, which gives new COBOL programmers a familiar structure.

User-defined functions can only be called statically and must be defined within the same compilation group as the programs that call them.
User-defined functions is a COBOL 2002 standard feature. Support of programming language standards provides you with additional functionality so that you can modernize your application. It also allows for maximum portability of your source code among a variety of compiler implementations.

Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts COBOL sorting, with input GDG base COBOL Programming 7
No new posts Need help with ADABAS query (COBOL-AD... All Other Mainframe Topics 0
No new posts Replacing FILLER with FILLER<SeqNu... DFSORT/ICETOOL 2
No new posts System add a USER parm when job submi... TSO/ISPF 4
No new posts Compile Sp Cobol base COBOL Programming 1
Search our Forums:

Back to Top