Fortran "Error: Two main PROGRAMs at (1) and (2)” - function

I ran into this error while coding, and cannot find any legit reason for it. I have called and executed functions like this before. The code is:
implicit none
real et(5),k(5,4),v(5),f1,f2,f3,f4,f5,kk,t,h
integer i,j
do i=1,5
et(i)=i-1.0
v(i)=0.0
end do
h=0.01
kk=6.0
do j=1,1000
write(*,*) et(1),et(2),et(3),et(4),et(5),t
do i=1,5
et(i)=et(i)+h*v(i)
end do
k(1,1)=h*f1(et(1),et(2),t) !partcle,k_no
k(2,1)=h*f2(et(1),et(2),et(3),t)
k(3,1)=h*f3(et(2),et(3),et(4),t)
k(4,1)=h*f4(et(3),et(4),et(5),t)
k(5,1)=h*f5(et(4),et(5),t)
k(1,2)=h*f1(et(1)+0.5*k(1,1),et(2)+0.5*k(2,1),t+0.5*h)
k(2,2)=h*f2(et(1)+0.5*k(1,1),et(2)+0.5*k(2,1),et(3)+0.5*k(3,1),
+ t+0.5*h)
k(3,2)=h*f3(et(2)+0.5*k(2,1),et(3)+0.5*k(3,1),et(4)+0.5*k(4,1),
+ t+0.5*h)
k(4,2)=h*f4(et(3)+0.5*k(3,1),et(4)+0.5*k(4,1),et(5)+0.5*k(5,1),
+ t+0.5*h)
k(5,2)=h*f5(et(4)+0.5*k(4,1),et(5)+0.5*k(5,1),t+0.5*h)
k(1,3)=h*f1(et(1)+0.5*k(1,2),et(2)+0.5*k(2,2),t+0.5*h)
k(2,3)=h*f2(et(1)+0.5*k(1,2),et(2)+0.5*k(2,2),et(3)+0.5*k(3,2),
+ t+0.5*h)
k(3,3)=h*f3(et(2)+0.5*k(2,2),et(3)+0.5*k(3,2),et(4)+0.5*k(4,2),
+ t+0.5*h)
k(4,3)=h*f4(et(3)+0.5*k(3,2),et(4)+0.5*k(4,2),et(5)+0.5*k(5,2),
+ t+0.5*h)
k(5,3)=h*f5(et(4)+0.5*k(4,2),et(5)+0.5*k(5,2),t+0.5*h)
k(1,4)=h*f1(et(1)+k(1,1),et(2)+k(2,1),t+h)
k(2,4)=h*f2(et(1)+k(1,1),et(2)+k(2,1),et(3)+k(3,1),t+h)
k(3,4)=h*f3(et(2)+k(2,1),et(3)+k(3,1),et(4)+k(4,1),t+h)
k(4,4)=h*f4(et(3)+k(3,1),et(4)+k(4,1),et(5)+k(5,1),t+h)
k(5,4)=h*f5(et(4)+k(4,1),et(5)+k(5,1),t+h)
do i=1,5
v(i)=v(i)+(1.0/6.0)*(k(1,i)+2*k(2,i)+2*k(3,i)+k(4,i))
end do
end do
end program
real function f1(et(1),et(2),t)
f1=kk*(et(2)-et(1))+cos(2*t)
return
end function
real function f2(et(1),et(2),et(3),t)
f2=kk*(et(3)-2*et(2)+et(1))
return
end function
real function f3(et(2),et(3),et(4),t)
f3=kk*(et(4)-2*et(3)+et(2))
return
end function
real function f4(et(3),et(4),et(5),t)
f4=kk*(et(5)-2*et(4)+et(3))
return
end function
real function f5(et(4),et(5),t)
f5=kk*(et(4)-et(5))
return
end function
when compiled only gives this error :
implicit none
1
real function f1(et(1),et(2),t)
2
Error: Two main PROGRAMs at (1) and (2)
Can you please suggest debugging? Is there some issue in calling a function with arrays as argument?

You cannot name your variables or dummy arguments with parentheses:
real function f1(et(1),et(2),t)
You must name them normally with alphabetic characters and numbers only
real function f1(et1,et2,t)

Related

VHDL function with no parameters?

Is it possible to define a VHDL function with no parameters?
I'm trying to compute the number of bits in a record type, but to do so it is necessary to create an instance of that type. Therefore, since I can't just define const BITS = t_rec.a'length + t_rec.b'length, I would like to define a function function BITS() return natural that instantiates a record rec : t_rec and returns rec.a'length + rec.b'length. However, the compiler fails with unexpected '(') at the function declaration, before it even reaches the definition.
I would just include an unused, dummy parameter, but I suspect that the lint tools would complain.
Defining subtypes for each record field in advance of the record would be too verbose.
Complete example:
package pack is function BITS() return natural; end package pack; package body pack is function BITS() return natural is begin return 0; end function; end package body pack;
Error message:
pack.vhd:1:30: empty interface list not allowed
You should leave out the parentheses when defining a function with no inputs, eg:
function BITS return natural is
variable rec : rec_t;
begin
return rec.a'length + rec.b'length;
end function;
You should not include the parentheses when you call the function, either. eg:
report integer'image( BITS );
not
report integer'image( BITS() );
https://www.edaplayground.com/x/5fMB

Lua function needs to be assigned to variable

I have been trying to learn some lua recently, and I came across something I didn't understand with functions today, the code below didn't work
function iter()
local i=0
return function() print(i); i=i+1; end
end
iter()
iter()
I had to assign a variable to my function iter() and then call the variable before it would run:
function iter()
local i=0
return function() print(i); i=i+1; end
end
it=iter()
it()
it()
can anyone clarify why that is?
First of all, functions are just values. Your outer function is assigned to the variable iter. That function returns an anonymous function. ("Anonymous" just means you did not give the function a name before returning it.)
Secondly, an argument list in parentheses is basically an operator that calls a function (unless it's in a function declaration). When you use that operator, the function runs and the expression results in the return value.
In the statement iter(), you call a function and ignore its return value, so you never get to see the inner function run.
In the statement it = iter(), you end up with a named function called it. Every time you call it, it increments the i variable from inside the iter call that created it.
As a side note, it would be legal to say iter()() to immediately call the returned function. This wouldn't actually be useful in your case, because each call to iter returns a fresh closure with i starting at zero.

How to call a function within a function in Lua?

I have this code:
function test()
function awesome()
print("im awesome!")
end
function notawesome()
print("im not awesome.")
end
function notevenawesome()
print("im not even awesome...")
end
end
test().notawesome()
When I run this, the console prints
15: attempt to index a nil value
What I'm trying to do is call the function notawesome() within the function test(), how would I do that?
Your function is not returning anything (thus returning nil). Something like this should work:
function test()
function awesome()
print("im awesome!")
end
function notawesome()
print("im not awesome.")
end
function notevenawesome()
print("im not even awesome...")
end
result = {}
result["notawesome"] = notawesome
result["awesome"] = awesome
result["notevenawesome"] = notevenawesome
return result
end
test().notawesome()
#Axnyff explains what you might be trying to do. I'll explain what you did.
If you are familiar with other languages, please note that Lua does not have function declarations; It has function definitions, which are expressions that produce a function value when evaluated. Function definition statements, which you have used, are just shortcuts that implicitly include an assignment. See the manual.
When you run your code, a function definition is evaluated and the resulting function value is assigned to the variable test. Then the variable test is evaluated and its value is called as a function (and it is one).
When that function executes, three function definitions are evaluated and assigned to the variables awesome, notawesome, and notevenawesome, repsectively. It doesn't return anything.
So, when the result of calling test (nil) is indexed by the string "awesome", you get the error.
If you wanted to call the function value referred by the variable awesome, just do awesome().
If you want to achieve that instead to use the main function you can use an object:
test = {
awesome = (function()
return 'im awesome!'
end),
notawesome = (function()
return 'im not awesome.'
end),
notevenawesome = (function()
return 'im not even awesome...'
end)
}
To call your functions use this:
print(test.notawesome()) --> im not awesome.

Lua - find out calling function

In Lua, is it possible to know which function has called the current function.
For instance
function a()
get_calling_function() --Should print function b
end
function b()
a()
end
Is something like this possible?
Does the debug library have such functionality?
You could use debug.traceback():
function a()
print(debug.traceback())
end
function b()
a()
end
b()
which would print:
stack traceback:
./test.lua:45: in function 'a'
./test.lua:50: in function 'b'
./test.lua:53: in main chunk
[C]: in ?
you can use debug.sethook() to set up a hook that gets called each time certain special events happen in lua. it can be useful for things like this.
local debugInfo = { caller = nil, callee = nil }
function hook()
local info = debug.getinfo(2)
if info == nil then
debugInfo.callee = nil
return
end
-- we only want to watch lua function calls (not C functions)
if info.what ~= "Lua" then
debugInfo.callee = "C function"
return
end
debugInfo.caller = debugInfo.callee
debugInfo.callee = info.name
end
debug.sethook(hook, "c")
function caller1()
if debugInfo.caller ~= nil and debugInfo.callee ~= nil then
msg = debugInfo.callee.. " was called by ".. debugInfo.caller.. "!"
print(msg)
end
end
function caller2()
caller1()
end
caller2()
this prints 'caller1 was called from caller2!'
debug.sethook can handle 3 different characters in the second parameter so you can let it know when to notify you. 'c' means call your hook function any time a function is called in lua, 'r' means call your hook function every time a function returns in lua, and 'l' means call your hook function whenever lua processes a new line of code.
you could set this up to build your own custom stack trace if you really wanted to, and you could also use debug.getlocal() within your hook to even try to work out what arguments were passed to your called function.
edit for lhf. this is actually a much simpler way of doing what you're asking, if you don't need to track this and just need to know the context of how the function was called.
function caller1()
local current_func = debug.getinfo(1)
local calling_func = debug.getinfo(2)
print(current_func.name.. " was called by ".. calling_func.name.. "!")
end
function caller2()
caller1()
end

Function Returning Boolean?

I have simple function in VBA and I would need to check whether or not it has been successfully performed. I do not know VBA much, so I have no idea whether or not its possible. I want to do something like this: bool X=MyFunction().
I am using VBA in the QTP descriptive programming. This does not work:
Function A as Boolean
A=true
End Function
It says: Expected statement
But I cannot see any return type in my method etc.
function MyFunction() as Boolean
.....
.....
MyFunction = True 'worked
end function
dim a as boolean = MyFunction()
In VBA, you set a function's return value by assign to a variable with the same name as the function:
Function MyFunc() as Boolean
MyFunc = True
End Function
I suspect you may be using VBScript instead of VBA? If that's the case then VBScript doesn't state Type
this will work in VBScript
dim test,b
test = 1
b=false
msgbox ("Calling proc before function test=" & test)
msgbox("Calling proc before function b=" & b)
b = A(test)
msgbox ("Calling proc after function test=" & test)
msgbox("Calling proc after function b=" & b)
Function A(test)
test = test +1
A=true
End Function
or in your example
Function A()
A=true
End Function
There is no real way to check if a function worked in VBA. You must decide for yourself whether your function was successful. For example:
Function AFunction() as Boolean
on error goto 1
MyFunc = True
AFunction = True
Exit Function
1
AFunction = False
End Function
The above would let you know if the function failed. If it fails, it goes to the label '1' and then returns false, otherwise, it returns true.
If it isn't an 'error' you are looking for, then you must decide if the data returned or supplied is proper. One way of doing this is to return a specific value [error-code] which represents a failure.
Well if you have access to your function declaration, you can set a bool return type for it and return true or false depending on the execution.
Once your function returns a bool, your code will work.