Using input argument as output - c++

my question is:
I saw(in CUDA examples) that it is possible to use one of the input arguments of a function as output variable. Example: add two integers, c=a+b:
void function AddT(int a,int b,int c){
c=a+b;
}
But this will not work. The function will not alter the value of c in the main program. Who can I fix it and allow the function to change the value of c?

Pass the variable c by reference.
void function AddT(int a, int b, int& c)
{
c = a + b;
}
This will make it so that any changes to c that you make in the function will remain even after the function ends. My wording is pretty poor here; you can look here for more information:
Pass by Reference / Value in C++
What's the difference between passing by reference vs. passing by value?

Related

Cross product function gives really small values, c++ [duplicate]

This question already has answers here:
What's the difference between passing by reference vs. passing by value?
(18 answers)
Closed 6 years ago.
I am new to programming and here is a simple question about how passing by reference works. In this program, I am calculating roots of a quadratic equation.
void getCoefficients(double &a, double &b, double &c);
void solveQuadratic(double a, double b, double c, double &x1, double &x2);
void printRoots(double x1, double x2);
void error(string msg);
int main() {
double a,b,c,x1,x2;
getCoefficients(a,b,c);
solveQuadratic(a,b,c,x1,x2);
printRoots(x1,x2);
return 0;
}
So, my question is I seem to be passing values to getCoefficients and solveQuadratic from main program but in the function definitions of getCoefficients and solveQuadratic, I seem to be accepting references as arguments and am confused as to how this works?
When passing a variable by reference, whatever changes you make to it in the function are reflected back in the calling function.
On the other hand, when you pass a variable by value, the changes made to it are local, and hence not reflected in the calling function.
For example,
#include "iostream"
using namespace std;
void function1(int &x, int y) // x passed by reference
{
x+=y;
}
void function2(int x, int y) // x passed by value
{
x+=y;
}
int main()
{
int x=10;
function1(x, 10); // x is passed by reference (no & while calling)
cout << x << endl; // 20
function2(x, 1000);// x is passed by value
cout << x << endl; // 20
}
Notice that whatever value of y you pass in the call to function2 does not make any difference to the second cout statement.
You do not decide whether to pass values or references in main. The function definition decides that for you. Irrespective of pass by value or pass by reference, the format of calling a function remains the same.
void getCoefficients(double &a, double &b, double &c);
This says, "I take 3 parameters - all of the type double & (reference to a double). Reference is quite a lot confused with pointers, so I recommend you read this up first.
Let's call the a,b,c inside the main as main_method_vars.
When you call getCoefficients, whatever this function does to the passed variables inside it, is reflected on the main_method_vars. Actually, this method works with the main_method_vars.
If instead you have void getCoefficients(double a, double b, double c), this means that whenever you call this method with the main_method_vars, this method will copy the values of a,b,c and work with new copies, instead of working with the original passed copies to it.
void getCoefficients(double &a, double &b, double &c);
void solveQuadratic(double a, double b, double c, double &x1, double &x2);
For example, function getCoefficients, variable a,b,c is passed by reference, so the value for the three variables will be changed in the main function also, if their value changed in the getCoefficients function.
Although not directly an answer, you could look at the topic "variable scope". It would explain why the symbols "a", "b" and "c" may or may not not represent the same thing in the different functions. The concept of local variables is a prerequisite of understanding of "pass by value", "pass by pointer" and "pass by reference".
You can also do this first test: try changing the names of parameters in one of the functions, for example getCoefficients(double &first,double&second,double &third).
You can also do this second test: try calling solveQuadratic(10, 20, 30, x1,x2) and getCoefficients(1,-2,1). The first should work, but not the second.
Finally, you can try this third test: change the value of arguments x1 and x2 in the printRootsfunction. Then check if these changes also occurred in the main function (after the call to printRoot, of course).

C++ function input argument by reference vs by pointer

I am new to C++ and have done only MATLAB earlier.
My Q is about the input argument of the following functions, which call variables by value,reference and pointer.
void SwapbyValue (int a, int b){
// Usual swapping code
}
void SwapbyRef (int &a, int &b){
// Usual swapping code
}
void SwapbyPoint(int *a,int *b){
//Usual swapping code
}
Since my Q isn't about how the above functions work but rather about how I call them, I've left out the code. So, I understand we call the above functions by typing SwapbyRef (i1,i2),SwapbyRef (i1,i2) and SwapbyPoint(&i1,&i2) when i1 and 12 are int.
That confuses me the life out of me. Okay, I get that the first function takes in values and makes sense. But in the second one, calling by just i1 and i2 doesn't make sense as when the function is defined, we set its input as &a and not just a but it still runs. Again in the third, we set the input argument as a pointer i.e. *a but we're passing an address &a (like 0x7956a69314d8) when we call it.
Why does it run when we pass the wrong kind of input to the function?
For example,a Matlab analogy,it looks like passing a char to a int function. Help!
int &a is a reference to an int, meaning, it will accept all int variables that already exist. What you cannot do is for example SwapbyRef(4, 5) using SwapbyRef (int &a, int &b), because 4 and 5 are temporary ints that do not exist somewhere in memory as variables.
Btw, you should probably just look up what a reference in c++ is. That would help you most, I think.

C++ passing by reference or by value? [duplicate]

