Function referncing

Hello everyone. I have a problem with referencing int functions. My function has three arguments so my question is, can functions with arguments be referenced at all, I cant seem to find this on the internet and referencing seems to work fine if it is with a function with no parameters.


#include <iostream>
#include <cmath>
#include <string>

using namespace std;

int first(1), second(2), third(3);

int ThreeIntegerAddition(int first, int second, int third){return first + second + third;}
int& ThreeIntegerAddition;

int main(){

cout << "Please enter three numbers to add them up" << endl;
cin >> first;
cin >> second;
cin >> third;
cout << ThreeIntegerAddition() << endl;
}

This program isn't really meant for any practical application and is a bit stupid, but it was just something I chose to try to apply referencing with functions.
why do you have (1) (2) and (3) at the end of the variable name if your not using them?
The code your showed shall not be compiled. So it is not clear what you want.
Last edited on
Im sorry the reason i put (1) (2) and (3) was because I made a new program out of the code which didnt use referencing, however I changed it back so that I could use it to display the problem I was having, the code is meant to be without the value, so:

#include <iostream>
#include <cmath>
#include <string>

using namespace std;

int first, second, third;

int ThreeIntegerAddition(int first, int second, int third){return first + second + third;}
int& ThreeIntegerAddition;

int main(){

cout << "Please enter three numbers to add them up" << endl;
cin >> first;
cin >> second;
cin >> third;
cout << ThreeIntegerAddition() << endl;
}
I am repeating the second time. The code you showed shall not be compiled.
What do you mean "Shall not be compilled"...Why are you using future tense...I dont understand
Try something like void function( int &a , int &b, int &c )........;
Look at the documents->tutorials->functions
@LethalJ

What do you mean "Shall not be compilled"...Why are you using future tense...I dont understand


It is required a little to understand what I am saying about. Just try to compile your code. Why are you showing a code that you even did not compile yourself?!
Last edited on
Why am I showing a code which I did not compile myself?.....because it wont compile! Thats the issue which I'm having, hence why I am asking for help on this forum.
@LethalJ

You only had a few minor problems with your code
Here it is, corrected, with explanations..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Adding.cpp : main project file.

#include <iostream>
//#include <cmath> // Not needed. You're not using high math
// #include <string> // Not needed. You're not using strings

using namespace std;

// int first, second, third; // Try not using global variables 

int ThreeIntegerAddition(int one, int two, int three)
{
	// These, int one, int two, int three, take the values of the variables being passed
      return one + two + three;
}
//int& ThreeIntegerAddition;  // Not needed

int main()
{
int first, second, third;
cout << "Please enter three numbers to add them up" << endl;
cin >> first;
cin >> second;
cin >> third;
cout << ThreeIntegerAddition(first, second, third) << endl;
// You needed to send the variables to the function
return 0;
}
Except you are passing a copy instead of reference change 11 to
void ThreeIntegerAddition( int &one , int &two , int &three )
Then remove the return statement
Thanks alot for all the help and feedback, its greatly appreciated!
Topic archived. No new replies allowed.