Fortran 90/95 : passing parameters to functions

In summary, the conversation discusses the issue of finding the zero of a function that depends on a parameter. The suggested solution is to define the function with the parameter alpha and set it to a value when it is not needed. However, this is not possible due to the limitations of the subroutine fzero. Another suggestion is to pass the parameter as an additional parameter, but this is also not possible due to the number of parameters needed. The only viable solution is to use a module to define and access the parameter.
  • #1
pezze
8
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
  • #2
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.
 
  • #3
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.
 
  • #5
That doesn't quite work, I have many parameters, but thanks anyway!
 

Related to Fortran 90/95 : passing parameters to functions

What are the differences between passing parameters by value and by reference in Fortran 90/95?

Passing parameters by value means that the function receives a copy of the parameter's value, while passing by reference means that the function receives a reference to the original variable. This means that any changes made to the parameter inside the function will also affect the original variable in the calling program.

How do I pass an array to a function in Fortran 90/95?

To pass an array to a function in Fortran 90/95, you can either use the assumed-shape array notation or the explicit-shape array notation. The assumed-shape array notation is used to pass an array of any size, while the explicit-shape array notation is used to pass an array of a specific size.

Can I pass a variable number of parameters to a function in Fortran 90/95?

Yes, you can pass a variable number of parameters to a function in Fortran 90/95 using the keyword "optional" in the function's argument list. This allows you to call the function with a different number of arguments each time.

How do I pass a derived type to a function in Fortran 90/95?

To pass a derived type to a function in Fortran 90/95, you can use the "type" keyword in the function's argument list and specify the name of the derived type. The function will then receive a copy of the derived type's data.

Can I pass a function as a parameter to another function in Fortran 90/95?

Yes, you can pass a function as a parameter to another function in Fortran 90/95. This is known as a "function pointer" and it allows you to use the passed function as a normal variable inside the receiving function.

Similar threads

  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
4
Views
752
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
Back
Top