Hi,

I would like to understand how arguments work in C++ functions.

I have this:

HRESULT PCSys::CheckActNode(WCHAR *iNod, ULONG iSeq, ULONG *ioHiber)

I don't understand it.
what is the difference between the input argument and the output argument and the inputoutput (3rd argument).
How can we differentiate between them and what happens when we call this function and pass three arguments?

In general you can only differentiate between them by looking at the code of the function or the function documentation.

However that said an argument passed by value, constant reference or constant pointer is an input argument.

An argument passed by non-constant reference or non-constant pointer could be an in, out or inout argument.

ok could you pls give me an example because I still don't understand it pretty well.

So let's say:
type func1( int x, double* y, ULong z)
how can i make any of the arguments in and another one out or inout

In your function x and z are input arguments because they are passed by value. What ever the function does it can not alter the value of x and z in the calling code.

*y (note not y) could already be an in, out or inout parameter because the function can alter the value of *y in the calling code but you can tell if it actually does without seeing the function code or description.

For a parameter to be an out or inout parameter it must passed a reference to the variable in the calling code, even if it does it the old C way of passing a pointer to the variable by value.

Your best bet is to write some code and experiment a bit with the various ways of passing a parameter to a function and seeing what happens if you try to alter the parameter in the function.

Note that the terms "IN", "OUT", and "INOUT" are just a description of what the parameters are used for. Only for human consumption and mean absolutely nothing to the actual program or function. When you learned C or C++ language you did not use those terms, but you did learn the same functionality. You can normally just ignore the terms IN, OUT and INOUT and just concentrate on what the function does, what parameters to pass, and what the return value(s) is(are).

That's what I really wanted to know.

So no matter what the arguments say, i, o or io that makes no difference?

So in all cases when I call the function and pass the parameters, those parameters will all be passed to the function and nothing read from the function's parameters, am i correct?

>>am i correct?
No. The function will use the parameters as described by IN, OUT or INOUT. That says how the parameters are used by the function. For example, if the parameter is INOUT then you need to set the value of the parameter then pass a pointer to it. For example the function could be something like this. Note that INOUT is in comments because there is no such c++ keyword.

BOOL foo( /*INOUT*/ int* n)
{
   *n = *n * 5;
   return TRUE;
}

int main()
{
    int x = 2;
    BOOL b = foo(&x);
    cout << "x = " << x << '\n';
}

If you are writing COM/ATL program using VC++ compiler then you might find IN, OUT, and INOUT keywords in the *.idl file, but that is a different language -- not c or c++.

Edited 6 Years Ago by Ancient Dragon: n/a

yes ATL/COM is what we are using but that's very new for me and there's no one to explain to me how this works.

for ex, I have in one of the C++ files the following
fRc = fiSource->Read(&fOffst, fLength, &fCount, &fData.m_string)

and the definition of read is the following:

virtual /* [helpstring][id] */ HRESULT STDMETHODCALLTYPE Read(
/* [out][in] */ long __RPC_FAR *ioOff,
/* [in] */ long iBufferL,
/* [out] */ long __RPC_FAR *oDatL,
/* [retval][out] */ BSTR __RPC_FAR *oBuffer) = 0;

Could you please explain how this works?

Thanks a lot I appreciate your help

It works just as I said before. The code snippet you posted is C code

You, the programmer, are responsible for writing the body of that Read() function. Apparently you told vc++ you wanted that to be a member of the ATL program you are writing (or someone else write for you).

The first parameter is a pointer to a long integer. Because it is [IN][OUT] its value must be set before passing it to Read(). The Read() function might use that for something and change its value to something else. What it does, I don't know since you didn't post the whole function.

The second parameter is a long integer passed by value because its only [IN].

The third parameter is [OUT], and looks like a buffer to hold some data results. The Read() function will most likely use it to read a file or something into that buffer.

The last parameter [RETVAL][OUT] is a BSTR which Read() will set to something. RETVAL is similar to [IN][OUT], but a function can only have one RETVAL parameter. Not sure myself why, except that's just the way it is.

This article has been dead for over six months. Start a new discussion instead.