+ Reply to Thread
Results 1 to 8 of 8

Thread: Variable Arguments

  1. #1
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Variable Arguments

    Java Variable Arguments

    Java allows you to pass a variable number of arguments to a function. We specify several arguments with a variable type followed by an ellipise and a name. The variable number of arguments must come as the last parameter of the function.

    Example:

    Code:
    public static int sum(int... nums) {
            int nSum = 0;
    
            for (int x=0;x<nums.length;x++) {
                nSum += nums[x];
            }
            return nSum;
        }
    This function takes a variable number of arguments and returns the sum of all the arguments. The key here is that you treat the variable arguments as an array.

    Use this code to test out the function:

    Code:
    System.out.println(sum(3,4,2,5));
    System.out.println(sum(-1,2));
    System.out.println(sum(9,3,5,3,2,3,4,3,2,3,2,1,9,-5));
    There is also a foreach loop that you can use in the function.

    Code:
    public static int sum(int... nums) {
            int nSum = 0;
    
            for (int i: nums) {
                nSum += i;
            }
            return nSum;
        }
    This code basically does the same thing as above. It assigns each item in the array of parameters to i and then increases the sum by i. They both do the same thing. However, I like the first loop better because it allows me to control where in the array I start and where in the array I stop.


    Combining varargs with static arguments


    You can use a variable list and static number of arguments. The key here is that the variable arguments list must be declared at the end of the parameter list.

    E.g.

    Code:
     public static int product(int n, int... nums) {
            int product = 1;
    
            for (int i=0;i<nums.length;i++) {
                product *= (nums[i]);
            }
            product *= n;
            return product;
        }
    In this code the user must pass one integer and then as many integers as they want.

    Example:

    Code:
    System.out.println(product(3, 4,2,5));
    In this case, 3 is the integer that must be passed. The rest of the integers are optional.

    E.g.

    Code:
     System.out.println(product(3));
    Hope this helps! Also it is nice to note that the same functionality exists in C and C++.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Variable Arguments

    I didn't know Java had this as well. +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Sinipull's Avatar
    Sinipull is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Tallinn, Estonia, Estonia
    Posts
    382
    Rep Power
    13

    Re: Variable Arguments

    Me neither. That's great! +rep.

  5. #4
    Jordan Guest

    Re: Variable Arguments

    Glad to see you wrote it! Nice work. +rep

  6. #5
    ThemePark is offline Programmer
    Join Date
    Oct 2008
    Location
    Århus, Denmark
    Posts
    105
    Rep Power
    0

    Re: Variable Arguments

    +1 of not knowing this also existed in Java. So +rep for teaching us that.

  7. #6
    Join Date
    Aug 2009
    Location
    ~/
    Posts
    918
    Rep Power
    19

    Re: Variable Arguments

    Just now noticed this one
    +rep
    Last edited by debtboy; 10-10-2009 at 02:26 PM.

  8. #7
    Phineas is offline Newbie
    Join Date
    Jan 2010
    Posts
    11
    Rep Power
    0

    Re: Variable Arguments

    Nice work, would rep++ if I knew how.

  9. #8
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Variable Arguments

    Click the justice-scale icon next to #1 (permalink) in his post header.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. MCQ: variable declared in arguments of this function
    By jackson6612 in forum C and C++
    Replies: 8
    Last Post: 06-05-2011, 07:14 AM
  2. Variable number of arguments
    By Flying Dutchman in forum C and C++
    Replies: 6
    Last Post: 03-10-2011, 11:16 AM
  3. how to write function for variable arguments
    By sai in forum Managed C++
    Replies: 1
    Last Post: 01-03-2011, 10:10 AM
  4. Replies: 3
    Last Post: 11-26-2010, 08:53 AM
  5. Functions With a Variable Number of Arguments
    By dargueta in forum C Tutorials
    Replies: 6
    Last Post: 10-05-2009, 04:22 PM

Tags for this Thread

Bookmarks

Posting Permissions

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