Omo
Omo

Reputation: 1

Fortran: how to pass function name in a common block

In Fortran, is it possible to put a function in a common block as in: COMMON /myblock/ func (where x is some variable and func is a function).

My problem is that I would like to create a function s(x) that calls an external function func(x) but without passing func in s(x). For my project, s(x) has to be a function of only one variable, i.e., I do not want to do: function s(x,func) s=func(x)

Instead, I am hoping I could do: function s(x) common /myblock/ func s=func(x)

Or, if someone has some other suggestion using modules or something, this will be great.

Thanks in advance for any help.

o.

and then have the same common (myblock) in the subroutine that calls s(x).

Upvotes: 0

Views: 522

Answers (4)

M. S. B.
M. S. B.

Reputation: 29401

The modern way to do this is with a pointer to a function. The pointer could be passed as an argument or, for the design of this question, placed into a module. See, for example, Function pointer arrays in Fortran

Upvotes: 2

Jonathan Dursi
Jonathan Dursi

Reputation: 50947

Modern fortran standards prohibit this. From 5.5.2 of Fortran 2003:

A common-block-object shall not be ... a function name, an entry name...

And at any rate, using global variables to pass around non-constant data is just a terrible, terrible idea. As ja72 points out, you could do this with modules, but I refuse to demonstrate it with sample code.

Upvotes: 1

John Alexiou
John Alexiou

Reputation: 29274

I think you are not supposed to use common blocks for this, but modules. Put your function func in a module called myfunctions and then when needed insert at use myfunctions statement and thats it.

Upvotes: 1

Ted Hopp
Ted Hopp

Reputation: 234857

I don't believe that this is possible in any portable way. Some implementations may allow you to use some tricks to do it.

Upvotes: 2

Related Questions