11. Using Methods II


#1
class Dog{
  int age;
  public Dog(int dogsAge){
    age = dogsAge;
    
  }
  public void bark(){
    System.out.println("Woof!");
    
  }
  public void run(int feet){
    System.out.println("Your dog ran " + feet + " feet!");
  }




	public static void main(String[] args) {
    Dog spike = new Dog(13);
    spike.bark();
    spike.run(8);

	}
}

This is the provided code for the example. In this exercise, void is defined as “keyword (which means “completely empty”) indicates to the method that no value is returned after calling that method.” I truly don’t understand its purpose despite the explanation. Please explain what function void serves in general and what it does in this example.


#2

The above System.out.println() returns nothing, hence the method returns nothing. Remove the keyword and run to see what, if any error is raised.


#3

It is just execution, does not return anything.
For example,

public void printString(String str) {
System.out.println(str);
}

public String stringMethod(String str) {
return str;
}

The first method does not return any value, but prints str to console

The second method returns str…
See if this helps,


#4

There isn’t any error, and the screen does return the message in the System.out.println. It won’t work if I take out the word void, which i don’t understand because void returns nothing right? Also, what’s the point of even having a keyword that undermines your code and returns nothing? So much confusion here.


#5

void does not return anything. The keyword is a declaration that the method returns nothing. In its abscence, Java must expect and prepare for a return value, which is why it doesn’t work when the keyword is missing.


#6

What exactly does returning mean? Is it different from just displaying a message on screen, which is all that the “bark method” does?


#7

A function call is like a transaction between two parties. One party is referred to as the caller, the other, the callee. When the callee sends data back to the caller, it is called a return value. When there is no data sent back, then there is no return value, and hence no return, so the function must be declared void of return.


#8

Okay. I really appreciate your help, but there’s just one more thing. Is the “Woof” that is printed on the screen when I run the code not a return? I thought it was since it is the message being given to me. Does return mean that something is stored?


#9

No, it is not. It is sent to the I/O buffer, not returned to the caller.


#10

What’s the difference between what the void functions are doing and what the return functions are doing? They are both made to display a message to the user, so what is the difference?


#11

A function declared void has no expected return value. It runs and terminates. A function not declared void will be expected to have a return, no matter what it is. If there happens to be some printing done in the function, it will have no bearing. There must be an explicit return.


#12

So what’s the difference between printing and returning? I see a message either way, correct?


#13

But what can you do with the message once you see it, if the value is not returned?


#14

So if it’s returned, does that mean that I can access that message in a different function by referring to that function later.


#15

Not the function, the return value that would have been stored. Refer to it as often as you need.


#16

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.