Why Add Void to Method Parameter List

Why add void to method parameter list

The C++03 standard says (emphasis mine):

8.3.5.2

The parameter-declaration-clause determines the arguments that can be
specified, and their processing, when the function is called. [Note:
the parameter-declaration-clause is used to convert the arguments
specified on the function call; see 5.2.2. ] If the
parameter-declaration-clause is empty, the function takes no
arguments
.

This means that if you are talking to the compiler it's just a matter of taste.

If you are writing code that will be read by others, then the C++ way of doing things is

void foo();

The other form remains valid only for reasons of compatibility with C, where there was a difference among the two signatures.

Is it better to use C void arguments void foo(void) or not void foo()?

void foo(void);

That is the correct way to say "no parameters" in C, and it also works in C++.

But:

void foo();

Means different things in C and C++! In C it means "could take any number of parameters of unknown types", and in C++ it means the same as foo(void).

Variable argument list functions are inherently un-typesafe and should be avoided where possible.

Multiple calls to a void method using list parameter as return value is better than a method that return a List?

The second option MIGHT be more optimal.

The issue is the first way not only has to make another List object for every brand, which is then just thrown away but if there are a lot of cars for a brand, then Java will resize the list (which is initialized to the default size of 16) many times. The resizing operation requires copying the array. This could get expensive if you resize many times.

The second option only has one list and since resizing usually doubles the capacity, it should have to resize fewer times than the first option.

However, this is getting into micro-optimizations. I wouldn't worry about this kind of thing unless you are noticing a performance issue and have done analysis to determine that this is a bottleneck.

If you are worried about the method name, I think having the word "get" in the name is throwing you off because it usually implies returning something. Naming it something like addBrandOfCarsTo() might make it read more like a sentence:

for(Brand brand : brands) {
addBrandOfCarsTo(result, brand));
}

Use of 'void' in a function parameter

The two canonical forms of main are, according to the standard (see C99 section 5.1.2.2.2 "Program startup"):

int main (void);
int main (int argc, char *argv[]); // or equivalent such as char **argv

Others are specifically allowed but those are the required ones.

As to the preferred form between fn(void) and fn(), I prefer the former since I like to explicitly state that there are no parameters.

There is also a subtle difference. C99 section 6.7.5.3 "Function declarators", paragraph 10, states:

The special case of an unnamed parameter of type void as the only item in the list
specifies that the function has no parameters.

Paragraph 14 of that same section shows the only difference:

An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied.

In other words, it means the same as void in the function definition but does not mean that in a standalone declarator (i.e., the prototype). int fn(); means that no information on the parameters is yet known but int fn(void); means there are no parameters.

That means that:

int fn();
int fn (int x) { return x; }
int main (void) { return fn(0); }

is valid but:

int fn(void);
int fn (int x) { return x; }
int main (void) { return fn(0); }

is not.

When does a 'void' method affect the parameter, and when does it affect the original object?

Changing the value of the parameter itself never affects the argument in Java, because all arguments are passed by value. However, look at this method:

public static void editnumbersArray(int[] a){
a[2] = 9;
}

That assignment doesn't change the value of the parameter. The value of a is still the same reference, to the same array - it just changes the contents of the array.

Imagine if I wrote my home address on a piece of paper for you. It wouldn't matter what you did to that piece of paper - that wouldn't change where I lived. However, if you visited the address and painted the front door green, without ever changing the piece of paper at all, I would see that change.

It's very important to differentiate between different concepts:

  • A variable is a named storage location; it holds a value, which is always either a primitive value (e.g. an int) or a reference. In my example above, the piece of paper was like the variable.
  • A reference is just a value which allows you to navigate to an object. It's not the object itself. It's like the address on the piece of paper.
  • An object contains other variables. There may be several variables which all have values which are references to the same object. It's like the house in my example: I can write my address on several pieces of paper, but there's only one house.

An array is an object which acts as a container for other variables. So the value of a is just a reference to the array.

What is the purpose of ListVoid?

It is possible that this method signature was created as a by-product of some generic class.

For example, SwingWorker has two type parameters, one for final result and one for intermediate results. If you just don't want to use any intermediate results, you pass Void as the type parameter, resulting in some methods returning Void - i.e. nothing.

If there were a method List<V> returnAllIntermediateResults() in SwingWorker with Void as the type parameter V, it would have created a method just like you posted in your question.

The code would be perfectly valid. You can instantiate any implementation of the List interface (e.g. ArrayList) with type parameter Void. But the only value a Void type can have is null. So the list could not hold anything else but nulls, if the implementation allows null elements.

void * typed function parameter

void * is a generic pointer which can point to any object type. The above function can take a pointer to any type and can return a pointer to any type.

A generic pointer can be used if it is not sure about the data type of data inputted by the user.

Example: The following function will print any data type provided the user input about the type of data

void funct(void *a, int z)
{
if(z==1)
printf("%d",*(int*)a); // If user inputs 1, then it means the data is an integer and type casting is done accordingly.
else if(z==2)
printf("%c",*(char*)a); // Typecasting for character pointer.
else if(z==3)
printf("%f",*(float*)a); // Typecasting for float pointer
}


Related Topics



Leave a reply



Submit