return
-
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
-
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
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 )
-
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
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.
-
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
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
-
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
-
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
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
-
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
Can u recomend any books that are very simple so taht a person who is new to oops can learn C# himself Luc :)..... j
-
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.
Thanks a million Pete.... am sorry for asking such stupid qstns but i am trying my best thanks again
-
Can u recomend any books that are very simple so taht a person who is new to oops can learn C# himself Luc :)..... j
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
-
Can u recomend any books that are very simple so taht a person who is new to oops can learn C# himself Luc :)..... j
Microsoft Press Step-by-step series is excellent.
Upcoming FREE developer events: * Glasgow: SQL Server Managed Objects AND Reporting Services ... My website
-
Microsoft Press Step-by-step series is excellent.
Upcoming FREE developer events: * Glasgow: SQL Server Managed Objects AND Reporting Services ... My website
Thank you guys....:) j