examples about call by refrence

hi every body i need examples of func void one and its return value so ineed some examples in that
cout<<"thank s a lot";
closed account (S6k9GNh0)
I'm not sure on what your asking. Please improve your sentence structure and design so it's readable by everyone.

If your asking what I think your asking:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std; //meh

void AddOne(int &num)
{
   num += 1;
}

int main()
{
   int bob = 12;
   AddOne(bob);
   //bob now equals 13. See?
}


Explanation: In the code we declare a integer called bob. We then pass bob by reference to AddOne(). Notice AddOne() doesn't have a return. Instead of assigning bob a return value, because we passed by reference, we added one to the variable directly. It's the same as doing this in code:

1
2
3
4
5
int main()
{
   int bob = 12;
   bob += 1;
}


Another example is make a reference variable.

1
2
3
4
5
6
7
int main()
{
   int bob;
   int& sally = bob;
   sally = 44; //Now bob = 44 as well. See how that works?
   //That's why it's called a reference. It refers to another variable.
}


Also, passing by reference to functions is a little less heavy than copying variables over. Especially when you have huge classes and arrays. It's similar to passing by pointer which is also just as light. Simply knowing this and applying it can speed up code tremendously on large applications.
Last edited on
Topic archived. No new replies allowed.