Author Topic: ProgEx07  (Read 2026 times)

0 Members and 1 Guest are viewing this topic.

Offline Frederick J. Harris

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 938
    • Frederick J. Harris
« on: November 04, 2009, 04:42:27 AM »
11/6/2009    -- added a couple unsigned int casts to clear up some compiler warnings with newer
                      GNU compilers.

Code: [Select]
/* ProgEx07 -- This Is A C++ Program!!!  Save File As Main.cpp.  It Won't Compile As Main.c

   This is the 1st C++ program we've encountered.  How do we know its a C++ program?  Well,
   because we saved it as Main.cpp instead of Main.C!  Ha! Ha!  Well, that isn't exactly true
   but its pretty close!  Actually, what makes it a C++ program is the alteration we've done
   to the parameter passing mechanism of the IncrOne() function we've been using for the past
   couple programs.  Note below that instead of int* ptrNumber like we had in ProgEx06 where
   we had to pass a pointer to a parameter to be able to give the function a number it could
   modify, in this version of IncrOne we've specified the parameter as int& iNumber.  This is
   a reference parameter - exactly like BASIC!  So, while C++ programmers kind of 'look down'
   on BASIC programmers, they've been stealing BASIC syntax & constructions (because they're
   better).  The really neat part of this is the way the function is called.  Remember in
   ProgEx06 we had to set up the call like this to IncrOne()...


   Well, when the function is defined as taking a reference parameter, you don't have to pre-
   pend the '&' symbol in front of iNumber like in ProgEx6 to pass its address to the function.
   Again, this is exactly like BASIC.  In the program below the call looks like this...


   ...and within the IncrOne function that function will have access to the address of iNumber
   and of course the value stored at &iNumber - which in the program below is 5.

   Another important detail...

   If this is confusing you...

   return ++iNumber

   ...all it means is the following...

   return iNumber

   The '++' or '--' operators can appear either before or after a variable (attached to it
   of course).  The meaning and effect though are different depending on whether they are
   located before or after.  If we had this...

   return iNumber++;

   ...what would happen is that the function would return the number 5 to main - not 6!
   Then (and even more confusing) before the function terminated it would increment iNumber
   to 6.  So the function would be passed the number 5; it would return 5; then, because it
   has the actual address of iNumber back in main(), it will increment it to 6!  If you don't
   believe me try it out.  C can be a devil!


#include <stdio.h>

int IncrOne(int& iNumber)
 printf("(In Function IncrOne()) -- &iNumber = %u\n",(unsigned int)&iNumber);  // prints address of iNumber
 printf("(In Function IncrOne()) -- iNumber  = %u\n",iNumber);   // just prints iNumber
 return ++iNumber;

int main(void)
 int iNumber=5;

 printf("iNumber=%d\n",iNumber);                  //prints value stored in variable iNumber
 printf("&iNumber=%u\n",(unsigned int)&iNumber);  //prints address where iNumber is stored
 printf("IncrOne(5)=%d\n",IncrOne(iNumber));      //passes address of iNumber to function
 printf("iNumber=%d\n",iNumber);                  //prints value stored in variable iNumber
 getchar();                                       //pauses execution by waiting for character key press

 return 0;

(In Function IncrOne()) -- &iNumber = 2293620
(In Function IncrOne()) -- iNumber  = 5
« Last Edit: November 07, 2009, 02:53:36 AM by Frederick J. Harris »