Thread: Statements and Functions

  1. #1
    Registered User
    Join Date
    Aug 2002

    Question Statements and Functions

    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!

  2. #2
    Registered User
    Join Date
    Mar 2002
    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


  3. #3

    Thanks 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?


  4. #4
    Registered User
    Join Date
    Apr 2002

    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. )

    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  5. #5
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    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.
    Couldn't think of anything interesting, cool or funny - sorry.

  6. #6
    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;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Reasons for keeping functions small
    By maththeorylvr in forum C Programming
    Replies: 7
    Last Post: 03-26-2006, 12:38 AM
  3. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. inheritance & virtual functions
    By xagiber in forum C++ Programming
    Replies: 3
    Last Post: 02-11-2002, 12:10 PM