Passing Arguments to Function Parameters

 

In the prototype

Int width(int feet);

Think of the parameter “feet” as representing a value, or message, that is delivered  to the called function by the caller. It is the caller’s way of telling the function exactly what is to be done.  In other words, the function may be designed to mail a letter. The parameter delivered to the function by the caller might be the details of what information is to be in that letter.

 

The calling parameter can be almost anything – a constant (literal), a function call, a variable, a pointer to an array or character string, or a true constant variable.

 

Argument Type Conversion:

The argument or arguments that are supplied to match the function’s parameters are subject to automatic conversion by the compiler, as necessary, so that they match the specified type of the parameter(s).  Obviously, the compiler will generate warnings or errors when the necessary conversion cannot be accomplished.

 

Pass by Value and by Reference:

Arguments are passed by Value as the default. If it is necessary that the parameter be passed by Reference, the calling program must instead create and pass a pointer or a reference to the object or variable.

 

When the function receives its parameter(s) by value, it is actually being given a copy of the actual variable. The function can modify that copy and the caller’s original is unaffected.

 

Conversely, when the caller passes a pointer to or a reference to the variable, the function gets the address of the actual variable and can then modify the original variable. This is obviously a more dangerous method of passing arguments that can lead to unexpected side effects.

 

 

Unnamed Function Parameters:

There may be reasons why a calling program must include arguments in the argument list that the function does not in fact want or need. This would be a situation involving “unnamed parameters”.

In such circumstances the function must be designed to accept the parameter, even though it does not use it. The compiler will warn you that the argument is not used by the function.

 

 

 

Default Arguments:

A C++ function may have one or more default argument values. This means that if the calling program does not provide an argument, the compiler will provide the default argument as provided in the function’s prototype. Conversely, if the calling program does provide the argument, the compiler accepts that argument and ignores the default argument.

 

Example:

void myfunct(int = 5, double = 1.23); //Here we have two default arguments.

 

myfunct(12. 3/45);      //caller provides the necessary arguments.

mfunct(3);                 //Compiler converts this to myfunc(3, 1.23);

myfunc();                   // Compiler converts this to myfunc(5, 1.23);

 

Rule for default arguments: You cannot omit an argument unless you omit all the arguments to its right, which themselves must have default arguments.

 

An interesting benefit of default arguments is that you can change the behavior of the function by making one change to the code and not have to change all of the places from which it is called.

 

Inline Functions:

An inline function is a function whose prototype is prefixed with the keyword inline. Declaring a function inline tells the compiler that you do not want the overhead associated with function calls. That is, you do not want every reference to the function to require pushing a return address onto the stack and pushing the calling arguments onto the stack and then jumping to the function code and jumping back to the point from which it was called, and popping the stack, etc. Instead, you want that function to be replicated at each point where it is needed. When the function is very small or compact, it makes sense to make it inline. Typically, the memory needed to replicate the code is less of a problem than the execution overhead.