Hi @giantgough - welcome back to the forums!
I’ll do my best to provide some clarity, though I’m going to answer parts of your post out-of-order because I think it’ll help clear things up.
Since C# 7, we’ve been able to declare the
out variable inside the method call. So, we can either do this:
string murmur = Whisper(statement, out marker);
string murmur = Whisper(statement, out bool marker);
The second version is more compact, but because we’re declaring the variable inside the method call we need to provide the type -
bool in this case. (For C# 6 and older versions, the only acceptable way to do this was the first example where we declare the variable ahead of the method call.)
In the method definition,
wasWhisperCalled is the parameter. In the method call, we’re declaring a new variable called
marker to hold the value which comes back from the method through the
marker is just a variable in the method call, so we can name it whatever we want.
I’ll have to reset the exercise and try your code to be sure, but I think you’ve managed to fudge it because the SCT is simply checking for the presence of some expected lines of code and not actually validating the output… As a result, your code isn’t a valid solution to the final task - it doesn’t do what is being asked, as you mention…
You couldn’t move the line
Console.WriteLine(sentence) to the
main method verbatim without changing the variable being printed, but even then your code would still fail and not write anything to the console. This is because you’re getting the error message
Not all code paths return a value; your method declaration specifies that
Whisper() returns a
string but your method never does so you get an error rather than
hello written to the console. You need to add a
return statement to the method.
Which brings us back around to…
The lesson asks you to create a method,
Whisper(), has two parameters: a
string and an
As I mentioned earlier, when we call the method we can pass in or declare any variable we like for the
out bool parameter - Codecademy’s solution has opted to use a variable
marker, whereas you went for the same name as your method definition and used
However, the two solutions do not generate the same result. The Codecademy solution ends up with the variable
murmur assigned the value
marker assigned the value
true, with both being in-scope and printable to the console from within
main(). Your solution fails to build because of the
not all code paths return a value error.
If we focus on the
The exercise is, I think, simply meant to get you used to using
out as a fudge to work around a limitation of C# when you want to return more than one thing from your methods. As it happens, I don’t think the exercise requires or checks whether you print the
out bool variable so it is sort of pointless here - aside from getting you to practice using it, that is.
(As an aside, this is a limitation which sort of no longer exists because Microsoft have expanded on their implementation of tuples as of C# 7 and you can return multiple values as a tuple from methods now.)
One final thing. Your solution is easily 95% of the way to being correct; the only issues are the missing
Whisper() and the misplaced
Console.WriteLine(), though the latter is really only wrong because the lesson asks you to print the returned value… so please don’t take this as a huge dig at your code because it’s really not far off!
Hopefully I’ve not confused you further, and you understand why CC went for
out bool marker in their example solution. Let us know if you’ve any further questions, though.