Public Group

# Arguments

This topic is 4425 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

No, I'm not trying to start an argument... =P This is about arguments as used in programming, namely in C++ programming. I'm new to studying C++ and am seeking greater understanding of certain concepts. The currect book I'm reading doesn't explain the use of arguments in functions very well. Are they similar to variables? How often do you need to use arguments in a function? Could someone give me an example to explain how to use them? Thanks, Mage

##### Share on other sites
You mean

{
return a+b;
}

Where A and B are the arguments

##### Share on other sites
Arguments are things you need to pass to a function for it to work properly. You only need to 'use' arguments when necessary. For example, lets say I have the following:

int Sum(int x, int y){    int sum;  //holds the sum of x and y    sum = x+y;    return sum;}

This function takes two arguments - x and y. It then returns the sum of these two arguments. In your code, you would call this function like so (assuming a variable called answer is going to hold the sum):
int answer;answer = Sum(2, 3);

Now answer holds the value of 5. You can see how the function needed to be told the two arguments (2 and 3) in order to properly function. If you are still having trouble understanding what arguments are, consider the following. When your program hits the line 'answer = Sum(2,3);' what is really happening is that the x and y in the Sum function are replaced by 2 and 3, respectively, such as this:
int Sum(int x, int y) //when passed the values 2 and 3, x = 2, and y = 3.{    int sum;  //holds the sum of x and y    sum = x+y; //imagine that this looks like: sum = 2 + 3; because when you                     passed the arguments 2 and 3 to the function, x became 2 and y                   became 3    return sum;}

Perhaps a bit long-winded post, but hopefully you understood something of that.

##### Share on other sites
Arguments are a way of passing in variables from one function to another. Variables can either be passed by reference or by value.

Consider this piece of code
#include <iostream> // Req. for coutvoid foo(){  int x=1;  int y=2;  std::cout << "Initial value of x=" << x << " Initial value of y=" << y << "\n\n";    // Make the call to bar(int, int&)  bar (x, y);  std::cout << "Returned from function bar\n\n";  std::cout << "Value of x=" << x << " Value of y=" << y << "\n\n";}// Here, x is passed by value and y is passed by referencevoid bar(int x, int &y){  x++;  y++;   std::cout << "Inside of function bar\n\n";  std::cout << "Value of x=" << x << " Value of y=" << y << "\n\n";}

If you run the code above, you will notice that the y value has changed even after the bar function returns however the x has not... Only inside the bar function.

You can also pass in pointers aswell. Pointers are a special type of variable that holds the address of a variable.

#include <iostream>void foo(){  int iarray[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };  bar(iarray, 10);  // If my logic is correct (numbers should be reversed in iarray)  for (int i=0; i<10; i++)    std::cout << "i[" << i << "] is " << iarray << "\n";  }void bar(int *p, int size){  // Do something with p  for (int i=0; i<size; i++)    *(p + i) = size - i;}

Hope this helps you.

##### Share on other sites
Ahh, yes, I understand now. Mostly, anyway. Thanks!

1. 1
2. 2
Rutin
25
3. 3
4. 4
5. 5

• 10
• 10
• 13
• 20
• 14
• ### Forum Statistics

• Total Topics
632949
• Total Posts
3009367
• ### Who's Online (See full list)

There are no registered users currently online

×