- Отметить как новое
- Закладка
- Подписаться
- Отключить
- Подписка на RSS-канал
- Выделить
- Печать
- Сообщить о недопустимом содержимом
Hello,
Yet another problem: I have a 3rd party code in which there is a "chain" of optional argument passing done without checking with 'present'.
It looks like tnis (example, I/O is done to use unpredictable data, to avoid compiler optimizations):
module m0016
contains
subroutine f(j)
implicit none
integer,intent(in),optional::j
integer i
write(*,*)'f(): enter i:'
read(*,*)i
call g(i,j)
return
end subroutine f
subroutine g(i,j)
implicit none
integer,intent(in)::i
integer,intent(in),optional::j
write(*,*)'g(): i=',i
if(present(j)) write(*,*)'g(): j=',j
return
end subroutine g
end module m0016
program test
use m0016
implicit none
integer::j
call f()
write(*,*)'main: enter j:'
read(*,*)j
call f(j)
stop
end program test
This code compiles and surprisingly works...
However use of j in f() in call to g() without 'present' looks strange...
Is such a careless use of optional arguments correct and allowed by Fortran standards?
Ссылка скопирована
3 Ответы
- Отметить как новое
- Закладка
- Подписаться
- Отключить
- Подписка на RSS-канал
- Выделить
- Печать
- Сообщить о недопустимом содержимом
Yes, this is allowed. The present-status of optional arguments is carried through. I am not sure why you call this "careless", it can be a very useful property. It does require that an explicit interface be visible at all steps.
- Отметить как новое
- Закладка
- Подписаться
- Отключить
- Подписка на RSS-канал
- Выделить
- Печать
- Сообщить о недопустимом содержимом
Compiler doc (on kwrd OPTIONAL) doesn't clarify this question and examples attached also do not cover the matter (<- but please don't read this as a complaint).
That's why I decided to ask this Q.
And yes, I admit: this property is useful - it decreases amount of code, delegating the task to compiler or some additional runtime code (?) generated implicitly.
- Отметить как новое
- Закладка
- Подписаться
- Отключить
- Подписка на RSS-канал
- Выделить
- Печать
- Сообщить о недопустимом содержимом
See the additional topic "Optional Arguments" that is referenced at the bottom of the OPTIONAL page. The description of OPTIONAL is about the syntax term - the concept of omitted arguments is more complex and discussed elsewhere. I'll also comment that the compiler reference is not intended as a tutorial on the language.

Ответить
Параметры темы
- Подписка на RSS-канал
- Отметить тему как новую
- Отметить тему как прочитанную
- Выполнить отслеживание данной Тема для текущего пользователя
- Закладка
- Подписаться
- Страница в формате печати