Im struggling to understand method outputs

I don’t understand how using the out parameter works or how it’s supposed to give multiple outputs? the lesson gives the example:

static string Yell(string phrase, out bool wasYellCalled)
{
wasYellCalled = true;
return phrase.ToUpper();
}

and says we use out so that it will return multiple parameters but the only thing returned is the string? also what is even the point of bool wasYellCalled? when they solve it for you they redefine a new bool but I can’t understand the purpose of it. sorry if this is a dumb question my brain is just not understanding :frowning:

Hello, @michelaplutoyahoo.co. Welcome to the forum!
Could you post a link to the exercise?

1 Like

hi thanks! here it is: https://www.codecademy.com/courses/learn-c-sharp/lessons/csharp-method-output/exercises/using-out?action=resume_content_item

1 Like

Here’s the how the full example code would appear inside a program with a few additions to produce visible output:

static void Main(string[] args)
{
  string message = "garrrr";
  string yelledMessage = Yell(message, out bool flag); // Here we are sending message as a parameter and declaring a bool variable that is also sent as a parameter. Because of the out modifier the method must assign a value to this parameter. That value will be returned even though it isn't included in the return statement.
  Console.WriteLine(yelledMessage); //prints "GARRRR"
  Console.WriteLine(flag); //prints "True"
}

static string Yell(string phrase, out bool wasYellCalled) //because of the out modifier the bool variable wasYellCalled is also returned
{
  wasYellCalled = true; //the value for our out parameter is assigned here
  return phrase.ToUpper(); //the uppercase string is returned to the caller
}

Output:

GARRRR
True

Hopefully my comments in the code above have added a little clarity. The out parameter will be implicitly returned without having to be included in a return statement.

You can even return additional values:

    static void Main(string[] args)
    {
      string murmer = Whisper("GARRRR", out bool flag, out string hello);
      Console.WriteLine($"murmer is: {murmer}\nflag is: {flag}\nhello is: {hello}");
    }

    static string Whisper(string phrase, out bool wasWhisperCalled, out string greet)
    {
      wasWhisperCalled = true;
      greet = "Hello World!";
      return phrase.ToLower();
    }

Output:

murmer is: garrrr
flag is: True
hello is: Hello World!

Hopefully this helps.

2 Likes

Most sane languages simply box up all the return values into a tuple and return that tuple.
C# came to its senses in 7.0 so you can do that here too. No need for arcane workarounds with out parameters.

using System;

namespace Derp {
class M {
    static void Main(string[] args) {
        var (murmur, flag, hello) = Whisper("GARRRR");
        Console.WriteLine($"murmur is: {murmur}\nflag is: {flag}\nhello is: {hello}");
    }

    static (string, bool, string) Whisper(string phrase) {
        return (phrase.ToLower(), true, "Hello World");
    }
}
}

If it’s the same thing then it should look the same too. Having one of the return values look different from the other two is weird.

3 Likes

Cool, but the exercise in question is teaching how to use out. Maybe you could suggest a curriculum update?

3 Likes

Now there’s a thought. Though, I have no clue how to phrase that to who or whether they’d want to change it, they may very well consider it “finished”

3 Likes

Thank you for explaining! sorry for the late reply…

I understand much better now, so thank you for your help! :slight_smile:

I have another question though if you or anyone else could answer it? in all the examples the out parameters were always just given a value that can never change, which seems less useful than just making a regular variable. are you able to actually modify things using out? like say have an out parameter that would turn a string all uppercase? or is that something that can only be done within the return statement?

2 Likes

well, that’s significantly easier… I’ll definitely use this syntax once I’m working on my own code and out of the lesson program, so thank you for explaining it :slight_smile:

These out parameters are ‘regular’ variables, and can be further referenced, accessed, manipulated, etc. after they are implicitly returned by the method where they are assigned. I’ll try to explain with comments in the code from one of my previous posts:

    static void Main(string[] args)
    {
     // the following line declares 3 variables: murmer, flag & hello
     // murmer will be assigned the value 'explicitly' returned by the Whisper method's return statement
     // flag and hello must be (and are) assigned values within the Whisper method
      string murmer = Whisper("GARRRR", out bool flag, out string hello); 
      // in this line we are printing the values of all 3 variables
      Console.WriteLine($"murmer is: {murmer}\nflag is: {flag}\nhello is: {hello}");
     // we could continue to 'use' all 3 variables in our code for example:
      if (flag)  // test the value of 'flag'
      {
        // change the value of 'hello'
        hello += " It is a beautiful Day!";
        Console.WriteLine(hello); // print the new value of 'hello'
      }
    }
    // since we are passing 'out' arguments to this method, the method declaration must
    // include 'out' parameters to accept the arguments
    // then inside the code body of the method, the 'out' parameters must be assigned values
    static string Whisper(string phrase, out bool wasWhisperCalled, out string greet)
    {
        // this is assigning 'true' to the 'out' parameter 'wasWhisperCalled' which will then be
        // implicitly returned to the caller passing its value back to the original argument: flag
      wasWhisperCalled = true; 
      // same thing here: "Hello World" will be implicitly returned, and be assigned to 'hello'
      greet = "Hello World!";
      // 'phrase' is not an 'out' parameter
      // its value is modified here by the method and explicitly returned as the value 
      // assigned to 'murmer'
      return phrase.ToLower();
    }

Output:

murmer is: garrrr
flag is: True
hello is: Hello World!
Hello World! It is a beautiful Day!

Hopefully this makes things a little clearer, and I didn’t just add more confusion. Anyhow, as @ionatan mentioned:

So, there is little need to spend too much time figuring out how to do something using an outdated technique other than to recognize what is going on if you come across it in a piece of code.

1 Like

ah okay, thank you for all of your help : )

2 Likes

I’m stuck on this lesson as well. From a technical standpoint I get the gist of what the code itself is doing, but I don’t understand the why. Maybe give a real life example of what something like this would be used for? Here’s the code as it appears in the lesson:

static void Main(string args)
{
string statement = “GARRRR”;
string murmur = Whisper(statement, out bool marker);
Console.WriteLine(murmur);
}

static string Whisper(string phrase, out bool wasWhisperCalled)
{
  wasWhisperCalled = true;
  return phrase.ToLower();
}

That seems like a whole lot of work just to print “garrr” to the console. What exactly is the primary purpose of the bools that we’re outing?

In my opinion, as long as you understand what the code is doing, it’s mission accomplished. It’s just something to add to your knowledge base as something that can be done. You may or may not ever use or see it again.

@jbendz you’re right to ask “why” whenever learning a new concept!

You’ll actually find a good example once you get to the “True or False?” project in Learn C#: Arrays and Loops. I’ll include a modified example that doesn’t use loops:

string input;
bool isBool;
bool inputBool;

Console.WriteLine("An eggplant is also known as an aubergine.");
Console.WriteLine("True or false?");
input = Console.ReadLine();
isBool = Boolean.TryParse(input, out inputBool);

if (!isBool)
{
  Console.WriteLine("Make sure to respond with true or false.");
  input = Console.ReadLine();
  isBool = Boolean.TryParse(input, out inputBool);
} 
else 
{
  bool answer = inputBool;
  // Maybe later you would compare this answer to the actual correct value, etc.
}

In this case Boolean.TryParse() returns a boolean that is true if the conversion succeeded and false otherwise. If it was a success, it sets the out argument to the converted value.

Let me know if that needs more clarification/explanation!