Fortran 90/95 : passing parameters to functions

Click For Summary

Discussion Overview

The discussion revolves around the challenge of passing parameters to functions in Fortran 90/95, specifically in the context of a subroutine designed to find the zero of a function. Participants explore various methods to include additional parameters in the function definition while adhering to the constraints of the existing subroutine structure.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes a subroutine, fzero, that requires a function func(x) to find its zero but does not accommodate additional parameters like alpha directly.
  • Another participant suggests defining the function with the parameter alpha from the start and using a default value when it is not relevant.
  • A later reply clarifies that the subroutine fzero does not permit the function func(x) to have extra parameters, which complicates the proposed solution.
  • Another participant proposes passing a transformed variable, y = ax, into the function as an alternative approach.
  • One participant expresses that this transformation does not suffice due to the presence of multiple parameters in their case.

Areas of Agreement / Disagreement

Participants do not reach a consensus on a definitive solution to the problem of passing additional parameters to the function. Multiple competing views and suggestions are presented, but no single approach is agreed upon as the best method.

Contextual Notes

The discussion highlights limitations related to the subroutine's interface, specifically its inability to handle functions with additional parameters, which remains unresolved.

pezze
Messages
7
Reaction score
0
Hello, I have the following doubt, maybe someone can help me with this.
Suppose I have a general purpose routine that finds the zero of a function f(x), something like

subroutine fzero( func, x1, x2, xzero )
implicit none
real, intent(in) :: x1, x2 ! Upper and lower bounds where the zero lies
real, intent(out) :: xzero ! zero of the function here
interface
real function func(x)
implicit none
real, INTENT(in) :: x
end function func
end interface
...
f = func( x )
...
end subroutine fzero


My problem is the following. The function I want to find the zero depends on some parameter, say alpha, so it would be something like function(x,alpha)
Now, to use the previous general purpose routine I need to input the alpha in some other way. One possibility is to put "alpha" in a module, something like

module parameter
implicit none
real :: alpha
end module parameter

and then define the function

real func( x )
use parameter
...
end func

That would work. But do you know of any other way of doing the same thing?

Thanks for your help!
 
Technology news on Phys.org
Why not defing the function with the parameter alpha to begin with, then set it equal to some value like 1 when it doesn't matter.
 
Dr Transport said:
Why not defing the function with the parameter alpha to begin with, then set it equal to some value like 1 when it doesn't matter.

The problem is that the general purpose subroutine fzero does not allow the function func(x) to have additional parameters.
 
That doesn't quite work, I have many parameters, but thanks anyway!
 

Similar threads

  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K