-1

So far I have been writing my own functions where my arguments are separated by comma's, but is there a way to write a function with semicolons instead of comma's? If so, what is the reason for wanting to do that? I guess when I look at the for-loop statement, its arguments are separated by semicolons and I don't understand that. I am just trying to understand the small things in C++.

Thanks--

user2863626
  • 187
  • 3
  • 12
  • 7
    Why would you want to write C++ code that isn't C++ code? `for` isn't a function, it's a control structure. – tadman Jul 25 '14 at 19:15
  • 3
    You need to pick up a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) on C++. – Captain Obvlious Jul 25 '14 at 19:17
  • 2
    Why not read a book on C++ instead of seconding guessing the language – Ed Heal Jul 25 '14 at 19:18
  • 1
    Programming Principles and Practice using c++ by Bjarne Stroustrup and C programming language by K&R –  Jul 25 '14 at 19:34

1 Answers1

3

In fact in the for loop statement each part of the sstatement is a separate expression statement. So it is naturally that they are separated by semicolons. Its parts are defined as

for-init-statement  
condition;  
expression

Even the condition is considered as a statement because there apart from all there can be a declaration. Moreover each part can contain either a list of expressions (separated by comma) or the comma operator. So the only way to distinguish each part is to use the semicolon.

Consider the following for loop

for ( size_t i = 0, j = std::strlen( s ); i < j; i++, j-- ) std::swap( s[i], s[j] );

if do not use the semicolon it would be difficult to write the similar statement that to understant where each part ends or starts.

As for function parameters then you deal with a list. In the C++ grammar items of a list are separated by commas that to distinguish them from statements.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335