C
C#2y ago
Soinagie

void and return

For starters I still don't undersatnd basically everything so sorry for being stupid Do you always have to use return in non void methods since its the only way to get a value out of it? Why would I make non void method without a return?
10 Replies
Thinker
Thinker2y ago
Yes, you always have to use the return statement in a non-void method. (there's one minor exception to this, although that's not worth mentioning here)
MODiX
MODiX2y ago
thinker227#5176
REPL Result: Failure
static int GetSomeNumber()
{
int x = 1 + 2;
// oops, nothing is returned
}
static int GetSomeNumber()
{
int x = 1 + 2;
// oops, nothing is returned
}
Exception: CompilationErrorException
- 'GetSomeNumber()': not all code paths return a value
- 'GetSomeNumber()': not all code paths return a value
Compile: 395.174ms | Execution: 0.000ms | React with ❌ to remove this embed.
sibber
sibber2y ago
if the return type is not void, youre guaranteeing to the caller that the method will return something so you have to return
Soinagie
Soinagie2y ago
Sorry I dont know what a caller is
sibber
sibber2y ago
whatever calls your method aka executes it
Soinagie
Soinagie2y ago
So method
sibber
sibber2y ago
what
Soinagie
Soinagie2y ago
You execute methods in other methods
sibber
sibber2y ago
oh yeah
Soinagie
Soinagie2y ago
Ok thanks this was helpful