C++ Functions and command-line parameters

In this C++ programming tutorial we will talk some more about functions. We also will take a look at command-line parameters and function prototypes.

Command-line parameters

In some cases you want to give a parameter at the start of a program. For example:


	# myprogram -i

The program “myprogram” will start and something extra will be done because of the command-line parameter -i (What it will do is up to you, this is just an example).

Now let’s make a program that will print the words that are typed behind the program at execution time.
(Compile the program with the name myprogram). Here is the example:


	#include<iostream>
	using namespace std;

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

		cout << argc << '\n';
		for (x=0; x < argc; x++)
			cout << argv[x] << '\n';

		return 0;
	}

After compiling the program “myprogram” , start it as follows:


     # myprogram aa bb cc dd

In this code, the main program accepts two parameters, argc and argv. The argv parameter is an array of pointers to a string that contains the parameters entered when the program was invoked at the UNIX command line. (Pointers will be explained in a later tutorial, for now it is enough to know that it points to an address in memory where the parameters are stored). The argc integer contains a count of the number of parameters. In this case five (myprogram,aa,bb,cc,dd.)

First the program will print the number of parameters given when the program was invoked (stored in argc).
This number will be used in a “for loop”. In this case it will print the number five.

The second cout statement will print the parameters given when the program was invoked, one by one. Try it! Command-line parameters can be used for many things.

Function Prototype

A function prototype declares the function name, its parameters, and its return type to the rest of the program. This is done before a function is declared. (In most cases at the beginning of a program.) To understand why function prototypes are useful, try the following program:


	#include<iostream>
	using namespace std;

	int main()
	{
		int answer;

		answer = Add(3,3);
		cout << answer;
		return 0;
	}

	int Add(int A, int B)
	{
		return A + B;
	}

This program will not compile on most compilers. (On some compilers you have to set a compiler option before it gives a warning or error.) The compiler will give an error, something like: ” Add, identifier not found”. (As we said before the function must be known before it’s called).

To solve this problem you can make use of function prototypes. If you use prototypes, the compiler can find the function and the compiler will check the parameters list (for wrong type usage).
Try the next example:


	#include<iostream>
	using namespace std;

	int Add(int,int); //Function prototype

	int main()
	{
		int answer;

		answer = Add(3,3);
		cout << answer;
		return 0;
	}

	int Add(int A, int B)
	{
		return A + B;
	}

The compiler can now find the Add function. It will also check if correct types (int,int) are used. This program should compile without a problem.

So in the future use function prototypes for every function you declare. It will save you a lot of time.

That’s all for this tutorial.

This entry was posted in C++ Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. Tweet This! Tweet This! or use to share this post with others.

There is currently one response to “C++ Functions and command-line parameters”

Why not let us know what you think by adding your own comment!

  1. adam on March 14th, 2013:

    Thanks, helped me alot.

    Note: Well-Organized website!

Leave a Reply: