Hello, could someone be so kind as to give me a short tutorial on how to use a function that accepts multiples values and parameters using references. I need to create a function that accepts an int and a double and squares both values by using references. I'm not sure how to create a function that accepts multiple arguments let alone of different types. Thank you!

>>I'm not sure how to create a function that accepts multiple arguments.

Its easy, its very same as a single argument function. Compare these 2 functions.

void one(int a){ /* do something with a */}
void two(int a, int b){ /* do something with a and b */ }

int main(){
  one(5); //call the one parameter function
  two(1,4); //call the two parameter function
}

That should get you started.

>>I'm not sure how to create a function that accepts multiple arguments.

Its easy, its very same as a single argument function. Compare these 2 functions.

void one(int a){ /* do something with a */}
void two(int a, int b){ /* do something with a and b */ }

int main(){
  one(5); //call the one parameter function
  two(1,4); //call the two parameter function
}

That should get you started.

Ok, thank you! But another problem I have is that is has to accept an int type and also a double type. Is there something special I have to do now? And also, I have to use references for this. Meanwhile, I'm going to work on it now with your examples.

Thanks!

>>But another problem I have is that is has to accept an int type and also a double type

There are couple of ways to do this. It depends on what you know. Have you
heard of function overloading? Here is an example :

void print(int a){ cout << a << endl; }
void print(double b){ cout << b << endl; }

int main(){
 int a = 5;
 double b = 3.14;
 print(a); //call the print(int);
 print(b); //call the print(double);
}

>>have to use references for this

Using reference is not that hard. You just need to understand what it means to use reference.
Here is an example :

void nonRef(int a){ a = 0; }
//use the '&' to mean pass-by-reference
void ref(int& b){ b = 0; }

int main(){
 int a = 123;
 nonRef(a); //a = 123 still because nonRef makes a copy of the variable a
 cout << a << endl;
 ref( a ); //a = 0 because ref does NOT use a copy of the variable a, instead it uses the actual variable a.
  cout << a << endl;
}

Edited 6 Years Ago by firstPerson: n/a

Why didn't you continue this thread?http://www.daniweb.com/forums/thread294268.html

Sorry I was just trying to get a better understanding of the concepts of functions and references. After reading your response to the previous thread I was confused and wasn't sure what you were trying to tell me. I think my problem lies with not understanding functions and multi values period let alone using references. I was wanting someone to give me sort of a tutorial on this before I went back to tackling the problem again. I sure do thank you and everyone else who has helped.

>>But another problem I have is that is has to accept an int type and also a double type

There are couple of ways to do this. It depends on what you know. Have you
heard of function overloading? Here is an example :

void print(int a){ cout << a << endl; }
void print(double b){ cout << b << endl; }

int main(){
 int a = 5;
 double b = 3.14;
 print(a); //call the print(int);
 print(b); //call the print(double);
}

>>have to use references for this

Using reference is not that hard. You just need to understand what it means to use reference.
Here is an example :

void nonRef(int a){ a = 0; }
//use the '&' to mean pass-by-reference
void ref(int& b){ b = 0; }

int main(){
 int a = 123;
 nonRef(a); //a = 123 still because nonRef makes a copy of the variable a
 cout << a << endl;
 ref( a ); //a = 0 because ref does NOT use a copy of the variable a, instead it uses the actual variable a.
  cout << a << endl;
}

I've heard of function overloading but know nothing about it I haven't gotten to that in my studies yet.

Sorry I was just trying to get a better understanding of the concepts of functions and references. After reading your response to the previous thread I was confused and wasn't sure what you were trying to tell me. I think my problem lies with not understanding functions and multi values period let alone using references. I was wanting someone to give me sort of a tutorial on this before I went back to tackling the problem again. I sure do thank you and everyone else who has helped.

It's okay, just didn't want to you to think that you couldn't ask for clarification. I'm not sure what you mean by multi values. Functions can take any number of arguments (parameters). There is definitely a finite number that you can have but it depends on how large the stack is, so don't worry about that for now.

Functions are analogs to their counterparts in math. y = f(x) takes one value of x and transforms it into a y value. You could also have z = f(x,y) which takes a pair of x and y and transforms it into a third value.

A function in C or C++ can only return one value. Therefore we have to resort to "trickery" to get more than one thing back. It would be like if we had y = f(x) in mathematics which would not only return y, but would also change the value of x for the next time we needed to use it.

Forget about the function returning an int for the time being. Imagine a simple swap function:

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

and in main()

int main()
{
    int a = 3,b = 1;
    myswap(a,b);
    std::cout<<"A= "<<a<<"B= "<<b<<std::endl;

}

Were this to be an ordinary function (without the &s in the definition, a would be copied into x, b would be copied into y and the swap would happen with their copies, meaning that once you returned to main the changes would be gone. With a reference no copies are made and essentially x and y are a and b. Not a copy, not a standin, but represent the very same a and b from main. When the swap is made, the values are preserved so that when you get back to main a has 1 for a value and b has 3.

Let me know if that's helped or hurt the situation :) If it's still giving you problems, try explaining it to me as far as you understand it.

Assuming you have some experience with pointers this has a great summary in the top post about references versus pointers. If you don't have any experience with pointers, look up a swap function using them and note how it's different from the one I showed you.

It's okay, just didn't want to you to think that you couldn't ask for clarification. I'm not sure what you mean by multi values. Functions can take any number of arguments (parameters). There is definitely a finite number that you can have but it depends on how large the stack is, so don't worry about that for now.

Functions are analogs to their counterparts in math. y = f(x) takes one value of x and transforms it into a y value. You could also have z = f(x,y) which takes a pair of x and y and transforms it into a third value.

A function in C or C++ can only return one value. Therefore we have to resort to "trickery" to get more than one thing back. It would be like if we had y = f(x) in mathematics which would not only return y, but would also change the value of x for the next time we needed to use it.

Forget about the function returning an int for the time being. Imagine a simple swap function:

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

and in main()

int main()
{
    int a = 3,b = 1;
    myswap(a,b);
    std::cout<<"A= "<<a<<"B= "<<b<<std::endl;

}

Were this to be an ordinary function (without the &s in the definition, a would be copied into x, b would be copied into y and the swap would happen with their copies, meaning that once you returned to main the changes would be gone. With a reference no copies are made and essentially x and y are a and b. Not a copy, not a standin, but represent the very same a and b from main. When the swap is made, the values are preserved so that when you get back to main a has 1 for a value and b has 3.

Let me know if that's helped or hurt the situation :) If it's still giving you problems, try explaining it to me as far as you understand it.

Ok, thanks for understanding. First off, I'm a bit confused about the void type that's declared for the myswap function. They say void is used for types that do not return values, but doesn't it return a value? I would think that it returns the value of a and b by looking at it. So you can see how much I understand this, not very much. Next, I understand how the swap takes place and a and b are going to swap with x and y. Now if I were to replace (int & y) with (double & y) would that create an error? Because I am trying to pass an int and a double to the function. Or, will the double convert automatically?

Assuming you have some experience with pointers this has a great summary in the top post about references versus pointers. If you don't have any experience with pointers, look up a swap function using them and note how it's different from the one I showed you.

By the way, I looked at a swap function using pointers and the difference that I see right off is the * alongside the variables inside the swap function.

Ok, thanks for understanding. First off, I'm a bit confused about the void type that's declared for the myswap function. They say void is used for types that do not return values, but doesn't it return a value? I would think that it returns the value of a and b by looking at it. So you can see how much I understand this, not very much. Next, I understand how the swap takes place and a and b are going to swap with x and y. Now if I were to replace (int & y) with (double & y) would that create an error? Because I am trying to pass an int and a double to the function. Or, will the double convert automatically?

In this case there's nothing being returned, that is the function has no value when we've returned to main. Don't necessarily think of the parameters you pass in as being returned, it's almost as if you would be using the variables defined in main right in the swap method itself.

If you were to replace one of the ints with a double it would give you a warning just because of the nature of the function I created (assigning one to the other would truncate the double to it's integer portion when it was assigned it to the int). For what you intend there should be no problem.

By the way, I looked at a swap function using pointers and the difference that I see right off is the * alongside the variables inside the swap function.

Right, a benefit of the references is you don't have to do the dereferencing (confusingly) that you would have to do with the pointer. Like that link I posted was saying, a reference is a pointer with these added benefits (can't be reassigned, etc.).

In this case there's nothing being returned, that is the function has no value when we've returned to main. Don't necessarily think of the parameters you pass in as being returned, it's almost as if you would be using the variables defined in main right in the swap method itself.

If you were to replace one of the ints with a double it would give you a warning just because of the nature of the function I created (assigning one to the other would truncate the double to it's integer portion when it was assigned it to the int). For what you intend there should be no problem.

Right, a benefit of the references is you don't have to do the dereferencing (confusingly) that you would have to do with the pointer. Like that link I posted was saying, a reference is a pointer with these added benefits (can't be reassigned, etc.).

Just wanted to let you know that I got it figured out, with your help of course. Thanks to you and everyone else who pitched in on this. After reading your posts it just clicked in my mind all of a sudden and I did some coding and it worked. Now I understand the concept way better than I did before. Thanks again !

This question has already been answered. Start a new discussion instead.