This question already has answers here:
What's the difference between passing by reference vs. passing by value?
(18 answers)
Closed 6 years ago.
I am new to programming and here is a simple question about how passing by reference works. In this program, I am calculating roots of a quadratic equation.
void getCoefficients(double &a, double &b, double &c);
void solveQuadratic(double a, double b, double c, double &x1, double &x2);
void printRoots(double x1, double x2);
void error(string msg);
int main() {
double a,b,c,x1,x2;
getCoefficients(a,b,c);
solveQuadratic(a,b,c,x1,x2);
printRoots(x1,x2);
return 0;
}
So, my question is I seem to be passing values to getCoefficients and solveQuadratic from main program but in the function definitions of getCoefficients and solveQuadratic, I seem to be accepting references as arguments and am confused as to how this works?
When passing a variable by reference, whatever changes you make to it in the function are reflected back in the calling function.
On the other hand, when you pass a variable by value, the changes made to it are local, and hence not reflected in the calling function.
For example,
#include "iostream"
using namespace std;
void function1(int &x, int y) // x passed by reference
{
x+=y;
}
void function2(int x, int y) // x passed by value
{
x+=y;
}
int main()
{
int x=10;
function1(x, 10); // x is passed by reference (no & while calling)
cout << x << endl; // 20
function2(x, 1000);// x is passed by value
cout << x << endl; // 20
}
Notice that whatever value of y you pass in the call to function2 does not make any difference to the second cout statement.
You do not decide whether to pass values or references in main. The function definition decides that for you. Irrespective of pass by value or pass by reference, the format of calling a function remains the same.
void getCoefficients(double &a, double &b, double &c);
This says, "I take 3 parameters - all of the type double & (reference to a double). Reference is quite a lot confused with pointers, so I recommend you read this up first.
Let's call the a,b,c inside the main as main_method_vars.
When you call getCoefficients, whatever this function does to the passed variables inside it, is reflected on the main_method_vars. Actually, this method works with the main_method_vars.
If instead you have void getCoefficients(double a, double b, double c), this means that whenever you call this method with the main_method_vars, this method will copy the values of a,b,c and work with new copies, instead of working with the original passed copies to it.
void getCoefficients(double &a, double &b, double &c);
void solveQuadratic(double a, double b, double c, double &x1, double &x2);
For example, function getCoefficients, variable a,b,c is passed by reference, so the value for the three variables will be changed in the main function also, if their value changed in the getCoefficients function.
Although not directly an answer, you could look at the topic "variable scope". It would explain why the symbols "a", "b" and "c" may or may not not represent the same thing in the different functions. The concept of local variables is a prerequisite of understanding of "pass by value", "pass by pointer" and "pass by reference".
You can also do this first test: try changing the names of parameters in one of the functions, for example getCoefficients(double &first,double&second,double &third).
You can also do this second test: try calling solveQuadratic(10, 20, 30, x1,x2) and getCoefficients(1,-2,1). The first should work, but not the second.
Finally, you can try this third test: change the value of arguments x1 and x2 in the printRootsfunction. Then check if these changes also occurred in the main function (after the call to printRoot, of course).

Any way to notate the outputs in function arguments?

Lets say I have a function
void doStuff(vector<int> &a, int b, vector<int> &c> {
c = vector<int>(a.size());
for (int i = 0; i < a.size(); i++) {
c[i] = a[i] + b;
}
}
obviously, upon seeing the function, we know that "c" is the output.
For anybody who hasn't seen the function definition though, it remains a mystery unless i name c something like "output_c". Maybe I'm just being vein but I don't like naming things "ouput_xxx", is there any syntax candy for letting the user of the function know that its supposed to be the output?
Syntax, by itself, can be a guide to indicate which one is an input argument and which one is an output argument. However, an output argument can also serve as an input argument too. You cannot tell that by just looking at the signature.
Examples:
int foo(int arg); // The argument is copy by value. It can only be an input argument.
void foo(std::vector<int> const& arg); // The argument is by const&.
// It can only be an input argument.
void foo(std::vector<int>& arg); // The argument is by &. It can be:
// 1) an output argument.
// 2) an input and output argument.
// 3) an input argument (bad practice)
You could add a preprocessor directive:
#define OUT
and put it in the parameter list like so:
void doStuff(vector<int> &a, int b, OUT vector<int> &c) ...
I think I've seen some APIs do something like this. That way it is explicitly stated in the function signature but you don't have to modify the variable names. The code is also unchanged at compile time since OUT is not defined to be anything, it is just a defined symbol.
I think, though, I would rely on your own documentation when writing the function and/or return-by-value instead of doing something like this. You could also make use of the const keyword to flag a parameter that is guaranteed not to change - that's what the syntax is designed for.

Problems with functions parameters in pass by references in C++

I am newbie in c++ and just learning it.
I have written the following code.
#include<iostream>
#include<cstdio>
using namespace std;
void first(int &x,int n)
{
int i;
for(i=0;i<n;i++)
{
cout<<x+i;
}
cout<<endl;
}
void second(int *x,int n)
{
int i;
for(i=0;i<n;i++)
{
cout<<*x+i;
}
cout<<endl;
}
int main()
{
int exm[5]={1,2,3,4,5};
first(exm[0],5);
second(exm,5);
return 0;
}
this program gives output correctly.but the problem is i don't understand the differences between using & and * in function parameters...
both are the techniques of passing arguments by references and when we pass by reference we just send the memory address...
but in function first when i tried to call the function as follows
first(exm,5);
function occurred an error.
why ?
but when i called the function as follows
first(exm[0],5);
it compiled properly and gave right output...but i know that the both calling is equivalent...
then why this error occured?
what is the difference between using & and * in function parameters?
The type of the variable exm is int[5], which doesn't meet the signature of first(int &x,int n).
But int[N] can be converted implicitly to int* that points to the first element of the array, hence second(exm,5) can compile.
what is the difference between using & and * in function parameters?
It's the difference between a reference and a pointer.
There are lots of differences between them.
In this case, I think the biggest difference is whether it accepts NULL or not.
See:
- What are the differences between a pointer variable and a reference variable in C++?
- Are there benefits of passing by pointer over passing by reference in C++?
- difference between a pointer and reference parameter?