View previous topic :: View next topic
|
Author |
Message |
Vishwamurthy
New User
Joined: 11 Mar 2008 Posts: 57 Location: India
|
|
|
|
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 |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8700 Location: Dubuque, Iowa, USA
|
|
|
|
Have you tried searching the COBOL manuals for user-defined function? What results did you get? |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
Hello and welcome to the forum,
What functionality do you want to provide? |
|
Back to top |
|
|
Vishwamurthy
New User
Joined: 11 Mar 2008 Posts: 57 Location: India
|
|
|
|
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 |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
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 |
|
|
Vishwamurthy
New User
Joined: 11 Mar 2008 Posts: 57 Location: India
|
|
|
|
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 |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
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 |
|
|
Vishwamurthy
New User
Joined: 11 Mar 2008 Posts: 57 Location: India
|
|
|
|
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 |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
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 |
|
|
Vishwamurthy
New User
Joined: 11 Mar 2008 Posts: 57 Location: India
|
|
|
|
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 |
|
Back to top |
|
|
agkshirsagar
Active Member
Joined: 27 Feb 2007 Posts: 691 Location: Earth
|
|
|
|
Answer is NO. |
|
Back to top |
|
|
Vishwamurthy
New User
Joined: 11 Mar 2008 Posts: 57 Location: India
|
|
|
|
Thank you. Scherrer for being very active, and AGKSHIRSAGAR for the reply. |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
Hello,
You're welcome
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 |
|
|
sfazel
New User
Joined: 16 Jan 2014 Posts: 1 Location: us
|
|
|
|
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 |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
You got references for any of that? |
|
Back to top |
|
|
bam390
New User
Joined: 22 Oct 2016 Posts: 3 Location: Kingsport, TN
|
|
|
|
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 |
|
|
|