Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How can I use a method to just change values?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Function means method. It's what I'm used to, sorry.

Say there is a variable n with value 1 in the main function. There is an IF operator in the main function which if satisfied, does- doSomething(n) which is a void function as follows:-



After the IF statement (assuming it is satisfied), if I want to use n in an operation (assuming the IF in the function doSomething is satisfied) it does not take the value of n as 3 but as 1, there is no change. Why is this? What changes do I have to make to rectify this?

For further clarification, here is the whole program :-



Please forgive any syntax errors.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Arguments to methods are passed by value in Java. That means that if you call a method, for example doSomething(n); in line 5 of your second code snippet, then only the value of the variable 'n' is passed to the method 'doSomething' - not the variable 'n' itself. Inside the 'doSomething' method, the value is assigned to a new variable (called 'i' in your code). If you change the value of the variable 'i', then the value of 'n' in the main method is not affected, because 'n' and 'i' are different variables.

You can't directly do what you want to do in Java. Instead of trying to modify the value of a variable that you pass in to the method, make the method return the desired value, and assign it in the 'main' method.
 
Ranch Hand
Posts: 954
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, variables are passed as a value not reference. So by making changes in formal parameter does not make changes in actual parameter. You need to return the value for the same.

Jesper given better explanation then me.
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joson Mathew wrote:Function means method. It's what I'm used to, sorry.


Then I suggest you "unlearn" that idea. In most computing languages (and, I believe, in maths) a "function" is a process that returns something, and void ain't "something".

Winston
 
Joson Mathew
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Joson Mathew wrote:Function means method. It's what I'm used to, sorry.


Then I suggest you "unlearn" that idea. In most computing languages (and, I believe, in maths) a "function" is a process that returns something, and void ain't "something".

Winston



But 'function' is the term given in my school textbook. And in all other textbooks in my board. It must be correct, they wouldn't teach hundreds and thousands of students a wrong thing.
 
Joson Mathew
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jesper de Jong wrote:Welcome to the Ranch.

Arguments to methods are passed by value in Java. That means that if you call a method, for example doSomething(n); in line 5 of your second code snippet, then only the value of the variable 'n' is passed to the method 'doSomething' - not the variable 'n' itself. Inside the 'doSomething' method, the value is assigned to a new variable (called 'i' in your code). If you change the value of the variable 'i', then the value of 'n' in the main method is not affected, because 'n' and 'i' are different variables.

You can't directly do what you want to do in Java. Instead of trying to modify the value of a variable that you pass in to the method, make the method return the desired value, and assign it in the 'main' method.



You sir, are an epic genius. However, I need my 'method' to remain void as I need to use System.out.println inside the method, although that is not shown in the program here. I can't use that in your method can I? What are my options?
 
Bartender
Posts: 2230
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joson Mathew wrote:However, I need my 'method' to remain void as I need to use System.out.println inside the method, although that is not shown in the program here. I can't use that in your method can I? What are my options?


Have you tried calling System.out.println from a method that returns int? What happened?
 
lowercase baba
Posts: 12871
62
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joson Mathew wrote:, I need my 'method' to remain void as I need to use System.out.println inside the method, although that is not shown in the program here. I can't use that in your method can I? What are my options?


Often the best way to find out if you can or can't do something is to try it.

Further, if you are going to make such bold statement, you might want to provide some supporting evidence - i.e. "My textbook says..." or "According to the Java Language Specification..."

When you do try it, and "it doesn't work", post your code showing what you tried, and tell us exactly what happened.
 
Winston Gutkowski
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joson Mathew wrote:But 'function' is the term given in my school textbook. And in all other textbooks in my board. It must be correct, they wouldn't teach hundreds and thousands of students a wrong thing.


Well in this case, I'd say they have. Even back in 1976, when I was learning my stuff, the main difference between COBOL and FORTRAN was that, in the main, FORTRAN had "functions" and COBOL had "procedures".

The problem is that, with the advent of higher-level languages (and especially Object-Orientation), "procedure" became a dirty word, so the word "method" sprang up instead; and I'm pretty sure it's still the official term for a block of code that takes input (either implicit or explicit) and does something with it; whether it returns anything or not. I say, therefore, that functions are a subset of methods; and while I can't find a one-line definition that confirms this, practically every Wiki page that refers to "functions" includes stuff about both input and output.

I'd just add that I dislike void methods in general, and try to avoid them if I can. There are obvious exceptions to this 'rule', such as "setters", but usually I find it much more useful for a method to return something - thus making it, as far as I'm concerned, a "function".

Winston
 
Joson Mathew
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:

Joson Mathew wrote:However, I need my 'method' to remain void as I need to use System.out.println inside the method, although that is not shown in the program here. I can't use that in your method can I? What are my options?


Have you tried calling System.out.println from a method that returns int? What happened?



Often the best way to find out if you can or can't do something is to try it.

Further, if you are going to make such bold statement, you might want to provide some supporting evidence - i.e. "My textbook says..." or "According to the Java Language Specification..."

When you do try it, and "it doesn't work", post your code showing what you tried, and tell us exactly what happened.



I feel like a fool. Thank you all for your help. My problem is solved. I don't where I got this notion from and why it was so firmly fixed in my brain.
 
Marshal
Posts: 69884
278
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joson Mathew wrote: . . .
But 'function' is the term given in my school textbook. . . .

It is still the wrong term. They might use that name in C, but they say all sorts of things wrong in C. They even used the wrong symbol for assignment = not := which is why we end up messing about with == operators.

But I shall go farther than Winston. If you ask a mathematician they will tell you that a function really should always return the same thing from the same input. A log is a function: log1.234 is always the same amount. So the logarithm method behaves as a function, always returning the same result from the same argument.

And welcome again
 
Campbell Ritchie
Marshal
Posts: 69884
278
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have non‑void methods, as Winston suggests, you can do things like what you see in this post.
 
Campbell Ritchie
Marshal
Posts: 69884
278
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote: . . . "method" . . . and I'm pretty sure it's still the official term for a block of code that takes input (either implicit or explicit) and does something with it; . . .

Well the Java Language Specification says method and I don't think I have ever read function or procedure in it.
Method is a nice name in that it does not specify whether you are using functions or procedures.
 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add to this, most of the textbooks used in colleges around the world contain quite a few mistakes, and I'm talking mistakes more important than terminology. Textbooks generally are sufficient for learning purposes, but you shouldn't take what they say as dogma. I've tutored quite a few IT students in java, and was at times flabbergasted at the things they learned from a professor.
 
Joson Mathew
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All right, I'll make the switch from function to method as soon as I'm done with school. Don't want to mess up my board paper. Thanks everyone. Any ADMINs around, feel free to close this thread.
 
Campbell Ritchie
Marshal
Posts: 69884
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quietly discuss the name with your teachers.
Remember you are in a technical field and people use jargon. The reason for jargon is that everybody in the field understands it. If you say, “void method” to a Java® programmer, they will immediately know what you mean, so you need to use the jargon correctly, and not be some sort of Humpty Dumpty.
 
Winston Gutkowski
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joson Mathew wrote:Any ADMINs around, feel free to close this thread.


You should be able to do that yourself. Just look around for a button labeed "Mark Resolved" or one with a tick on it (usually at the bottom and, unfortunately, quite small).

Winston
 
mooooooo ..... tiny ad ....
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic