passing functions

Feb 20, 2016 at 8:43pm
I'm having problems understanding how to pass a function. It is giving me that a and b is not declared in this scope.

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
  using namespace std;
#include <iostream>

int aa()
{
   int a = 1;
   return a;
}

int bb()
{
   int b = 2;
   return b;
}

void display(int a, int b)
{
   int new = a + b;
}

int main()
{
   cout << display(a, b) << endl;
   return 0;
}
Last edited on Feb 20, 2016 at 8:44pm
Feb 20, 2016 at 8:58pm
Indeed, a and be are not declared in your main() function. Just because you have an a and a b declared elsewhere, does not mean they are visible to main(). What is it exactly that you are trying to achieve with this code?
Feb 20, 2016 at 9:07pm
I was trying to successfully compile this code. How would I declare them on main?

thanks,

Feb 20, 2016 at 9:19pm
How would I declare them on main?

Similar to (but not necessarily the same as) lines 6 and 12.

Insert the new code just after the open brace { at line 22.
Feb 20, 2016 at 9:22pm
What do you want to do?
Do you want to pass the result of aa() and bb() to display or do you want to pass two variables to display?

BTW. new is a reserved word.
1
2
3
4
void display(int a, int b)
{
   int new = a + b;
}
Feb 20, 2016 at 9:29pm
I'm practicing passing functions to main. My idea is to pass the value of a and b to void display and cout the result on main

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
using namespace std;
#include <iostream>

int aa()
{
   int a = 1;
   return a;
}

int bb()
{
   int b = 2;
   return b;
}

void display(int a, int b)
{
   int c = a + b;
}

int main()
{
   int a;
   int b;
   cout << display(a, b) << endl;
   return 0;
}


now I'm getting a really weird compile error.
Last edited on Feb 20, 2016 at 9:29pm
Feb 20, 2016 at 9:37pm
Well for one thing in your main you never initialized a or b so your just passing in garbage. And another thing is that your display function isn't doing anything except assigning c a value. Not only that but display function is of type void thus it won't be able to return back the results. In order to get the results you want you'd have to change it from void to int. This is due to the fact that the results you are returning is of type int.
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
29
30
31
32
using namespace std;
#include <iostream>

int aa()
{
   int a = 1;
   return a;
}

int bb()
{
   int b = 2;
   return b;
}

int display(int a, int b)
{
   int c = a + b;
   return c;
}

int main()
{
   int a;
   int b;
   cout << "Enter in two numbers: ";
   cin >> a >> b;
   cout << "Here are the results: ";
   cout << display(a, b) << endl;
   return 0;
}
Feb 20, 2016 at 9:42pm
thanks for the heads up with my void function, but it is not what I wanted to do.
In your example I have to assign new values to a and b, but in reality, I want to use the values assigned on int aa() and int bb().

Thus the result would be 3.
Feb 20, 2016 at 9:50pm
In that case just assign the variables to the function's return values.
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
29
30
using namespace std;
#include <iostream>

int aa()
{
   int a = 1;
   return a;
}

int bb()
{
   int b = 2;
   return b;
}

int display(int a, int b)
{
   int c = a + b;
   return c;
}

int main()
{
   int a = aa();
   int b = bb();
   cout << "Here are the results: ";
   cout << display(a, b) << endl;
   return 0;
}
Feb 20, 2016 at 9:58pm
thanks

=)
Topic archived. No new replies allowed.