Should functions end with a return statement? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 2

Should functions end with a return statement?

I ask this question knowing very well that it might be covered in the C++ tutorial. However, I have not yet gotten there, have become somewhat obsessed with a code I am working on, and a new semester has started. I made a function in the code that I have been working on and it has been working just fine. I was just wondering that since the main function has return 0 at the end, should other functions in the code have a return statement at the end of the function as well? If they don't the code still runs just fine, but I notice my compiler does give me a warning (and I think it is because the function doesn't return anything). Thanks for your answers!

17th Jan 2018, 5:43 AM
Parker king
Parker king - avatar
4 Antworten
+ 3
The return statement in the void function are actually optional but there must be return statement in the non-void function. You can also see here :- http://www.fredosaurus.com/notes-cpp/functions/return.html https://stackoverflow.com/questions/32513793/c-and-c-functions-without-a-return-statement
17th Jan 2018, 5:55 AM
Akash Pal
Akash Pal - avatar
+ 2
What does void mean? I have seen it various places as I have looked up how to do different things, but haven’t learned about it in the tutorial yet. What is void and what does it even mean?
17th Jan 2018, 5:14 PM
Parker king
Parker king - avatar
+ 1
@Parker In C++ , a function have different kinds of return value. Void in the start of the function means that the function which didn't have any particular return type.It can return any kind of value such as string, int , double,etc. .Some particular return types are int,string,etc. But if a function have a return type as Int it can't return a string value. Did you understand @Parker.For other problems related to this question.Comment them here too.
18th Jan 2018, 12:32 PM
Akash Pal
Akash Pal - avatar
+ 1
@Akash, here is an example of a function I created. I called it an integer but it does output string, and it has no return. When I compile it, I just get an warning but it runs just fine. Should this function actually be void? Or is it fine as int and it just needs to have a return? (note, input 2 integers if you compile on SoloLearn) #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; int q = 0, score = 0, totalScore = 0, num = 1, totalNum = 1, ans; int mathQuestion (int q){ int a, b, c; if (q == 1){ a = rand() % 10 + 1; cout << "\n" << num << ". What is " << a << "+" << a << "?\nYour answer: "; cin >> ans; if (ans == a + a){ cout << "Correct!" << endl; score += 1; totalScore += 1; } else{ cout << "Incorrect" << endl; } } if (q == 2){ a = rand() % 10 + 1; b = rand() % 10 + 1; cout << "\n" << num << ". What is " << a << "+" << b << "?\nYour answer: "; cin >> ans; if (ans == a + b){ cout << "Correct!" << endl; score += 1; totalScore += 1; } else{ cout << "Incorrect" << endl; } } num += 1; totalNum += 1; } int main() { srand (time(NULL)); mathQuestion(1); mathQuestion(2); return 0; }
18th Jan 2018, 8:07 PM
Parker king
Parker king - avatar