Home > Enterprise >  C how to make a function that has optional parameters [duplicate]
C how to make a function that has optional parameters [duplicate]

Time:10-02

I want to make a function that has parameters like here void example(int x, int y, int z); but I dont have to put z in there like this example(34, 21); is that possible in C ?

CodePudding user response:

You basically have two options

  1. Create a overloaded function that takes 2 parameters. So you would have
void example(int x, int y, int z)

and

void example(int x, int y)
  1. Assign default value to z
void example(int x, int y, int z = 0)

CodePudding user response:

The way to use undefined amount of parameters is to use ellipsis. For further read i recomend to look through https://en.cppreference.com/w/cpp/language/variadic_arguments To implement it just google how to use c ellipsis and thats it!

  • Related