- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It happens a lot of time that one of the inputs of a function or subroutine are not declared (almost always) like
function hh(a)
real,intent(in)::a(:)
but you have to define a lot of arrayes that are dependent on the size of a, so on every line you have to write x(size(a)), and it becomes wose when you are going to define multidimensional arrays, like x(size(a),size(a)),xh(size(a),size(a))
well it makes the code a little messy, is there any way to assign this size to an integer and use it?
function hh(a)
real,intent(in)::a(:)
but you have to define a lot of arrayes that are dependent on the size of a, so on every line you have to write x(size(a)), and it becomes wose when you are going to define multidimensional arrays, like x(size(a),size(a)),xh(size(a),size(a))
well it makes the code a little messy, is there any way to assign this size to an integer and use it?
1 Solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Although what you desire is not allowed in the Fortran language, you can achieve almost the same thing by using the preprocessor. Here is a version of your function that you may examine to see if the idea will work for you. You see slightly neater code, and the compiler sees the same messy code as before, which the preprocessor pass feeds it.
Do note that using a preprocessor does have its disadvantages and you should guard against them.
[fortran]function hh(a)Compile with ifort /Qfpp. You may also use Fortran style preprocessor directives instead of C style.
#define SZa size(a)
real,intent(in)::a(:)
! local arrays
real :: b(SZa)
integer :: ix(SZa,SZa)
hh=0
do i=1,SZa
b(i) = a(i)*a(i)
if(a(i).gt.0)then
ix(i,i)=1
hh=hh+a(i)
else
ix(i,i)=0
endif
end do
return
end function hh
[/fortran]
Do note that using a preprocessor does have its disadvantages and you should guard against them.
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nope. Sorry. By the time you "assign" anything, you can't declare the bounds.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Although what you desire is not allowed in the Fortran language, you can achieve almost the same thing by using the preprocessor. Here is a version of your function that you may examine to see if the idea will work for you. You see slightly neater code, and the compiler sees the same messy code as before, which the preprocessor pass feeds it.
Do note that using a preprocessor does have its disadvantages and you should guard against them.
[fortran]function hh(a)Compile with ifort /Qfpp. You may also use Fortran style preprocessor directives instead of C style.
#define SZa size(a)
real,intent(in)::a(:)
! local arrays
real :: b(SZa)
integer :: ix(SZa,SZa)
hh=0
do i=1,SZa
b(i) = a(i)*a(i)
if(a(i).gt.0)then
ix(i,i)=1
hh=hh+a(i)
else
ix(i,i)=0
endif
end do
return
end function hh
[/fortran]
Do note that using a preprocessor does have its disadvantages and you should guard against them.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Well, you could try this:
real function hh( a )
real, dimension(:) :: a
hh = hhh( a, size(a) )
contains
real function hhh( a, sza )
real, dimension(:) :: a
real, dimension(sza, sza, sza) :: work ! Automatic array for workspace
...
end function hhh
end function hh
Whether that improves your code sufficiently is up to you, but it avoids the repetition
of size(a) and size(a,1), size(a,2) in case of multidimensional arrays.
You could even leave out thedeclaration of a in hhh (and the argument), as it is
host-associated from the containing function hh.
Regards,
Arjen
real function hh( a )
real, dimension(:) :: a
hh = hhh( a, size(a) )
contains
real function hhh( a, sza )
real, dimension(:) :: a
real, dimension(sza, sza, sza) :: work ! Automatic array for workspace
...
end function hhh
end function hh
Whether that improves your code sufficiently is up to you, but it avoids the repetition
of size(a) and size(a,1), size(a,2) in case of multidimensional arrays.
You could even leave out thedeclaration of a in hhh (and the argument), as it is
host-associated from the containing function hh.
Regards,
Arjen

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page