Return Confusion C#

Hi! So im working through C# currently and have a good understanding of everything so far, but i will admit ive made it to this same point once before and im always stumped by this.

My issue is “returns”. What does it mean? like i get what its suppose to do i just dont get how its doing, so like

static string DecoratePlanet(string planetName)
{
return $"Welcome to {planetName}!";
}
Console.WriteLine(DecoratePlanet("Jupiter"));

what i dont get is how i pass jupiter as an argument but decorateplanet actually outputs the welcome message and just includes the argument jupiter.

Sorry if this is a silly or not so difficult topic but it just confuses me for some reason. like what if i changed

return $"Welcome to {planetName}";

to

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

or

return Console.WriteLine($"Welcome to {planetName}");

the one above is one of the major ones that mess with my brain.

is it just that the way the course is showing an example is confusing me? i just dont understand the purpose or how to actually use return or how return even works.

I’m not exactly sure where your confusion lies. If you are thinking that return and Console.WriteLine are somehow interchangeable that is inaccurate. return sends a value back to the caller. The caller is the specific line of code that called the function or method that is returning a value. In your code, you have a method named DecoratePlanet that returns a string.

Console.WriteLine() prints the arguments passed to it to the console. It does not return. Printing to the console makes information visible to humans, but does nothing to preserve values or make them usable in other parts of the program.

Both of the snippets you suggested would throw errors that prevent your code from compiling. Your method specifies a return type of string. Removing the return keyword means your function returns nothing, so it would have to be declared with void rather than string.

Your second snippet:

is a little more difficult to explain. The value to be returned is the result of a call to the method, Console.WriteLine(). That method as previously mentioned only prints its arguments to the console. It doesn’t return anything, so your code ends up essentially saying:

return void

Since void isn’t a string, an error is once again thrown. If you replace the return type of string with void in the method signature, you would still throw an error for having the return keyword in the method when it isn’t supposed to return anything.

3 Likes

ahhhhh okay well that clears up the majority of all that so i really appreciate it. but i guess to specify my major confusion is…

DecoratePlanet("Jupiter");

does

static string DecoratePlanet(string planetName)
{
return $"Welcome to {planetName}";
}

mean that return takes the welcome message inside the method and stores it in DecoratePlanet(); so essentially DecoratePlanet has the value of $“Welcome to {planetname}” and so when i call DecoratePlanet with the argument “Jupiter” it just inserts that argument into it? so when i call DecoratePlanet(“Jupiter”), the return in the method basically just means DecoratePlanet = “Welcome to Jupiter”? then ofc to see it id call Console.WriteLine(DecoratePlanet(“Jupiter”));` in order for it to print the value to the console.

and to clarify i know the term “value” wouldnt be the term to explain the method situation with return but if you understand what i mean and im correct its just my wway of understanding it :slight_smile:

1 Like

It sounds like you have a basic understanding of what is going on.

perfect thank you so much :smiley:

1 Like