C function or sub ByRef (porting question)

C

Curbie

Are all variables passes by C to a function (or sub), passed ByRef? As
an example, this routine seems to want to change the value of the
passed variable "poa" in the calling routine?

Thanks,

Curbie

/* Function declaration */
float transpoa( float poa,float dn,float inc );

float transpoa( float poa,float dn,float inc )
{
int i;
float
b0=1.0,b1=-2.438e-3,b2=3.103e-4,b3=-1.246e-5,b4=2.112e-7,
b5=-1.359e-9,x,DTOR=0.017453293;

inc = inc/DTOR;
if( inc > 50.0 && inc < 90.0 )
{
x = b0 + b1*inc + b2*inc*inc + b3*inc*inc*inc +
b4*inc*inc*inc*inc
+ b5*inc*inc*inc*inc*inc;
poa = poa - ( 1.0 - x )*dn*cos(inc*DTOR);
if( poa < 0.0 )
poa = 0.0;
}
return(poa);
}
 
T

Tom van Stiphout

On Sat, 23 May 2009 10:06:30 -0400, Curbie <[email protected]>
wrote:

That is not a question about the Microsoft Access database product,
the subject of this newsgroup.

-Tom.
Microsoft Access MVP
 
D

Dirk Goldgar

Curbie said:
Are all variables passes by C to a function (or sub), passed ByRef? As
an example, this routine seems to want to change the value of the
passed variable "poa" in the calling routine?

Not that this question is on topic for this newsgroup, but the answer is no:
non-pointer variables passed by C are passed on the stack, which makes them
effectively "ByVal" arguments. The argument "poa" in your example function
can safely be modified inside the function without affecting the original
variable in the calling function at all.
 
C

Curbie

This isn't a good place to ask for programming advice for languages other
than vba or sql, the languages used by Microsoft Access databases.
My definition of a "good place to ask for programming advice" is where
I can get correct answers to honest questions. And thank you for your
reply!

BTW, the port is to VBA (Access), so, the C folks don't think Access
questions belong in C forums and the VBA folks don't think C questions
belong in Access forums. For future reference and for those who are
upset by my "improper" posting, where should I post these type of
questions?

Thanks, Chris & Dirk

Curbie

Option Compare Database
Option Explicit

Public Function transpoa(poa As Single, dn As Single, inc As Single)
As Single
' Calculates the irradiance transmitted thru a PV module cover.
' Uses King polynomial coefficients for glass from 2nd World
Conference Paper, July 6-10, 1998. Bill Marion 12/8/1998
Static b0 As Single ' polynomial coefficient
b0 (ASE300-init loads 1#)
Static b1 As Single ' polynomial coefficient
b1 (ASE300-init loads -0.002438)
Static b2 As Single ' polynomial coefficient
b2 (ASE300-init loads 0.0003103)
Static b3 As Single ' polynomial coefficient
b3 (ASE300-init loads -0.00001246)
Static b4 As Single ' polynomial coefficient
b4 (ASE300-init loads 0.0000002112)
Static b5 As Single ' polynomial coefficient
b5 (ASE300-init loads -0.000000001359)
Static DTOR As Single ' degress to radians
init loads 0.017453293
Static Init As Boolean ' function initalization
flag
Dim x As Single '

If Not Init Then GoSub Init ' if function variables
NOT initalized, do it

inc = inc / DTOR ' convert in incident
angle to degrees
If (inc > 50# And inc < 90#) Then ' if incident angle
between 50 and 90 degrees
x = b0 + b1 * inc
x = x + b2 * inc * inc
x = x + b3 * inc * inc * inc
x = x + b4 * inc * inc * inc * inc
x = x + b5 * inc * inc * inc * inc * inc
poa = poa - (1# - x) * dn * Cos(inc * DTOR) ' Adjust for relection
between 50 and 90 degrees
If (poa < 0#) Then poa = 0# '
End If ' if incident angle
between 50 and 90 degrees
transpoa = poa ' return irradiance
transmitted thru a PV module cover
Exit Function ' all done

Init: ' initalize function
variables
b0 = 1# ' initalize variable
b1 = -0.002438 ' initalize variable
b2 = 0.0003103 ' initalize variable
b3 = -0.00001246 ' initalize variable
b4 = 0.0000002112 ' initalize variable
b5 = -0.000000001359 ' initalize variable
DTOR = 0.017453293 ' initalize variable
Init = True ' function variables
initalized, we don't need to do it again
Return ' back to caller
End Function
 
G

George

I wouldn't pretend to speak for others, but it seems to me that we should
not interpret responses about the subject matter of this, or any other,
forum as indicating anyone is "upset" about a question on C posted in the
Access forum.

Rather it ought to be seen, and understood, for what it is--a friendly
suggestion that you might get more useful answers in a forum dedicated to
the programming language in question, C, because, well, because this forum
is not heavily populated by C experts.

Access uses VBA, not C. The vast majority of regulars in the Access forum,
therefore, are more conversant with VBA. There are regular contributors here
who can respond to C questions, and you've had two such responses, at least,
so far.

However, it is also true that a question in a forum dedicated to support of
the C language would probably (no, definitely) be seen by a much larger
group of C developers.

It's as simple as that.
 
C

Curbie

Chris,

Your time and answers are valuable to me do, I don't want to waste
them debugging my code, so I'll explain further the example I posted,
really just to prove that although this may have seemed like a C
question, it was really a VBA porting question.

I know the function I posted as an example is icky VBA coding but
there are some reasons I coded it that way.

The exploded "x=" expression is so I can study the expression in parts
for commenting understanding.

The init construct and the static variables deals with speed issues
while testing on (365 * 24) 239 or 1020 iterations.

Once I get the C code to match the results a faithful port of VBA
code, I can clean-up all the icky-ness, testing along the way against
known verified results.

I'm not too concerned with errors during the testing & debugging
process, but I have a much used error library I wrote back in V1.1 era
if I need it.

The following is a "off the top of my head" clean-up of what the
routine will probably look like.

Thanks for your time and help!

Curbie

Public Function transpoa(ByRef poa As Single, ByRef dn As Single,
ByRef inc As Single) As Single
' Calculates the irradiance transmitted thru a PV module cover.
' Uses King polynomial coefficients for glass from 2nd World
Conference Paper, July 6-10, 1998. Bill Marion 12/8/1998
Const b0 As Single = 1# ' polynomial
coefficient b0 (ASE300-init loads 1#)
Const b1 As Single = -0.002438 ' polynomial
coefficient b1 (ASE300-init loads -0.002438)
Const b2 As Single = 0.0003103 ' polynomial
coefficient b2 (ASE300-init loads 0.0003103)
Const b3 As Single = -0.00001246 ' polynomial
coefficient b3 (ASE300-init loads -0.00001246)
Const b4 As Single = 0.0000002112 '
polynomial coefficient b4 (ASE300-init loads 0.0000002112)
Const b5 As Single = -0.000000001359 '
polynomial coefficient b5 (ASE300-init loads -0.000000001359)
Const DTOR As Single = 0.017453293 ' degress
to radians init loads 0.017453293
Dim x As Single '

ErrorRouter.ErrorHandler Me.Name & ".transpoa "

inc = inc / DTOR ' convert incident angle
to degrees
If (inc > 50# And inc < 90#) Then ' if incident angle
between 50 and 90 degrees
x = b0 + (b1 * inc) + (b2 * inc ^ 2) + (b3 * inc ^ 3) + (b4 * inc ^
4) + (b5 * inc ^ 5)
poa = poa - (1# - x) * dn * Cos(inc * DTOR) ' Adjust for relection
between 50 and 90 degrees
If (poa < 0#) Then poa = 0# ' plane of array
irradiances less than zero, set to zero
End If ' if incident angle
between 50 and 90 degrees
transpoa = poa ' return irradiance
transmitted thru a PV module cover
Exit Function

ErrorRoute: ' error routing
ErrorRouter.ErrorHandler cf.Name & ".OpenDialog"
End Function
 
D

david

It used to be required to declare functions like that as
__stdcall

stdcall is the calling convention used by Windows (hence standard)
and VBA, rather than the standard C calling convention.

Actually, sometimes the VBA will appear to work even though
you are using the wrong calling convention: VBA automatically
checks the stack and fixes up stack errors after calls to DLLs.
However. you can still loose the function return value.

By the way, you used to be able to test it by trying the same DLL
with OpenOffice StarBasic. If it crashes, you have the declaration
wrong. StarBasic doesn't fix up stack errors.

(david)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top