Fortran可变范围的困难 [英] Difficulty with fortran variable scope

查看:129
本文介绍了Fortran可变范围的困难的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在fortran的一个函数中初始化了一个变量。我在另一个功能中使用它。但价值并不一样。在其他函数调用中,我正在获取垃圾值。



例如:

 
条目a( )
num_calls = 0

条目b()
num_calls = num_calls + 1

从条目b中,我得到了num_calls作为一些垃圾

解决方案在经典Fortran(Fortran 77或更早版本)中, d确保num_calls被定义在一个公共块中 - 可能是一个已命名的公共块。

  COMMON / magic / num_calls 

我没有使用过Fortran 90,所以我不知道它有什么额外的功能。它可能会保留为了向后兼容而命名的公共块,但也可能提供更好的一些。



我也欺骗了,并且使用了变量的隐式声明。完整的,我应该写:

  INTEGER * 4 num_calls 
COMMON / magic / num_calls

code>


I have intialized one variable in one of the fortran functions. I am using it in another function. But the value is not remains same. In other function call I am getting garbage value. How do i maintain the initialized value.

Ex:

entry a()
num_calls=0

entry b()
num_calls= num_calls + 1

From entry "b" i am getting num_calls as some garbage

解决方案

In classic Fortran (Fortran 77 or earlier), you'd ensure that num_calls is defined in a common block - probably a named common block.

COMMON /magic/ num_calls

I've not used Fortran 90, so I don't know what extra facilities it has. It likely retains named common blocks for backwards compatibility, but likely provides something better too.

I cheated, too, and used an implicit declaration of the variable. In full, I should write:

INTEGER*4 num_calls
COMMON /magic/ num_calls

这篇关于Fortran可变范围的困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