Jump to content

C++ Help

Spev

I am trying to pass two values by reference from my main to be used throughout other functions I have created. Basically I'm trying to find a way to pass two values by reference entered in the main function and be able to plug those values into other functions to use them as part as calculations....I don't know the best way to do this, or really what to put in the void function at all. I mean, after all I'm just trying to basically use it as a place holder function(I think). Sorry if I'm not explaining it well, but can anyone help?

Current PC build: [CPU: Intel i7 8700k] [GPU: GTX 1070 Asus ROG Strix] [Ram: Corsair LPX 32GB 3000MHz] [Mobo: Asus Prime Z370-A] [SSD: Samsung 970 EVO 500GB primary + Samsung 860 Evo 1TB secondary] [PSU: EVGA SuperNova G2 750w 80plus] [Monitors: Dual Dell Ultrasharp U2718Qs, 4k IPS] [Case: Fractal Design R5]

Link to comment
Share on other sites

Link to post
Share on other sites

errr... something like this?

subfunction1(int Value1, int Value2)
{
	int subresult;
	subresult = Value1 + Value2;
	return result;
}

int main()
{
	int Value1, Value2, result;
	result = subfunction1(Value1, Value2);
}

forgive me if i made a mistake, its been ages since i last touched C++ `-`

-sigh- feeling like I'm being too negative lately

Link to comment
Share on other sites

Link to post
Share on other sites

If you want to pass by reference just put & in front of it.

int main()
{
	int x, y;
	SomeFunction(&x, &y);
	return 0;
}

int SomeFunction(int& x, int& y)
{
	return 0;
}

super basic example, but that should do it.

Link to comment
Share on other sites

Link to post
Share on other sites

On 28/4/2016 at 7:46 AM, r3bify said:

If you want to pass by reference just put & in front of it.


int main()
{
	int x, y;
	SomeFunction(&x, &y);
	return 0;
}

int SomeFunction(int& x, int& y)
{
	return 0;
}

super basic example, but that should do it.

Pass the arguments without &, this code will not compile, you are passing pointers instead of references.

Link to comment
Share on other sites

Link to post
Share on other sites

I made up an example for you, I hope it's sufficient:

 

http://ideone.com/57LnhW

 

#include <iostream>
  
void square_ref(int& a);
int  square_val(int a);
  
int main() {
    int a = 2;
    int s = 2;
    
    std::cout << "a: " << a << ", s: " << s << std::endl;
    std::cout << "------" << std::endl;
    
    square_ref(a);
    square_val(s);
    std::cout << "a: " << a << ", s: " << s << std::endl;
    std::cout << "------" << std::endl;
    
    square_ref(a);
    s = square_val(s);
    std::cout << "a: " << a << ", s: " << s << std::endl;
    return ;
}
  
void square_ref(int& a)
{
    a = a*a;
}
  
int square_val(int a)
{
    return a*a;
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×