Thread: function, function call, for loop

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    function, function call, for loop

    Hi

    Personally I would prefer to give function's definition before making calls to it because it provides more understanding how C++ works. As some member here told me that time-travel (moving back and forth) is not possible all the time in C++. The statements are executed in a sequence so we should also try to write the code in a sequence to make it more understandable.

    for(int j=0; j<45; j++) - The for loop is executed, then it's value is incremented, then the incremented value is checked against the test expression. So, don't you think having the syntax this way, for(int j=0; j++; j<45), makes more sense?

    Please help me. Thanks a lot.

    Code:
    // table.cpp
    // demonstrates simple function
    #include <iostream>
    using namespace std;
    
    void starline();                          //function declaration
                                              //   (prototype)
    int main()
       {
       starline();                            //call to function
       cout << "Data type   Range" << endl;
       starline();                            //call to function
       cout << "char        -128 to 127" << endl
            << "short       -32,768 to 32,767" << endl
            << "int         System dependent" << endl
            << "long        -2,147,483,648 to 2,147,483,647" << endl;
       starline();                            //call to function
       return 0;
       }
    //--------------------------------------------------------------
    // starline()
    // function definition
    void starline()                           //function declarator
       {
       for(int j=0; j<45; j++)                //function body
          cout << '*';
       cout << endl;
       }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Well, you got the order wrong.

    1) The initialing is executed.
    2) The condition is checked.
    3) [Loop Happens]
    4) The incrementing expression is executed.

    Repeat 2,4.

    Soma

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I guess this becomes a non-issue if you are going to have more than one source file (declarations in headers, definitions in implementation files).

    But anyway, you don't normally care exactly how a function works, just as long as you know what it does. For example, have you read all the code for the overloads of cout<< before using it?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to call a function from a function pointer
    By Richardcavell in forum C Programming
    Replies: 9
    Last Post: 02-16-2011, 04:22 AM
  2. call base class function or derived class function
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-18-2008, 05:23 AM
  3. Replies: 5
    Last Post: 10-17-2006, 08:54 AM
  4. Replies: 2
    Last Post: 06-21-2005, 02:41 PM
  5. function call for a average function
    By ricekingg in forum C++ Programming
    Replies: 1
    Last Post: 02-04-2002, 02:16 PM