back of the sofa? Regards Shaine Ismail
On 29 May 2010 02:04, Christian Catchpole <[email protected]> wrote: > > can not find variable > > have you checked your pockets? :) > > On May 29, 10:16 am, Luke Vandervort <[email protected]> wrote: > > Thank you! > > > > > > > > > > > > On Fri, May 28, 2010 at 10:42, PhilDin <[email protected]> wrote: > > > Just to clarify a little more, a variable will only be visible in the > > > block in which you declare it. So when you go > > > > > public static int prduct(int x, int y) > > > { > > > int mult; > > > mult = (x * y); > > > return mult; > > > } > > > > > the mult variable is only visible between the { and } and the caller > > > will not be able to it or any other variable inside your function. > > > This gives you the freedom to change the variables inside your > > > function without introducing changes into the calling code (this is > > > called block scoping, not all languages do it but most do). > > > > > I usually explain this to people using the following example from > > > mathematics: > > > > > Let a=1 and b=f(x) > > > Question, what is the value of a? > > > > > It's not a trick question, the value of course is 1. In a computer > > > program though, the answer is not obvious, f() might change the value > > > of a. > > > > > What is the lesson? The lesson is that when you call a function, it > > > should compute some result and return it, it shouldn't compute some > > > result and store it in a variable. There will be exceptions to this > > > rule; sometimes your function must change a value. When this happens, > > > the name of the function should convey the fact that something will > > > change, often by calling it setSomething(). > > > > > Finally, a note about your naming. You have created a function called > > > prduct(), most people can figure out that this means product() but why > > > not just call it product() anyway. People used to abbreviate function > > > names because there were limits to how long your variable and function > > > names could be (I still work with some old code which limits variables > > > to 10 characters) but today, your variable and function names can be > > > as long as you like. > > > > > -phil > > > > > On May 28, 3:07 am, Luke Vandervort <[email protected]> wrote: > > > > Hi guys, hope you don't mind me asking a question! Can anyone tell me > why > > > > variable mult is not returning to the main method? Everything else > works > > > > great! Excuse my ignorance I am just learning this! Thanks! > > > > > > public class Numbers > > > > { > > > > > > public static void main(String[] args) > > > > { > > > > int a = 20; > > > > int b = 67; > > > > > > mysum(a, b); > > > > mydifference(a, b); > > > > prduct(a, b); > > > > > > System.out.println("The product of a * b = " + mult); > > > > } > > > > public static void mysum(int x, int y) > > > > { > > > > > > int newsum; > > > > newsum = (x + y); > > > > System.out.println("The sum of a and b = " + newsum); > > > > } > > > > public static void mydifference(int x, int y) > > > > { > > > > int newdifference; > > > > newdifference = (x - y); > > > > System.out.println("The difference between a and b = > " + > > > > newdifference); > > > > } > > > > > > public static int prduct(int x, int y) > > > > { > > > > int mult; > > > > mult = (x * y); > > > > return mult; > > > > } > > > > } > > > > > > -- > > > > Luke M. P. Vandervort > > > > > > — Philippians 3:18-19 > > > > > -- > > > You received this message because you are subscribed to the Google > Groups > > > "The Java Posse" group. > > > To post to this group, send email to [email protected]. > > > To unsubscribe from this group, send email to > > > [email protected]<javaposse%[email protected]> > <javaposse%2bunsubscr...@googlegroups .com> > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/javaposse?hl=en. > > > > -- > > Luke M. P. Vandervort > > > > —Philippians 3:18-19 > > For many, as I have often told you and now tell you even in tears, > > conduct themselves as enemies of the cross of Christ. > > Their end is destruction. Their God is their stomach; their glory is > in > > their "shame." Their minds are occupied with earthly things. > > -- > You received this message because you are subscribed to the Google Groups > "The Java Posse" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]<javaposse%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/javaposse?hl=en. > > -- You received this message because you are subscribed to the Google Groups "The Java Posse" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/javaposse?hl=en.
