Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Not understanding use of return statement in c#.

Not understanding use of return statement in c#.

Scheduled Pinned Locked Moved C#
questioncsharplearning
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hassan Y Ansari
    wrote on last edited by
    #1

    I am new to programming and learning on youtube. I have question in following code: using System; class test { public static void Main() { int i = 0; abc(ref i); Console.WriteLine(i); Console.ReadLine(); } public static int abc(ref int j) { j = 100; return j; } } After running this, I am getting answer 100. But when I change return statement as "return 0", again answer is 100. May I know why? If I am returning 0, method should return 0 value to variable i no? I am new to programming so please explain me in very details. Thank you so much.

    R P D 3 Replies Last reply
    0
    • H Hassan Y Ansari

      I am new to programming and learning on youtube. I have question in following code: using System; class test { public static void Main() { int i = 0; abc(ref i); Console.WriteLine(i); Console.ReadLine(); } public static int abc(ref int j) { j = 100; return j; } } After running this, I am getting answer 100. But when I change return statement as "return 0", again answer is 100. May I know why? If I am returning 0, method should return 0 value to variable i no? I am new to programming so please explain me in very details. Thank you so much.

      R Offline
      R Offline
      Ron Nicholson
      wrote on last edited by
      #2

      It has to do with variable scope. You are passing by Reference which is different than passing by value. You are basically allowing the function ABC to modify the actual variable i. For this to act as you expect you need to assign the return value from ABC to i like this: i = abc(i) and remove the Ref keyword. There is a little more to it but see below. Look here at a StackOverflow answer[^] and here at the Microsoft Ref keyword.[^]

      Jack of all trades, master of none, though often times better than master of one.

      H 1 Reply Last reply
      0
      • H Hassan Y Ansari

        I am new to programming and learning on youtube. I have question in following code: using System; class test { public static void Main() { int i = 0; abc(ref i); Console.WriteLine(i); Console.ReadLine(); } public static int abc(ref int j) { j = 100; return j; } } After running this, I am getting answer 100. But when I change return statement as "return 0", again answer is 100. May I know why? If I am returning 0, method should return 0 value to variable i no? I am new to programming so please explain me in very details. Thank you so much.

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        You aren't actually using the return statement in your code. What is happening here is that the ref statement is the thing that is actually setting the value of the variable. If you want some fun, try to predict what the value of this statement is (changing your code slightly):

        int i = 0;
        i = abc(ref i);

        Edited with the correct statement on line 2.

        This space for rent

        OriginalGriffO 1 Reply Last reply
        0
        • P Pete OHanlon

          You aren't actually using the return statement in your code. What is happening here is that the ref statement is the thing that is actually setting the value of the variable. If you want some fun, try to predict what the value of this statement is (changing your code slightly):

          int i = 0;
          i = abc(ref i);

          Edited with the correct statement on line 2.

          This space for rent

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          Fortunately, you'll get a compiler error:

          Error CS1510 A ref or out value must be an assignable variable

          But ... back in the seventies I worked with a FORTRAN compiler that didn't issue an error, and would happily change the value of constants. Imagine if this C# code worked:

          void xx(ref int i)
          {
          i += 10;
          }
          static int main()
          {
          int i = 666;
          xx(666);
          int j = 666;
          Console.WriteLine($"{i}, {j}");
          }

          And printed 666, 676 Now debug that in a 1,000,000 line project without an IDE! :laugh:

          Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          P 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            Fortunately, you'll get a compiler error:

            Error CS1510 A ref or out value must be an assignable variable

            But ... back in the seventies I worked with a FORTRAN compiler that didn't issue an error, and would happily change the value of constants. Imagine if this C# code worked:

            void xx(ref int i)
            {
            i += 10;
            }
            static int main()
            {
            int i = 666;
            xx(666);
            int j = 666;
            Console.WriteLine($"{i}, {j}");
            }

            And printed 666, 676 Now debug that in a 1,000,000 line project without an IDE! :laugh:

            Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            Doh, it was meant to be

            i = abc(ref i);

            That's what I get when I type directly into the code editor.

            This space for rent

            1 Reply Last reply
            0
            • R Ron Nicholson

              It has to do with variable scope. You are passing by Reference which is different than passing by value. You are basically allowing the function ABC to modify the actual variable i. For this to act as you expect you need to assign the return value from ABC to i like this: i = abc(i) and remove the Ref keyword. There is a little more to it but see below. Look here at a StackOverflow answer[^] and here at the Microsoft Ref keyword.[^]

              Jack of all trades, master of none, though often times better than master of one.

              H Offline
              H Offline
              Hassan Y Ansari
              wrote on last edited by
              #6

              Thank you so much for your reply. But my question is that when i write statement "return j", it is returning value of j. This part I got but when I write "return 0", why it is not giving answer as 0? As I am new, I am not getting this. Please explain me in a little bit detail. Thank you

              P 1 Reply Last reply
              0
              • H Hassan Y Ansari

                Thank you so much for your reply. But my question is that when i write statement "return j", it is returning value of j. This part I got but when I write "return 0", why it is not giving answer as 0? As I am new, I am not getting this. Please explain me in a little bit detail. Thank you

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                The problem you have is that you are mixing two things up here. Let's break this method down:

                public static int abc(ref int j)
                {
                j = 100;
                return 0;
                }

                The method signature indicates that you are accepting a variable that you are going to populate in this method. It also indicates that you are returning a type of int as the return type from the method (the bit that goes int abc). This means that you have told the compiler that you have two methods of setting values here. Now, suppose you changed your call to this:

                int output = abc(ref i);

                You now have two values coming back from the method. The value in output would be 0, as that is the value coming back from the return statement. The value in i would be 100 because that is the value you set j to in abc.

                This space for rent

                H 1 Reply Last reply
                0
                • P Pete OHanlon

                  The problem you have is that you are mixing two things up here. Let's break this method down:

                  public static int abc(ref int j)
                  {
                  j = 100;
                  return 0;
                  }

                  The method signature indicates that you are accepting a variable that you are going to populate in this method. It also indicates that you are returning a type of int as the return type from the method (the bit that goes int abc). This means that you have told the compiler that you have two methods of setting values here. Now, suppose you changed your call to this:

                  int output = abc(ref i);

                  You now have two values coming back from the method. The value in output would be 0, as that is the value coming back from the return statement. The value in i would be 100 because that is the value you set j to in abc.

                  This space for rent

                  H Offline
                  H Offline
                  Hassan Y Ansari
                  wrote on last edited by
                  #8

                  Thank you so much for your helpful explaination. Now I got the actual concept of return statement. Thank you so much.

                  P 1 Reply Last reply
                  0
                  • H Hassan Y Ansari

                    Thank you so much for your helpful explaination. Now I got the actual concept of return statement. Thank you so much.

                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #9

                    You are most welcome.

                    This space for rent

                    1 Reply Last reply
                    0
                    • H Hassan Y Ansari

                      I am new to programming and learning on youtube. I have question in following code: using System; class test { public static void Main() { int i = 0; abc(ref i); Console.WriteLine(i); Console.ReadLine(); } public static int abc(ref int j) { j = 100; return j; } } After running this, I am getting answer 100. But when I change return statement as "return 0", again answer is 100. May I know why? If I am returning 0, method should return 0 value to variable i no? I am new to programming so please explain me in very details. Thank you so much.

                      D Offline
                      D Offline
                      David McGraw
                      wrote on last edited by
                      #10

                      Hi Hassan, You are getting result 100 because your function abc(ref int i) is accepting ref type parameter. Generally, the ref keyword is used to pass parameter as a reference this means when the value of parameter is changed in called method, then that will get reflected in calling method also. Whenever the following function executed, the result of value of j will be assigned to your parameter i.

                      public static int abc(ref int j)
                      {
                      j = 100;
                      return j;
                      }

                      That's the reason you are getting i values as 100 even after you change return statement as "return 0". If you want to know more about return statement and ref keyword check following topics it will help you understand how & when to use these keywords clearly in c#. Ref Keyword in C#[^] Return Statement in C#[^]

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups