Results 1 to 17 of 17
Like Tree2Likes
  • 1 Post By Diargg
  • 1 Post By jlczuk

Thread: Calling a Function

  1. #1
    skmightymouse is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Calling a Function

    I'm super new to Java and I was wondering how would you call a function 3 times from main?

    Any feedback would be great.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Calling a Function

    To repeat a behavior in Java you would use one of the loops Java has available. In general, if you know how many times you want to repeat something *before* the loop (as in this situation), you would use a for loop. So that's my recommendation: use a for loop.

  3. #3
    skmightymouse is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: Calling a Function

    The problem I'm having is that I want my halve () function to take a lone parameter and return the value. I need it to do that for my values of a, b, and c but I don't know how I would tell it to call the function and then return the values of a, b, and c.

    This is what I have so far. If you could just point me in the right direction I think I should be able to figure it out.

    Java Code:
    public class Test
    {
    
        public static void main(String[] args)
    
        {
        	System.out.println("Test \n");
    
        	double a = 5, b = 10, c = 22;
    
        	System.out.println ((int)halve(a));
        }
    
        public static double halve (double a)
        {
        	return a / 2;
        }
    }
    Last edited by skmightymouse; 04-24-2012 at 09:21 PM.

  4. #4
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Calling a Function

    Why not do
    Java Code:
    System.out.println ((int)halve(a));
    System.out.println ((int)halve(b));
    System.out.println ((int)halve(c));
    Of course, that's pretty useless if you want to add a value d. What about a list or array of values? Perhaps you could iterate over them in a for loop?
    Fubarable likes this.

  5. #5
    skmightymouse is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: Calling a Function

    If i do what you suggested than it will just print the values of a, b, and c. I need it to run through the function and then return the value. Its not necessary for me to do an array or anything just because I'm only doing these 3 values.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Calling a Function

    Quote Originally Posted by skmightymouse View Post
    If i do what you suggested than it will just print the values of a, b, and c.
    You haven't tested his code then because it does no such thing. It calls the necessary method three times and prints the result.


    I need it to run through the function and then return the value. Its not necessary for me to do an array or anything just because I'm only doing these 3 values.
    Then do you still have a problem? If so, please elaborate. Myself, I'd create an array for simplicity and ease in debugging.

  7. #7
    skmightymouse is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: Calling a Function

    Here's what I have so far but I'm getting errors when I compile.

    Java Code:
    public class Test
    {
    
        public static void main(String[] args)
    
        {
        	System.out.println("Test \n");
    
        	double a = 5, b = 10, c = 22;
    
        	System.out.println ((int)halve(a));
        	System.out.println ((int)halve(b));
        	System.out.println ((int)halve(c));
        }
    
        public static double halve (double a, double b, double c)
        {
    		return a / 2, b / 2, c / 2;
     	}
    }
    Why would this not work?

  8. #8
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Calling a Function

    Did you try the code I gave you first? I literally gave you the answer accidentally.

    Your code will fail because the calls "halve(double a)" to the method halve(double a, double b, double c) don't match.

    But I'm just guessing. You should really show us the error code you get when you try to compile, and we can walk you through what it means.

  9. #9
    skmightymouse is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: Calling a Function

    System.out.println ((int)halve(a));
    System.out.println ((int)halve(b));
    System.out.println ((int)halve(c));
    I thought I did do that.

    The error I'm getting says the following :

    error: ';' expected
    error: not a statement

    It says that for line 18

  10. #10
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Calling a Function

    return a / 2, b / 2, c / 2;
    return statements don't work like this in Java. You can only return a single item, or nothing. Why not do the work three times? Send one in, halve it, get that value and print it, then move on to the next one.

  11. #11
    skmightymouse is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: Calling a Function

    Thats exactly what I'm trying to do but how do I tell the program to take half of a and return it and then do half of b then return and then do half of c and return?

  12. #12
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Calling a Function

    Methods Tutorial
    Read this. It's a lot to take in, though, so here's the boiled down version.

    Methods take in whatever you pass them(when you call halve(a)), and work on them in whatever way is described in the the function.
    So
    halve(a) -> halve(double numPassedIn) -> numPassedIn/2 -> a/2
    halve(b) -> halve(double numPassedIn) -> numPassedIn/2 -> b/2

  13. #13
    skmightymouse is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: Calling a Function

    I understand what you are saying but I am having trouble relating it to my program. I'm really trying to figure out the answer so if you could give me an example or something that would be helpful.

  14. #14
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Calling a Function

    No. You need to read the documentation and understand how to properly call and use a method.

    An example:

    Java Code:
    in main...
    System.out.println("This and 3 make: " + append3("this"));
    then..
    Method declaration...
    public String append3(String input){
        return input+"3";
    }
    I can send append3 the string "cows" and it'll return "cows3".

  15. #15
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Calling a Function

    Quote Originally Posted by skmightymouse View Post
    If i do what you suggested than it will just print the values of a, b, and c. I need it to run through the function and then return the value. Its not necessary for me to do an array or anything just because I'm only doing these 3 values.
    If you do
    Java Code:
    (int)halve(a);
    You have called the METHOD (not function) halve, passing it the variable a, with which halve calculates and returns a value that is hopefully half of the value of the variable a.

    If you do
    Java Code:
    (int)halve(b);
    You have called the METHOD (not function) halve, passing it the variable b, with which halve calculates and returns a value that is hopefully half of the value of the variable b.

    As you can see, the method halve performs the calculation you need. When you do
    Java Code:
    System.out.println((int)halve(b));
    You are having System.out.println print the value returned by halve(b) as an int.

    You are most certainly NOT printing the value the variable b in this prior example. You are printing what is returned by the call to the method halve.

    Does that help your understanding a little better? You seem to be confused about methods (what you called functions), their input parameters (a or b or c in this case) and what happens with what the method returns.
    Fubarable likes this.

  16. #16
    skmightymouse is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: Calling a Function

    Oh ok I think I got it now. Thank you jlczuk for that last post, it makes sense now. Thanks Diargg for helping me work through the majority of this.

    I appreciate all the help

  17. #17
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

Similar Threads

  1. Calling native function
    By Bharathgl in forum Advanced Java
    Replies: 2
    Last Post: 01-25-2012, 11:43 PM
  2. Calling function in Javascript- from other function
    By jdigger in forum New To Java
    Replies: 1
    Last Post: 02-27-2011, 10:00 PM
  3. Calling the Function again
    By Swetz55 in forum New To Java
    Replies: 2
    Last Post: 12-19-2009, 12:10 PM
  4. Calling a generated GUI function
    By dejos456 in forum New To Java
    Replies: 5
    Last Post: 12-09-2009, 08:02 PM
  5. Calling Java Function through JSP
    By Vikrant Pawar in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 06-06-2008, 07:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •