Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 430875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:55:06+00:00 2026-05-12T19:55:06+00:00

How I can have variable number of parameters in my function in C++. Analog

  • 0

How I can have variable number of parameters in my function in C++.

Analog in C#:

public void Foo(params int[] a) {
    for (int i = 0; i < a.Length; i++)
        Console.WriteLine(a[i]);
}

public void UseFoo() {
    Foo();
    Foo(1);
    Foo(1, 2);
}

Analog in Java:

public void Foo(int... a) {
    for (int i = 0; i < a.length; i++)
        System.out.println(a[i]);
}

public void UseFoo() {
    Foo();
    Foo(1);
    Foo(2);
}
  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T19:55:07+00:00Added an answer on May 12, 2026 at 7:55 pm

    These are called Variadic functions. Wikipedia lists example code for C++.

    To portably implement variadic
    functions in the C programming
    language, the standard stdarg.h header
    file should be used. The older
    varargs.h header has been deprecated
    in favor of stdarg.h. In C++, the
    header file cstdarg should be used.

    To create a variadic function, an
    ellipsis (...) must be placed at the
    end of a parameter list. Inside the
    body of the function, a variable of
    type va_list must be defined. Then the
    macros va_start(va_list, last fixed
    param)
    , va_arg(va_list, cast type),
    va_end(va_list) can be used. For
    example:

    #include <stdarg.h>
    
    double average(int count, ...)
    {
        va_list ap;
        int j;
        double tot = 0;
        va_start(ap, count); //Requires the last fixed parameter (to get the address)
        for(j=0; j<count; j++)
            tot+=va_arg(ap, double); //Requires the type to cast to. Increments ap to the next argument.
        va_end(ap);
        return tot/count;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a function that accepts variable number of parameters (meaning I can put
I have a Java function which can take variable number of parameters and in
I have an unsigned int variable and it can only have the values of
Suppose I have a variable of type Int = 08, how can I convert
I have lists of variable length where each item can be one of four
I have to write a function that accepts a variable number of arguments, similar
I have a logging function that takes in a variable number of arguments and
Possible Duplicate: How can I code a C# function to accept a variable number
Possible Duplicate: C/C++: Passing variable number of arguments around Imagine I have a function
Is it possible to have a C# lambda/delegate that can take a variable number

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.