Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining result data type in `function` statement

Well, let me say first why I want to do this. I frequently write code in C/C++, so for me it's very natural to define functions like:

vector<int> TestFunct (int a, int b){
<some code here>

return <result>;}

Now I'm learning Fortran, so I declare functions like this:

function TestFunc(a,b)
        integer, dimension(:)    :: TestFunc
        integer                  :: a
        integer                  :: b
        <some code here>
endfunction TestFunc

But I recently learned that the data type of the result can be defined in the function statement, like: <data_type> function TestFunc(a,b), which is much more natural for me, because I'm used to the similar C++ declaration.

The issue is that, when I', trying to define a vector (actually a integer, dimension(:), strictly talking) as the result data type, I have the ifort error #5082 (I will detail it in the next lines).

In an example, for the code:

real, dimension(:) function TestFunc(a,b)
         integer, intent(in)    :: a
         integer, intent(in)    :: b

         <more code here>

endfunction Testfunc

I get the output:

main.f90(23): error #5082: Syntax error, found ',' when expecting one of: ( * ) ( :: %FILL , TYPE INTEGER REAL COMPLEX BYTE CHARACTER CLASS DOUBLE ...
real, dimension(:) function TestFunc(a, b)
----^
main.f90(23): error #5082: Syntax error, found IDENTIFIER 'FUNCTION' when expecting one of: * :: , <END-OF-STATEMENT> ; [ / = => WITH
real, dimension(:,:) function TestFunc(a, b)
---------------------^

Hope I had clearly explained my issue.

EDIT: So, in order to sum up what I just said in one question: How I declare a vector (i.e.:integer, dimension(:)) as the returning data type in the function statement?

like image 290
Alfonso Santiago Avatar asked Oct 04 '13 14:10

Alfonso Santiago


1 Answers

You can define the return type before the function symbol, but it is limited. It is an older way meant only for simpler cases. The rules are somewhat obscure and I do not remember them by heart, but here is what the standard says:

The type and type parameters (if any) of the result of the function defined by a function subprogram may be specified by a type specification in the FUNCTION statement or by the name of the result variable appearing in a type declaration statement in the specication part of the function subprogram. They shall not be specified both ways. If they are not specied either way, they are determined by the implicit typing rules in eect within the function subprogram. If the function result is an array, allocatable, or a pointer, this shall be specied by specifications of the name of the result variable within the function body. The specifications of the function result attributes, the specification of dummy argument attributes, and the information in the procedure heading collectively define the characteristics of the function (12.3.1).

like image 186
Vladimir F Героям слава Avatar answered Sep 30 '22 15:09

Vladimir F Героям слава