1

I don't understand what the parenthesis after bool vote function is for.

bool vote(int voter, int rank, string name)

It could have been (void), what purpose does it serve here?

Please make my confusion clear, I will begin working on this pset3.

Thanks

2

Did you watch the "functions" short in week 2? Doug mentions void at around 17:00 (the rest is also worth watching).

void is a data type, just without any data. It's often used to indicate a lack of data, or in case of pointers not defining the data's type (you might find some pointers of type void* when some code does not care about the data format, but only for the number of bytes in the structure, e.g. code copying a number of bytes between two locations).

If a function is like void beep(int pitch), the function has no return value. It has one integer-type argument in this case, though. It can be used like beep(42);, but not blablubb = beep(42);, since you can't assign nothing to anything.

If a function is like int get_temperature(void), the function takes no arguments, you can call it like temp = get_temperature();, but can't say int get_temperature(fridge), because in this case you can't assign anything to nothing. This example function returns an integer, and is likely to return degrees Fahrenheit or Celsius, whatever you expect less ;)

And of course you could also have some void do_a_barrel_roll(void), taking no arguments, not returning anything.

This function signature only defines how calling the function would work. It does not define what the function does from being called to returning to the calling code. It might still have side-effects, like printing to stdout (standard output, usually to terminal), or feeding your cat (if it's a function in a food dispenser system).

And if you declare the function (semicolon after the signature), and later define it (no semicolon, but actual code in curly braces), the two signatures would have to match. The declaration does not need parameter names, but those might be convenient when trying to remember the parameters.

Finally, since our vote function per specification requires three arguments, it must be declared/defined as taking them. If it were bool vote(void), we couldn't pass any data to the function on this way.

1
  • Thank you, you explained very well. I always skip shorts, but now I know how important they are!
    – yoyo Ahmed
    Jan 31 '20 at 1:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .