Fortran Fortran passing parameters to a function of one variable

AI Thread Summary
The discussion centers on how to define a function in Fortran that allows for a constant parameter while still being able to call it with a single variable. The original poster describes their current method of defining a function g(x,y) and wanting to create a function f(x) that uses g with y held constant. They express concern about the inefficiency of writing and reading y from a data file within the function. Participants suggest alternatives to improve efficiency, such as passing both x and y as parameters to the function f, which would simplify the process. However, it is noted that the existing code may not allow for this change. Another solution proposed is to use a common block, a method from Fortran 77, to share the value of y between the main program and the function without repeatedly passing it as an argument. This approach could also be adapted for Fortran 90, albeit with more complexity. The conversation emphasizes the need for efficient coding practices while maintaining compatibility with existing code structures.
Thereso
Messages
1
Reaction score
0
Hi all! I have a question about fortran. So far in my coding, if i wanted to define a function ( say f(x) ) I would manually set a grid for x, and then define f for each x value, so that i would have a vector for f and a vector for x. However, I'd now like to use the 'function' command, so i can call f for ANY x.

I'll do my best to explain my question: say I define a function g(x,y) by

g(x,y) = x**2 + y

and I want to define another function f(x), which is EQUAL to g(x,y) but with y held constant. I want to use a set of subroutines on the function f, but the problem is that all of the subroutines call f as a function of one variable and not two. So basically I want to pass y as a parameter into a function defining f(x). Later I will change y and then repeat. One way I can do it is to write y to a data file, and then read it from inside the function f:
program lolz
implicit none
real, external :: f
real y,c,x
! g(x,y) = x^2 + y
!calculate g(3.0,2.0)
y=2.0
open(26,file='y.dat',status='unknown')
write(26,*) (y)
close(26)
!test
x=3.0
write(*,*)"f(x) is",f(x)

end

function f (x)
implicit none
real f
real x,y
open(26,file='y.dat',status='unknown')
read(26,*) y
close(26)
f= x**2.0 +y
return
end function f


this program correctly returns "f(x) is 11.". But I'm sure that this constant opening and closing of data files is inefficient, there must be a better way of doing it? Thanks everyone :D
 
Technology news on Phys.org
Well...I don't quite get what you REALLY are trying to achieve...why can't you just pass x AND y to the function f? ...just declare the function f to take two parameters and return one...nothing wrong with that.

Other than that, you could use common block (up to fortran 77 style) to set y before you start calling the function with just x; or, you could go the module/use route (fortran 90 forward style). These two methods allow the function and the main program to share values without having to pass them around as arguments/parameters.
 
gsal said:
Well...I don't quite get what you REALLY are trying to achieve...why can't you just pass x AND y to the function f? ...just declare the function f to take two parameters and return one...nothing wrong with that.

That won't work, because the OP has some existing code (which presumably can't be changed) that called f() as a function with one variable.

The way to do this in fortran 77 is use a common block, something like

Code:
function f(x)
common/fcom/y
...
g(x,y)
...
end

program main
common/fcom/y
...
y = 2.0
...
f(x)
...
y = 3.0
...
f(x)
...
end
I expect you can achieve the same effect in Fortran 90 without using COMMON, but with far more typing :smile:
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
25
Views
3K
Replies
8
Views
2K
Replies
8
Views
4K
Replies
59
Views
11K
Replies
4
Views
2K
Back
Top