FAQ: Method Output - Return

This community-built FAQ covers the “Return” exercise from the lesson “Method Output”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C#

FAQs on the exercise Return

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

using System;

namespace Return
{
  class Program
  {
    static void Main(string[] args)
    {
      DecoratePlanet("Jupiter");
    }
    static string DecoratePlanet(string planetName) {
      Console.WriteLine($"*.*.* Welcome to {planetName} *.*.*");
      return planetName;
    }
	}
}

1 Like

Took me a while to figure it out for some reason I didn’t use the hints just google

Can anyone explain me the use of “return” statement???

2 Likes

I feel like this answer is somewhat misleading, as you aren’t actually making any real use of the return access modifier. You could just as easily leave out your “return planetName;” line at the end of the method and set the access modifier to void.

using System;

namespace Return
{
  class Program
  {
    static void Main(string[] args)
    {
      DecoratePlanet("Jupiter");
    }
    static void DecoratePlanet(string planetName) {
      Console.WriteLine($"*.*.* Welcome to {planetName} *.*.*");
      //return planetName;
    }
	}
}

It will do the same exact thing…

alhaiam didn’t implement the method with the correct idea.


using System;
namespace Return
{
  class Program
  {
    static string DecoratePlanet(string name)
    {
      return  $"*.*.* Wellcome to {name} *.*.*"; 
    }  // DecoratePlanet

    static void Main(string[] args)
    {      		
	string msg1, msg2;

	msg1 = DecoratePlanet("Venus");
	msg2 = DecoratePlanet("Mars");

	// Now you can use msg1 ans msg2 anywhere (not only in Console.WriteLine()  )
	// Methods keep programs simpler, for example, if is has a lot of code

       Console.WriteLine(msg1);
       Console.WriteLine(msg2);      		      		
       Console.WriteLine(msg1 + msg2);      		

    } // Main
    
 } // class Program
}  // namespace Return

The intent is to teach the concept of using the return statement. Teaching with what appears to the student as the tedious or inefficient approach often pays dividends to the student later. Almost 2 months after your observation, you may see benefit.

That noted, it’s good to see you took some initiative to research. That is one of the approaches Codecademy and others recommend to aid independent learning.

static string DecoratePlanet(string planet) { return $"*..*..* Welcome to {planet} *..*..*"; }

The fact that there’s no accompanying variable or a direct “put this on the screen” instruction (such as Console.WriteLine) with the decorated text in the method confuses me. I don’t understand how the program knows what to do with that text when the only thing accompanying it is the keyword ‘return’. It’s returning it, but returning it to what? There’s no variable that the text is being assigned to, and apparently it isn’t replacing the Console.WriteLine method because we still needed that when we called it in Main. I thought that the only ways a program can use a piece of typed information is if you either assign it to a variable, or assign it to direct output via Console.WriteLine, otherwise it doesn’t know where to put it because you didn’t tell it where.

Perhaps you noticed after this step (2) that the next step (3) is the call to the function with said “Console.WriteLine()”.

1 Like

Hmmm…this beginning of this chapter is quite confusing for me…

Maybe I didnt understand the introduction text, but the output here can be done the same way like the previous chapter tasks. I added the solution and my code below. Therefore, an explanation what the difference is, would be great like:
“With the return statement it is possible to do X and/or Y, that you couldnt do in the previous chapter”

The 2nd thing that was irritating to me was the beginning of the example in the code:

static string Yell(string phrase)

I mean…Yell is blue, which seemd to be like a reserved word (function or so), however, the solution seems to use that I had to use the “function” Yell…however that is not…
You actually start with “static string DecorationPlanet”…

Does anyone else find this suboptimal, with no offence against Codecademy?


using System;

namespace Return

{

class Program

{

static void Main(string[] args)

{

  Console.WriteLine(DecoratePlanet("Jupiter"));

  **DecorationPlanets("Jupiter");**

}



static string DecoratePlanet(string planet)

{

   return $"*..*..* Welcome to {planet} *..*..*";

}

//Code from previous chapter

**static void DecorationPlanets(string planets)**

{

**Console.WriteLine($"*..*..* Welcome to {planets} *..*..*");**

}

}

}

For the blue font discrepancy, you can open a bug report (Get Help button on lower right) and explain that example content would better reflect a color scheme consistent with the user interface. As for the solution, you could do the same if it’s incorrect.

I’ve figured it out now, but at the time the fact that there was a separate function to write the line was what confused me because I didn’t understand what ‘return’ could possibly be doing if it wasn’t storing the string in a variable and also wasn’t writing the line itself. Later I figured out that it effectively is storing the string in a ‘variable,’ that variable being the method itself. I know the method isn’t literally a variable, but thinking of it that way is what helped me understand what was happening.

1 Like

This is what I came up with:

using System;

namespace Return
{
  class Program
  {
    static void Main(string[] args)
    {
      string welcome = DecoratePlanet("Jupiter");
      Console.WriteLine(welcome);
      
    }
    static string DecoratePlanet(string planetName)
    {
      return $"*.*.* Welcome to {planetName} *.*.*";
    }
    
	}
}

Is it the optimal way to tackle this exercise?

edit: improved it a bit by removing the unnecessary welcome string:

using System;

namespace Return
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine(DecoratePlanet("Jupiter"));
      
    }
    static string DecoratePlanet(string planetName)
    {
      return $"*.*.* Welcome to {planetName} *.*.*";
    }
    
	}
}
1 Like

It’s used where you want the method to output something, but not necessarily print it to the console. The returned value can then be used to store in a variable.

Like the user above said, you use return when you want your method to give something back for you, some kind of output value. And you will use it A LOT. I’ll try to give an example with a different data type.
Let’s look at this piece of code:

static void Main(string[] args)
        {
            Console.WriteLine("Please enter first number:");
            int num1 = int.Parse(Console.ReadLine());
            Console.WriteLine("Please enter second number:");
            int num2 = int.Parse(Console.ReadLine());
            Console.WriteLine("Result of multiplication is: {0}", Multiply(num1,num2));

        }
        static int Multiply(int a, int b)
        {
            return a * b;
        }

What keyword “return” does is literally returns some value (which has to be of the type you declared, in this case an int). I could’ve written this like:

static int Multiply(int a, int b)
        {
            int result = a * b;
            return result;
        }
//or inside Main like this:
static void Main(string[] args)
        {
             int result = Multiply(num1, num2);
             //then i can use this variable for something else... let's saaay... I wanna square it and write that in the terminal
             Console.WriteLine("The squared value of your result is: {0}", result * result);
        }

So in short, what comes after return can be stored and manipulated further. The value itself here is not printed until I do it inside the Main method, but the result of multiplication is stored inside the method itself. Later on when you get to build more complex logic, this will be incredibly useful since you will have one method which you can call literally every time you need it instead of writing that block of code repeteadly.

You have already been using methods that return an output, you just haven’t thought about it that way. Math.Round(double, int); returns some value to you, it does something “under the hood” which you can then use further for whatever you need. Or Math.Sqrt() which return a square root of the values you pass to it as arguments. Just like that, you will create methods in your app that you will use in a way they will give something back to you.
I hope this helped a bit… :slight_smile:

Sadly the theorie part and the explanaitions lacked greatly in this exercise. Please take another look at this lesson, I basically had to copy the solution whic won’t teach anyone but the instructions were unclear and the hints not helpful.