i thought i didn't need a return type?

ok so i was running this program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int function1()
{
int x = 12;
 cout << x << endl;
}
int main()
{
 int y = 3;
 cout << y << endl;
 return 0;
}


when i ran it, it would compile but i would get a warning saying "Line 8: control reaches end of non-void function "
but if i did this it would compile without warnings:
1
2
3
4
5
6
int function1()
{
 int x = 12;
 cout << x << endl;
 return x;
}


i know that void functions do not have a return type. but i also thought that even though i declared my function an int, i thought that returning something was OPTIONAL. so why am i getting warnings. is there any way to get rid of the warnings with having to "return x;" ?
You are telling the compiler you are returning something, so why wouldn't you want to return anything? What would happen if your function that is supposed to return an int but doesn't is used as an int somewhere such as on the right side of an assignment operator?
closed account (j3bk4iN6)
try using a void in front of function. Another idea is just type return; at end of function.
Last edited on
closed account (j3bk4iN6)
one thing you have to keep in mind when using functions is the variable scope. You can't reference x in main because the free store gets rid of it when the function returns control back to main.
Last edited on
a function is this:

1
2
3
(return type) (name) ((parameters))
{
}


so whatever the return type is, is what you need to return. If you don't need to return anything then make it void.
in you above example, it was pointed out that x is deleted once that function reaches the end. What if we wanted to use that variable back in int main() ?we could do this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int function1()
{
   int x = 12;
   cout << "x = " << x << endl;  // x = 12
   return x;
}
int main()
{
   int y = 3;
   cout << "y = " << y << endl;  // y = 3
   y = function1();  // here I assign the integer y to the function, which means its value becomes the returned value not the function.
   cout << "Now y = " << y << endl;  // y = 12

   return 0;
}
Last edited on
closed account (j3bk4iN6)
you can pass by value with return x. but can you pass a by reference by an assignment operator.
*(ptr) = x
Last edited on
im saying thought, can't i write a function that is not a void function and not have it return anything?
Some compilers may allow it, but my recommendation is you don't do it. There's no need not to do it when you can just use void.
closed account (j3bk4iN6)
thanks i just wondered if I could do that in a function that returned a value but also changed a different variable.
@jinjin12:

Functions not declared to return void must return a value in all paths of execution that reach the end of the function.
The ONE EXCEPTION to this rule is main -- main needn't return anything even though it should be declared to return
int. This is ONLY because lazy C programmers often didn't return anything from main, and enforcing this rule in C++
would essentially break backwards compatibility with C. But the good programmer will return something from main,
even if always zero, because it takes about 2 seconds to type "return 0;" at the end of main.
Topic archived. No new replies allowed.