Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,443 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,289 people online right now. Registration is fast and FREE... Join Now!




pass by value confusion

 
Reply to this topicStart new topic

pass by value confusion, why does function change values of args?

antmanbee
2 Feb, 2008 - 12:06 PM
Post #1

New D.I.C Head
*

Joined: 1 Feb, 2008
Posts: 7

It is my understanding that a function cannot change the values of args passed unless they are passed via pointer...
CODE

#include<iostream>
using namespace std;
int add(int, int);
int sum=0;
int a=0;
int b=0;
int main()
{
    
    cout<<"enter two numbers to be added"<<endl;
    cin>>a>>b;
    sum=add(a, b);
    cout<<a<<" plus "<<b<<"="<<sum<<endl;
    return 0;
}
int add(int , int )
{
    sum=a+b;
    a++;
    b++;
        return sum;
}


and here is output
enter two numbers to be added
4 5
5 plus 6=9
Press any key to continue
the values of a and b have been changed.....

however..if I write function definition as
CODE

int add(int a, int b)

the value of a and b are uneffected by a++ b++

what concept is eluding me?

damn! wrong forum

This post has been edited by antmanbee: 2 Feb, 2008 - 01:09 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Pass By Value Confusion
2 Feb, 2008 - 02:31 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,654



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
I will move this to the right forum. But in short parameters are passed by value. Meaning that a copy is made for the function alone to use which can be modified and played around with without effecting the original values. This keeps you from corrupting the data accidentally. When you pass by pointer (or by reference) you pass the actual memory address to the function and hence the actual variable itself. Changes made to it will then change the original value because they are the same variable.

Pass by reference and value are related to the topic of "scope". You can find much information on the Internet or in books that talk about variable scope and passing by value or reference.

I hope this answered your question. I am sure others will chip in with more information as they read this. Enjoy!

smile.gif
User is offlineProfile CardPM
+Quote Post

#include<wmx010>
RE: Pass By Value Confusion
2 Feb, 2008 - 02:45 PM
Post #3

D.I.C Head
**

Joined: 19 Jan, 2008
Posts: 75



Thanked: 1 times
My Contributions
taken from:
Book: Beginning C++ through Game Programming 2nd edition

CODE

// Swap
// Demonstrates passing references to alter argument variables

#include <iostream>

using namespace std;

void badSwap(int x, int y);
void goodSwap(int& x, int& y);

int main()
{
    int myScore = 150;
    int yourScore = 1000;
    cout << "Original values\n";
    cout << "myScore: " << myScore << "\n";
    cout << "yourScore: " << yourScore << "\n\n";
    
    cout << "Calling badSwap()\n";
    badSwap(myScore, yourScore);
    cout << "myScore: " << myScore << "\n";
    cout << "yourScore: " << yourScore << "\n\n";
        
    cout << "Calling goodSwap()\n";
    goodSwap(myScore, yourScore);
    cout << "myScore: " << myScore << "\n";
    cout << "yourScore: " << yourScore << "\n";

    return 0;
}

void badSwap(int x, int y)
{
    int temp = x;
    x = y;
    y = temp;
}

void goodSwap(int& x, int& y)
{
    int temp = x;
    x = y;
    y = temp;
}


This post has been edited by #include<wmx010>: 2 Feb, 2008 - 02:46 PM
User is offlineProfile CardPM
+Quote Post

pertheusual
RE: Pass By Value Confusion
2 Feb, 2008 - 05:17 PM
Post #4

D.I.C Head
**

Joined: 26 Jan, 2008
Posts: 233



Thanked: 4 times
My Contributions
The main thing that is affecting your program is variable scope.

When you write variables in your program, the location that you write them is very important.
In your code, you have "int a = 0;" outside of your methods. This means that they are "global" variables and can be accessed from any method. That is the reason that your code works right now. You are not actually passing the variables to your method at all.

In order to pass by pointer, you need to add "&" after the variable type, like in the example given be the person before me.

Global variables have their place, but try to keep variable declarations inside methods if possible. It makes programs much easier to follow.

Like this.

CODE

#include <iostream>
using namespace std;

int add(int& a, int& b);

int main()
{
    int a=0;
    int b=0;
    cout << "enter two numbers to be added"<<endl;
    cin >> a >> b;
    sum = add(a, b);
    cout << a << " plus " <<  b << "=" << sum << endl;
    return 0;
}
int add(int& a, int& b)
{
    int sum=a+b;
    a++;
    b++;
    return sum;
}



What are the "a++" and "b++" for?
User is offlineProfile CardPM
+Quote Post

antmanbee
RE: Pass By Value Confusion
6 Feb, 2008 - 05:14 AM
Post #5

New D.I.C Head
*

Joined: 1 Feb, 2008
Posts: 7

pertheusual....think I had function definition wrong
CODE

int add(int, int)

in fuction prototype only the data type need to be supplied but function definition has to be more specific?
data type(int) And name of variables...a..b
CODE

int add(int a, int b)


re a++ b++
CODE

int add(int , int )
{
    sum=a+b;
    a++;
    b++;
        return sum;
}

I was experimenting....wanted to see if the function would leave the original variables untouched and was baffled when the addition was done with the incremented values after sum=a+b....
helps to have definition written properly....
read up on this evening...functions refs and pointers.
was under the weather all weekend....
had the Metro Malaise....
which is a disease one gets when utilizing public transportation without face mask and rubber gloves.

thank you
antmanbee in Seattle

{next C++ question will be posted on appropriate forum)



User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 12:29PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month