-
Statements and Functions
Hello,
Im new to the site so allow me to say hi- hi <hehe>
That said, Im also new to C++ programming.
My question is:
What is the difference between a function and a statement.
I was told by the friend that introduced me to C++ that statements make functions, functions make modules, and modules make programs.
Does that mean that statements are individual commands and functions are made of statements- "named blocks of code"?
I understand that this is a basic question and I have tried to find a good C++ dictionary online for beginners but havent had much luck. It seems that a lot of sites assume you already know some things.
Nice to meet you all, and have great day!
GF
-
I think a statement is anything that ends with a semicolon or is immediately followed by a {.
char ch; //statement declaring a variable;
void display(); //statement declaring a function
if(ch == 'a' || ch == 'A')//an if statement
{
display(); //a function call statement
}
etc.
-
Thanks elad :^)
Elad,
Thanks for taking the time to reply.
Based on what you posted, is it fair for me to assume then, that a group of these statements that end in a semicolon are what make up a function?
GF
-
GF,
In C++, functions are "blocks" of code that perform a specific task. That block may consist of a single line of code, or may be a more complex sequence of related statements. The ideal function performs one, and only one, task that is specific to the execution of the program.
(You may also see functions referred to as "methods" or "procedures". Same thing, although, in this community, stick with the term "function".)
Now, with relatively few exceptions, all lines of code in C/C++ end with a semi-colon. Don't let semi-colons dictate what constitutes a function for you.
Research function prototypes (declarations), function calls and function definitions. This will give you a much stronger base to work from than "quick" answers would give you. ;)
Don't be concerned with asking basic questions on this board. Truth be told, most of us love to help with these types of questions. The important thing to remember is that we like (frequently, demand :)) questions that are asked from an informed point of view. That is, you've researched the topic before coming here to ask questions about it.
If you haven't researched the tutorials here yet, please do so. Lots of good stuff! Next - and I'm sure that there are royalties in my future for suggesting this! - find any post by Prelude and check out both of the links in her signature. (Should be required reading, in my opinion. :))
-Skipper
-
It seems to me that a statement is a single line of code, be it a declaration, function call whatever. A block of code is a section of many statements, usually seen included in curly braces. A function would seem to be a block of code that can be accessed through a single statement, ie a function call.
-
GeakFreak: I strongly recommend you not get too hung up on details that don't mean much. The definition of what is a statement is one such minutiae. Others include: what is the difference between a parameter and an argument? what is the difference between a pointer and the name of an array?
I agree with Endo's functional description of a function and I encourage you to follow Skipper's advice. The practical aspects of when and how to use functions is much more important than the details of defining what a function is.
IMO whether:
if(char a == 'a')
represents a statement or just a phrase is immaterial.
Likewise whether the following represents 1 statement (only one semicolon), 6 statements (six lines of code), or isn't a statement at all is immaterial:
while((strcmp(color, "red") != 0) &&
(strcmp(color, "white") != 0) &&
(strcmp(color, "blue") != 0))
{
cout << "choose another color" << endl;
}
More to the point is what does it do. Some people call this a while statement, some call it a while loop. The stuff inside the () is sometimes called a conditional statement. Whatever. Doesn't make much difference one way or the other in terms of the functionality.
Furthermore, whether the following is one or two statements is irrelevenant from a practical standpoint (BTW it is bad practice to use this style, IMO, so don't emulate it; even though it is legal to do it from a the standpoint of the C++ language);
cout << "enter a single keystroke" << endl; cin >> key;