Functions and Functional Arguments

main( ) and its Arguments

The function main( ) like other functions may have arguments. In most applications, no arguments are listed and therefore its signature is void. However, in some cases it is desirable that main( ) have a non void signature consisting of an int: argc and an array of character pointers: argv When main( ) has a non void signature, its construct is:

image from book

 int main(int argc,char* argv[ ]) {     } 

image from book

The arguments: argc & argv are not keywords but traditional programmer words instead. The first will contain the number of strings that appear on the command line. The second variable is an array of pointers whose size is the first argument: argc.

For example, the following line of code will call Window's NOTEPAD program when typed in at the command line. It will load the source file TEST.CPP into the program NOTEPAD:

image from book


image from book

In this case, argc is 2 and argv is an array of pointers the first of which points to the array: "NOTEPAD.EXE" and the second points to the array: "TEST.CPP"


Passing Arguments by Value

When a function is called, the variable placed as an argument does not itself pass into the function's body. Instead the value of the variable is placed on the stack for each call of the function. The body may then remove the value when required by the code. We therefore call this: passing by value See BYVALUE.CPP

Passing by value has several undesirable characteristics to include that it:

  • does not change the argument value

  • may place excessive demand on the stack

  • slows the program when a large variable or a large number of variables must be passed to the stack

One solution to these problems is to use: global variables. The advantages of global variables are:

  • the variable value may be changed in any function

  • the variable is not placed on the stack of the functions using it.

While global variable may appear to be helpful characteristics, there may also be harmful ones as well because global variables:

  • use their memory throughout the program.

  • may be changed in any function.

Therefore another solution to global variable may be required.

Passing and Returning by Reference

A reference of a variable has been discussed in the notes above. A reference of a variable is another name for the variable. References may be used as arguments of functions called passing by reference as in the following functional prototype:

image from book

 void swap(int & , int &); 

image from book

When the reference operator is used, it appears in the prototype and in the definition of the function but not in the call. See BYREF.CPP. Notice that the reference operator does not appear in the function call.

When a reference is used with an argument:

  • the variable's value may be changed

  • the amount of stack needed is less

  • the program is faster

A problem that may occur with references is that since the operator does not appear in the call, someone reading the code may not realize that the argument may be changed.

Passing and Returning Pointers

A process similar to passing by references is called passing by pointers. In this case pointers are used as arguments of functions. Passing by a pointer is different from passing by reference in that

  • the memory to which the pointer points may be changed within the function

  • the pointer would have to be dereferenced.

Passing by reference is closer to passing by a constant pointer.

Passing by pointers is different from passing by value because it permits:

  • modification of multiple variables

  • faster function execution

  • less stack memory use.


Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
Year: 2007
Pages: 142 © 2008-2017.
If you may any questions please contact us: