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. return

return

Scheduled Pinned Locked Moved C#
tutorialquestionlearning
11 Posts 7 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.
  • T Trustapple

    hi, i am trying to check how exactly the return statement woorks,for that i wrote a code...... my logic is like this.... result will be having a value which will be assigned to sum via return.... and in function(method) it should add the value of sum with one.... for example x=5 y=5 so sum = 10 in method a it should give me 11..... am a beginner.... the result rite now i am getting is 10 and 0 is my logic correct if not wer am i going wrong????? using System; using System.Collections.Generic; using System.Text; namespace Returnmine { class Program { static int x,y,result,sum; public static int Add() { result = x + y; return result; } public static void read() { Console.WriteLine("the value of x is "); x = Int32.Parse(Console.ReadLine()); Console.WriteLine("the value of y is "); y = Int32.Parse(Console.ReadLine()); } public static void a() { int a; a = sum + 1; Console.WriteLine("the value of {0} ",a); } static void Main(string[] args) { read(); int sum = Add(); Console.WriteLine("the value of result is {0}",result); a(); Console.ReadLine(); } } } thanks in advance j

    C Offline
    C Offline
    Christian Graus
    wrote on last edited by
    #2

    The read method assigns values to x and y The add method returns the sum of the two numbers, and prints it. I think I see your problem

    Trustapple wrote:

    int sum = Add();

    This creates a new variable called sum. The global variable of the same name is not assigned, as it is hidden by the new local one you created.

    Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

    1 Reply Last reply
    0
    • T Trustapple

      hi, i am trying to check how exactly the return statement woorks,for that i wrote a code...... my logic is like this.... result will be having a value which will be assigned to sum via return.... and in function(method) it should add the value of sum with one.... for example x=5 y=5 so sum = 10 in method a it should give me 11..... am a beginner.... the result rite now i am getting is 10 and 0 is my logic correct if not wer am i going wrong????? using System; using System.Collections.Generic; using System.Text; namespace Returnmine { class Program { static int x,y,result,sum; public static int Add() { result = x + y; return result; } public static void read() { Console.WriteLine("the value of x is "); x = Int32.Parse(Console.ReadLine()); Console.WriteLine("the value of y is "); y = Int32.Parse(Console.ReadLine()); } public static void a() { int a; a = sum + 1; Console.WriteLine("the value of {0} ",a); } static void Main(string[] args) { read(); int sum = Add(); Console.WriteLine("the value of result is {0}",result); a(); Console.ReadLine(); } } } thanks in advance j

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

      I really don't know where to begin. There are so many things wrong with this code.

      using System;
      using System.Collections.Generic;
      using System.Text;
      
      namespace Returnmine
      {
        class Program
        { 
          private int x,y;
          public int Add()
          {
            int result = x + y;
            return result;
          }
      
          public void read()
          {
            Console.WriteLine("the value of x is ");
            x = Int32.Parse(Console.ReadLine());
            Console.WriteLine("the value of y is ");
            y = Int32.Parse(Console.ReadLine());
          }
          public void a(int sum)
          {
            int outputValue;
            outputValue = sum + 1;
            Console.WriteLine("the value of {0} ",outputValue);
          }
      
          static void Main(string[] args)
          {
            Program p = new Program();
            p.read();
            int sum = p.Add();
            Console.WriteLine("the value of result is {0}",result);
            p.a(sum);
            Console.ReadLine();
          }
        }
      }
      

      While this code isn't perfect, it is better than the original. Compare the changes to see what I've done differently.

      Deja View - the feeling that you've seen this post before.

      T 1 Reply Last reply
      0
      • T Trustapple

        hi, i am trying to check how exactly the return statement woorks,for that i wrote a code...... my logic is like this.... result will be having a value which will be assigned to sum via return.... and in function(method) it should add the value of sum with one.... for example x=5 y=5 so sum = 10 in method a it should give me 11..... am a beginner.... the result rite now i am getting is 10 and 0 is my logic correct if not wer am i going wrong????? using System; using System.Collections.Generic; using System.Text; namespace Returnmine { class Program { static int x,y,result,sum; public static int Add() { result = x + y; return result; } public static void read() { Console.WriteLine("the value of x is "); x = Int32.Parse(Console.ReadLine()); Console.WriteLine("the value of y is "); y = Int32.Parse(Console.ReadLine()); } public static void a() { int a; a = sum + 1; Console.WriteLine("the value of {0} ",a); } static void Main(string[] args) { read(); int sum = Add(); Console.WriteLine("the value of result is {0}",result); a(); Console.ReadLine(); } } } thanks in advance j

        M Offline
        M Offline
        Matthew Cuba
        wrote on last edited by
        #4

        Looks to me like Main() has a local variable 'sum' that is set to 10 but your call to a() doesn't reference that one - since it is local to Main and wasn't passed in - it references the 'sum' that is part of your class. Good Luck!

        “You can't teach people to be lazy - either they have it, or they don't.” -Dagwood Bumstead

        1 Reply Last reply
        0
        • T Trustapple

          hi, i am trying to check how exactly the return statement woorks,for that i wrote a code...... my logic is like this.... result will be having a value which will be assigned to sum via return.... and in function(method) it should add the value of sum with one.... for example x=5 y=5 so sum = 10 in method a it should give me 11..... am a beginner.... the result rite now i am getting is 10 and 0 is my logic correct if not wer am i going wrong????? using System; using System.Collections.Generic; using System.Text; namespace Returnmine { class Program { static int x,y,result,sum; public static int Add() { result = x + y; return result; } public static void read() { Console.WriteLine("the value of x is "); x = Int32.Parse(Console.ReadLine()); Console.WriteLine("the value of y is "); y = Int32.Parse(Console.ReadLine()); } public static void a() { int a; a = sum + 1; Console.WriteLine("the value of {0} ",a); } static void Main(string[] args) { read(); int sum = Add(); Console.WriteLine("the value of result is {0}",result); a(); Console.ReadLine(); } } } thanks in advance j

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #5

          Another one who really needs to buy a "beginning programming" book. Seriously, it will help you much much more than we can.

          --- How to get answers to your questions[^]

          1 Reply Last reply
          0
          • T Trustapple

            hi, i am trying to check how exactly the return statement woorks,for that i wrote a code...... my logic is like this.... result will be having a value which will be assigned to sum via return.... and in function(method) it should add the value of sum with one.... for example x=5 y=5 so sum = 10 in method a it should give me 11..... am a beginner.... the result rite now i am getting is 10 and 0 is my logic correct if not wer am i going wrong????? using System; using System.Collections.Generic; using System.Text; namespace Returnmine { class Program { static int x,y,result,sum; public static int Add() { result = x + y; return result; } public static void read() { Console.WriteLine("the value of x is "); x = Int32.Parse(Console.ReadLine()); Console.WriteLine("the value of y is "); y = Int32.Parse(Console.ReadLine()); } public static void a() { int a; a = sum + 1; Console.WriteLine("the value of {0} ",a); } static void Main(string[] args) { read(); int sum = Add(); Console.WriteLine("the value of result is {0}",result); a(); Console.ReadLine(); } } } thanks in advance j

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #6

            Hi, You are confusing two variables both called sum. One is a static class member, the other is a variable local to Main(). your code is messy. 1. It is inconsistent: the add() method takes two members as input, modifies another member (result) AND returns a value; to be consistent it would either not return anything (so it operates on class members only), or it would not touch any members and work with input parameters and a return value only, which I certainly prefer: static int add(int v1, int v2) {return v1+v2;} In this way, there are no "side effects", the method simply does what its name indicates. 2. In general you should use class members only for things that have a permanent significance, whatever is local to a method should be handled by local variables. 3. rather than making everything (methods, variables) static, it would be much better to create a class, instantiate it, and forget about the static keyword (except for void Main), until you really need something that gets shared across all class instances. Something like this:

            class Test {
            public static void Main() {
            Test test=new Test();
            test.Run();
            }

            public void Run() {
                int v1;
                int v2;
                int.TryParse(Console.ReadLine(), out v1);
                int.TryParse(Console.ReadLine(), out v2);
                Console.WriteLine("The sum of "+v1+" and "+v2+" is "+add(v1,v2));
                Console.ReadLine();
            }
            
            public static void add(int v1, int v2) {
                return v1+v2;
            }
            

            }

            Notes: - there are no class data members at all; - the static keyword for add() is optional; I use it here to indicatethe method does not need any class members (and not because it would not compile without the static keyword). May I suggest you buy a book an C# and work your way through it, it will offer the fastest and most thorough way to get all the basic knowledge and skills. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


            T 1 Reply Last reply
            0
            • L Luc Pattyn

              Hi, You are confusing two variables both called sum. One is a static class member, the other is a variable local to Main(). your code is messy. 1. It is inconsistent: the add() method takes two members as input, modifies another member (result) AND returns a value; to be consistent it would either not return anything (so it operates on class members only), or it would not touch any members and work with input parameters and a return value only, which I certainly prefer: static int add(int v1, int v2) {return v1+v2;} In this way, there are no "side effects", the method simply does what its name indicates. 2. In general you should use class members only for things that have a permanent significance, whatever is local to a method should be handled by local variables. 3. rather than making everything (methods, variables) static, it would be much better to create a class, instantiate it, and forget about the static keyword (except for void Main), until you really need something that gets shared across all class instances. Something like this:

              class Test {
              public static void Main() {
              Test test=new Test();
              test.Run();
              }

              public void Run() {
                  int v1;
                  int v2;
                  int.TryParse(Console.ReadLine(), out v1);
                  int.TryParse(Console.ReadLine(), out v2);
                  Console.WriteLine("The sum of "+v1+" and "+v2+" is "+add(v1,v2));
                  Console.ReadLine();
              }
              
              public static void add(int v1, int v2) {
                  return v1+v2;
              }
              

              }

              Notes: - there are no class data members at all; - the static keyword for add() is optional; I use it here to indicatethe method does not need any class members (and not because it would not compile without the static keyword). May I suggest you buy a book an C# and work your way through it, it will offer the fastest and most thorough way to get all the basic knowledge and skills. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


              T Offline
              T Offline
              Trustapple
              wrote on last edited by
              #7

              Can u recomend any books that are very simple so taht a person who is new to oops can learn C# himself Luc :)..... j

              L C 2 Replies Last reply
              0
              • P Pete OHanlon

                I really don't know where to begin. There are so many things wrong with this code.

                using System;
                using System.Collections.Generic;
                using System.Text;
                
                namespace Returnmine
                {
                  class Program
                  { 
                    private int x,y;
                    public int Add()
                    {
                      int result = x + y;
                      return result;
                    }
                
                    public void read()
                    {
                      Console.WriteLine("the value of x is ");
                      x = Int32.Parse(Console.ReadLine());
                      Console.WriteLine("the value of y is ");
                      y = Int32.Parse(Console.ReadLine());
                    }
                    public void a(int sum)
                    {
                      int outputValue;
                      outputValue = sum + 1;
                      Console.WriteLine("the value of {0} ",outputValue);
                    }
                
                    static void Main(string[] args)
                    {
                      Program p = new Program();
                      p.read();
                      int sum = p.Add();
                      Console.WriteLine("the value of result is {0}",result);
                      p.a(sum);
                      Console.ReadLine();
                    }
                  }
                }
                

                While this code isn't perfect, it is better than the original. Compare the changes to see what I've done differently.

                Deja View - the feeling that you've seen this post before.

                T Offline
                T Offline
                Trustapple
                wrote on last edited by
                #8

                Thanks a million Pete.... am sorry for asking such stupid qstns but i am trying my best thanks again

                1 Reply Last reply
                0
                • T Trustapple

                  Can u recomend any books that are very simple so taht a person who is new to oops can learn C# himself Luc :)..... j

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #9

                  Hi, this is my standard answer on book questions: Sorry, I can't recommend any books on programming languages. I learned C# many years ago, I bought two books that both are no longer up-to-date (due to C# 2.0). In general I feel book appreciation is very subjective, it depends a lot on your prior knowledge and experience, and your preference for verbose vs. strict description. I tend to go to the book shop, look at say ten books on the subject and then buy the 1 or 2 that I like most (often one tutorial, one reference manual). One more comment, I tend to reread the tutorial a couple of times, say with 1 year intervals. A second/third pass through the book always reveals a few useful things that got lost on the first pass. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


                  1 Reply Last reply
                  0
                  • T Trustapple

                    Can u recomend any books that are very simple so taht a person who is new to oops can learn C# himself Luc :)..... j

                    C Offline
                    C Offline
                    Colin Angus Mackay
                    wrote on last edited by
                    #10

                    Microsoft Press Step-by-step series is excellent.


                    Upcoming FREE developer events: * Glasgow: SQL Server Managed Objects AND Reporting Services ... My website

                    T 1 Reply Last reply
                    0
                    • C Colin Angus Mackay

                      Microsoft Press Step-by-step series is excellent.


                      Upcoming FREE developer events: * Glasgow: SQL Server Managed Objects AND Reporting Services ... My website

                      T Offline
                      T Offline
                      Trustapple
                      wrote on last edited by
                      #11

                      Thank you guys....:) j

                      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