Why wont this function return a value?

i am learning functions, i can get the void function to output a value, but the function that returns a value is not. What am i doing wrong?

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
//mutifunction
#include <iostream>
using namespace std;
void add(int, int);
int addition(int,int);

int main()
{
  
  int a = 3, b = 5;
  add(3,5);
  return 0;
  
  int meow = addition(a,b);
  
  cout << meow << endl;
}

void add(int c, int d)
{
int even = c + d;

cout << even << endl;
}
  
int addition(int e, int f)
{
int cheese = e + f;
return cheese;
}
Last edited on
Everything after the return is ignored, you exit the main function. try putting it after the cout in main. The void function will never return anything thats why it is void, meaning nothing or no return.
i tried that all i am getting back is the void not the return function
ok i rewrote it without function prototypes
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
//mutifunction
#include <iostream>
using namespace std;

void cheese(int a, int b)
{
  int meow;
  meow = a + b;
  
  cout << meow << endl;
}

int chee(int a, int b) 
{
  int m;
  m = a + b;
  return m;
}

int main()
{
  int r = 3, t = 5;
  cheese(r,t);
  
  int meoww = chee(r,t);
  
  cout << meoww << endl;
  
}
1
2
3
4
  add(3,5);
  return 0;
  
  int meow = addition(a,b);


Your problem is here. What do you think happens when you hit that return 0?
It works for me, could you please describe your problem in more detail.
killertcell you said in your last post you rewrote without the function you can keep the function. What was said is return 0; in the main() function needs to be on the last line. Do that and it all works.

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
//mutifunction
#include <iostream>
using namespace std;
void add(int, int);
int addition(int,int);

int main()
{

  int a = 3, b = 5;
  add(3,5);


  int meow = addition(a,b);

  cout << meow << endl;
  return 0;
}

void add(int c, int d)
{
int even = c + d;

cout << even << endl;
}

int addition(int e, int f)
{
int cheese = e + f;
return cheese;
}


when main hits return0; its saying "Ok im done. programs fully done".

it returns that 0 to what ever called it to prove it was done basically.
Last edited on
Topic archived. No new replies allowed.