• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

ProgEx05

Started by Frederick J. Harris, November 04, 2009, 04:12:58 AM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

Frederick J. Harris


/*
 ProgEx05.Dev

 We've finally covered enough ground in terms of the very fundamentals of C programming
 to start getting into some 'meat'.  Below is an example of a truely simple program that
 has a truely simple function.  The name of the function is IncrOne() and all it does is
 return the number passed to it with 1 added to it, i.e., it increments the number passed
 to it by one.  The purpose of this program is to demonstrate the pass by value parameter
 passing nechanism in the use of C functions.  It is an important topic.

 The program starts in main() and an integer variable iNum is declared and set equal to
 five.  The first and second printf statements in main() output the address of iNum and
 the value stored at that address, which is five.  Lines 2 and 3 in the output show this.
 The third printf statement in main calls IncrOne() by placing it within the argument
 list of the function as a parameter.  This sort of 'nesting' of function calls is common
 in programming, so you might as well get used to it.  I admit it is hard to read.  It
 would be clearer to define another variable and assign the return to it from IncrOne()
 and then put that in the printf function, but I decided not to do it here.

 In any case, when that third printf executes program execution jumps to the IncrOne()
 function.  This can be seen in Line 4 of the output.  Line 5 of the output shows that
 the parameter passed to IncrOne() was 5.  The next line, Line 6, is specially
 interesting.  Note that the 'address of' operator , '&', was used to output the address
 of the parameter 'iNum' that IncrOne() received.  Further note that the value of that
 address is not the same as the value of the address of the iNum variable back in main()!
 What do you make of that?

 The only conclusion one can come to is that even though iNum in main() is named the same
 as the parameter to the function iNum, they are two different variables because there
 are two different addresses involved.  This is indeed the case.  When the C compiler hit
 that printf function in main() that contained the nested call to IncrOne(), it made a
 copy of the variable's value, i.e., '5', and it passed the address of that copy to
 IncrOne().

 If the above is true then one would surmise that if iNum in the function is incremented
 by one to be made equal to six, then that incrementation shouldn't have any effect on the
 iNum back in main() because they are two different varibles.

 Indeed, that is the case.  After leaving IncrOne() Line 9 shows that the function
 IncrOne() returned the desired value to the printf statement - which is iNum + 1 or 6.
 But then note that Line 10 proves that the value of the iNum in main() is unchanged.  It
 is still 5.  IncrOne() could not and did not change it, because IncrOne() was working
 with a copy of the original value at a different address.  This is known as passing a
 pareameter by value.  It is the standard or default parameter passing mechanism in C.
*/

#include <stdio.h>

int IncrOne(int iNum)
{
printf("\nEntering IncrOne()\n");
printf("iNum = %u\n",iNum);
printf("&iNum=%u\n",(unsigned int)&iNum);
iNum++; /* terse C code for iNum = iNum + 1 */
printf("iNum = %u\n",iNum);
printf("Leaving IncrOne()\n\n");

return iNum;
}

int main(void)
{
int iNum=5;  /* Set iNum to 5 */

puts("Starting In main()");
printf("&iNum=%u\n",(unsigned int)&iNum);   /* This statement will output address of iNum */
printf("iNum=%u\n",iNum);                   /* This statement will output value of iNum */
printf("IncrOne(iNum)=%d\n",IncrOne(iNum)); /* fn will return iNum+1 */
printf("iNum=%u\n",iNum);                   /* Important!!! iNum in main() is different from */
puts("End main()");                         /* iNum in IncrOne()!  While the function IncrOne() */
getchar();                                  /* indeed returned one more than iNum, iNum here still equals 5! */

return 0;
}

/*
Output:

1)  Starting In main()
2)  &iNum=2293620
3)  iNum=5

4)  Entering IncrOne()
5)  iNum = 5
6)  &iNum=2293584
7)  iNum = 6
8)  Leaving IncrOne()

9)  IncrOne(iNum)=6
10) iNum=5
11) End main()
*/