Parameters and arguments...

Hey! I am new to C++ and I do not understand what parameters or arguments are. It would help if you could explain each one to me in terms where I could understand.

Thanks in advance...
Last edited on
Im using C++ for Dummies and i ran across a few sections that could help you. Enjoy and i hope this helps.

Firstly he says about "Understanding the Details of Functions"

A function is logically seperated block of C++ code. The function construct has the following form:
<return type> name(<arguments to this function>)
{
//...
return <expression>;
}

The arguments to a function are values that can be passed to the function to be used as input information. The 'return value' is a value that the function returns. For example, in the call to the function square(10), the value 10 is an argument to the function square(). The returned value is 100.



Secondly, he says about "Accessing the arguments to main():"

The first argument to main() is an array of pointers to null terminated character strings. These strings contain the arguments to the program. The arguments to a program are the strings that appear with the program name when you launch it. These arguments are also known as parameters. For example, suppose that i entered the following command at the MS-DOS prompt:

MyProgram file.txt /w

MS-DOS executes the program contained in the file Myprogram.exe, passing it the arguments file.txt and /w.

Consider the following simple program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main (int nNumberofArgs, char* pszArgs[])
{
       //print a warning banner
        cout << "The arguments to " << pszArgs[0] <<  "are:\n";
       //now write out the remaning arguments
       for (int i = 1; i < nNumberofArgs; i++)
       {
         cout << i << ":" << pszArgs[i] << "\n";
        }
system("pause");
return 0;
}
 
{


As always, the function main() accepts two arguments. The first argument is an 'int' that i have been calling (quite descriptively, as it turns out) 'nNumberofArgs'. This variable is the number of arguments passed to the program. The second argument is an array of pointers of type char[]*, which I have been calling pszArgs. Each one of these char* elements points to an argument passed to the program.




Credit:
C++ for Dummies: 5th Edition
by Stephen Randy Davis

Last edited on
Topic archived. No new replies allowed.