question about library function with variables arguments.

Hi to all,
I’m not sure to post in the right forum (apologize).

I can’t C and C++ very well.
I wish to create a function in a custom library, with the ability to receive a variable number of arguments.

in a library you can create funcion like this:

class::function(int value1,){
//do something
}

could I have a function able to receive value1 or doing something by deault if the value1 is not given?
something like:

if funtion() → do something deault
if untion(value1) → use value1 to do something different.

tank you for any contribute to understand if it’s possible and how…

in a library you can create funcion like this:

Not exactly. The , at the end of the argument list is wrong.

The question that comes to mind is whether you want to write a function that has optional arguments, or truly a function (like sprintf()) that takes any number and type of arguments. If the former, simply define all the arguments in the function declaration, with default values for the optional arguments. If the latter, research on varargs will be essential.

Another option, if all the arguments are the same type, is to take an array and a number of elements in the array.

return_type class::function(parameter_type parameter_name, more parameters if needed){
//do something
}

C++ supports function "overloading"

meaning this

class::function() { //something}
class::function(int value) { //other something}

should work just fine

C++ supports function "overloading"

meaning this

class::function() { //something}
class::function(int value) { //other something}

should work just fine

Tank you Ps991, it works fine! (didn't know the power of overloading) It's not really elegant, because I have to duplicate the whole function, but it works esactly as I needed.

Note: I really don't understand what you meaned with the first code..

for PaulS: (tank you) I didn't understand all you writed, but I would not declare too many arguments with arrays or default values, because it would make everything less comprensive for users (too many arguments, maybe never used, maybe never understood). I prefer they can add an argument where and when they know what they are looking for.

ardukey: It's not really elegant, because I have to duplicate the whole function, but it works esactly as I needed.

Why do you have to duplicate the whole function? Aren't they doing two different things? In your first post you said:

ardukey: if funtion() -> do something deault if untion(value1) -> use value1 to do something different.

Do you just want a default value for your parameter instead? You can do that with a default value in the function declaration. That makes the argument optional.

My first code was what a function should look like, I was fixing your function when you typed

class::function(int value1,){
//do something
}

which is missing a return type and has an extra comma

However, if you want to use a function which is basically the same and just need a default value applied to "value", then you can do this.

class::function(int value = 0) {//do something }

//now when you call it, you have 2 options
function(); //this will act as it you typed function(0);
//you could also call it like this
function(5); //now value = 5

Or the slightly less elegant, but just as powerful...

void class::function() {
    function(someDefaultValue);
}

where the function with no arguments simply calls the other one with a default value supplied.