1. c 2 2 Check Schedule Page Daily! /2320/schedule.html We will often, but not always, discuss...

Post on 11-Jan-2016

213 views 0 download

Transcript of 1. c 2 2 Check Schedule Page Daily! /2320/schedule.html We will often, but not always, discuss...

1

c

22

Check Schedule Page Daily!

http://carme.cs.trinity.edu/thicks/2320/schedule.html

We will often, but not always, discuss upcoming assignments check the page!

Hints-Reading Assignments-Written Assignments/Labs!

3

3

http://Carme.cs.trinity.edu4

4

5

5

Printing!

Use Computer Science Printers Only For Your "Computer Science Homework!!!!"

Print Only What Is Necessary!

6

6

c

7

c

88

Microsoft Office Example

9

Structured Language Developed Windows Version FirstMac Version Cost 88% of Windows Version

Yuk!

OOP Language Developed Windows Version FirstWrote Parallel Versions Of A Few Short Utilities To Open/Close File, Move Cursor To Row/Col, Minimize Window, etc. using the basic operating system calls.

Mac Version Ran In One Day – had to tweek screen because of different pixel resolution – alter images on user manual.

Yea!9

Why OOP? Generic Software

Structured Language One quick sort each for char, int, float, Students, Employees, Parts, Athletes, etc.

C++ Templated OOP Language One quick sort – Assumes that object classes, such as Students, Employees, Parts, Athletes have operator overloads [normal process]

Programming Costs Go Down when we create one stack, one queue, one linked list, one binary tree, one sort, one search, one AVL tree, etc. that can be used with every datatype in the world.

10

c

11

If You Don’t Know These, Come Back & Review Them!

Know How To Declare & The Capacity Limitations!

C++ Has Same Basic Data Types As C

short int a = 1;int b = 2;long int c = 3;float d = 4.4;double e = 5.5;long double f = 6.6;char g = 'A';bool h = true;

12

The Declarations, Initializations, &Assignments Of C++Variables Are The Same As In C!

Same Basic Data Types - C/C++short int a = 1;int b = 2;long int c = 3;float d = 4.4;double e = 5.5;long double f = 6.6;char g = 'A';bool h = true;

Data Type

Minimal Standard

Range / Precision Visual C++ Compiler

Range / Precision

short int 1 bytes -128 to 127 2 bytes -32,768 to 32, 767

int 2 bytes -32,768 to 32, 767

4 bytes -2,147,483,648 to2,147,483,647

long int 4 bytes -2,147,483,648 to2,147,483,647

4 bytes -2,147,483,648 to2,147,483,647

float 4 bytes 7 digits accuracy 4 bytes 7 digits accuracy

double 8 bytes 15 digits accuracy 8 bytes 15 digits accuracy

long double

12 bytes 19 digits accuracy 8 bytes Not SupportedCompiles

char 1 char See ASCII Table0-255

1 char See ASCII Table0-255

bool 1 byte true/false 1 byte true/false13

13

Other Basic Data Types ?unsigned short int a = 1;unsigned int b = 2;unsigned long int c = 3;

Data Type Minimal Standard

Range / Precision Visual C++ Compiler

Range / Precision

unsigned short int 1 bytes 0 to 255 2 bytes 0 to 65,535

unsigned int 2 bytes 0 to 65,535 4 bytes 0 to 4,294,967,295

unsigned long int 4 bytes 0 to 4,294,967,296 4 bytes 0 to 4,294,967,295

14We are not going to use the 64 bit Integers

c

15

How can I find out the limits on a given compiler?

# include <stdio.h>

printf ("sizeof(short int) = %ld\n", sizeof(short int));printf ("sizeof(int) = %ld\n", sizeof(int));printf ("sizeof(long int) = %ld\n", sizeof(long int));printf ("sizeof(float) = %ld\n", sizeof(float));printf ("sizeof(double) = %ld\n", sizeof(double));printf ("sizeof(long double) = %ld\n", sizeof(long double));printf ("sizeof(char) = %ld\n", sizeof(char));printf ("sizeof(bool) = %ld\n", sizeof(bool));

16

Some Way To Verify The Range?

# include <limits.h>

#define SHRT_MIN (-32768) /* minimum (signed) short value */#define SHRT_MAX 32767 /* maximum (signed) short value */#define USHRT_MAX 0xffff /* maximum unsigned short value */#define INT_MIN (-2147483647 - 1) /* minimum (signed) int value */#define INT_MAX 2147483647 /* maximum (signed) int value */#define UINT_MAX 0xffffffff /* maximum unsigned int value */#define LONG_MIN (-2147483647L - 1) /* minimum (signed) long value */#define LONG_MAX 2147483647L /* maximum (signed) long value */#define ULONG_MAX 0xffffffffUL /* maximum unsigned long value */etc.

17

Can We Display These If Defined?

# include <limits.h>

printf ("SHRT_MIN = %d\n", SHRT_MIN);printf ("SHRT_MAX = %d\n", SHRT_MAX);printf ("INT_MIN = %d\n", INT_MIN);printf ("INT_MAX = %d\n", INT_MAX);printf ("LONG_MIN = %ld\n", LONG_MIN);printf ("LONG_MAX = %ld\n", LONG_MAX);

printf ("USHRT_MAX = %u\n", USHRT_MAX);printf ("UINT_MAX = %u\n", UINT_MAX);printf ("ULONG_MAX = %u\n",ULONG_MAX);

18

c

19

Be In Your Seat & Logged On In Windows

ByThe Time The Class Is Scheduled To Begin!

c

20

20

I Have Taught This Class Using A "Lecture

Oriented Approach"

Only I Use The Computer

I Can Get Through A LotOf Examples & Code, But

Sometimes Students In An 8:00 Class Struggle!

c

21

I Have Taught Portions Of This Class Using A "Participation

Oriented Approach"

Where I Wait Until Most Folks Get Everything Typed in Right

Many Get Really Bored – Not All Work At A Same Rate

I Am Unable To Cover The Expected Content

c

22

There Will Be Times That I Encourage You Type Along

With Me Today!

Some Of You Will Get Lost Along The Way

If The Person Beside You Can Help Get You On Track Without Getting Lost Themselves - Great

WHEN YOU GET LOST

1] Take Good Notes2] Watch On As Your Neighbor Works

All Should Copy Anything Written On

Board! Bring Pencil & Paper

c

23

Start Visual Studio Create A New Project

24

Start Visual Studio Create A New Project

25

Start Visual Studio Create A New Project

You Can Double-Click To Start Visual StudioWith This Application

26

c

27

c

# include <stdio.h>printf("Enter X : ");scanf("%ld", &X);printf("\nX = %4ld\n", X);

printf("Enter First Name : ");scanf("%s", &FirstName);printf("\nFirst Name = %s\n", FirstName);

printf("Enter Full Name : ");gets(FullName);printf("\nFull Name = %s\n", FullName);

printf("Enter Pay Rate : ");scanf("%7.2f", &PayRate);printf("\nFirst Name = %7.2f \n", PayRate);

printf("Enter [T/F] : ");scanf("%c", &Option);printf("\nOption = %c\n", Option);

puts("This will do a line feed after printing the string!");28

You Should Be Competent Using

The printf & scanf Statements!

c

29

c

# include <stream.h>cout << "Enter X : ";cin >> X;cout << endl << "X = " << X << endl;

cout << "Enter First Name : ";cin >> FirstName;cout << endl << "First Name = " << FirstName << endl;

cout << "Enter Pay Rate : ";cin >> PayRate;cout << "\nPay Rate = " << PayRate << endl;

cout << "Enter Full Name : ";cin.getline(FullName);cout << "\nFull Name = " << FullName << endl;

Required By Visual Studio Net

# include <iostream># include <fstream> using namespace std;

Required To Set Format Size # include <iomanip.h>

30

c

31

Constants vs. Variables

int A, B = 10;

A is Variable whose contents Is garbage/unknown! It is a container/objectwhose value can be changed during the execution of the program!

B is Variable whose contents at the moment is 10 - Can be changedduring the execution of the program!

# define PI 3.14# define Ch 'T'

B = 2 + 3;

PI and Ch are Constants Whose Contents are assigned by the pre-processor directive at the beginning of the program. They are containers/objectswhose values can not be changed during the execution of the program!

B is Variable Whose Contents At The Moment is 10 - Can Be ChangedDuring The Execution Of The Program!

2, 3, 'A', "Name String" are also constants! 32

33

Array – Character string array - often referred to a "character string".

char FirstName[15], LastName[20];

FirstName[0] = 'E';FirstName[1] = 'D';FirstName[2] = '\0'; // FirstName[2] = 0;

strcpy (FirstName, "Ed");

printf ("Length of String FirstName = %ld\n", strlen (FirstName));

if (strcmp (FirstString, SecondString) == 0) printf ("%s = %s\n", FirstString, SecondString);

if (strcmp (FirstString, SecondString) > 0) printf ("%s > %s\n", FirstString, SecondString);else printf ("%s < %s\n", FirstString, SecondString);

C/C++

C++ has no basic string data type

STL - Standard Template LibraryInclude User Defined String Class

# include <string.h>

NULL Terminator => '\0'

Character Strings

34

c

35

Let’s Add A New C++ Source File

36

Name The File Main.cpp

.cpp vs .c

37

Add Code To Main.cpp

38

Compile & Run The Program

39

Program Brings Up Dialog BoxAnd Then return (0) returns Control To The Operating System

And The Console Window Is Closed!

This Is Characteristic Of Many C++

Terminal Environments!

40

Many Folks Force The Compiler To Stop Until The User Enters Any Key By Using getchar()!

Add This Line Of Code

41

c

42

In C/C++, Some Functions Have An Explicit Return; Some Folks Refer To These As Functions Or Modules.

In C/C++, Some Functions Do Not Have An Explicit Return; Some Folks Refer To These As Functions Or

Modules Or Procedures

Why do we create Functions?

1] Reduce Duplication

2] Abstraction - Partition Task Into Major Ideas

3] Divide Task Into Smaller Portions That Can Be

Written By A Team Of Programmers.

43

c

44

Normal Program Layout Order

1. Include Statements

2. Define Statements

3. Structs & Classes

4. Function Prototypes

5. Functions In Some Type Of Logical Order (Including main)• Declare Variables At Top

45

c

46

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////                                                                              ////                            File Main.cpp                                     ////                                                                              //// Purpose :      ////                                                                              //// Written By : xxxxxxxxxxxxxxxxxxx                Environment : Windows 7    //// Date : xx/xx/xxxx                                  Compiler : Visual C++     //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Minimal File Document For Each File This Semester?

Don't Use Tabs In Documentation! - Tabs Are Different In Different Environments!

Do Use Tabs To Indent Code!

Must Be Boxed!

Must Be At The Top Of The Code!

47

c

48

Add The Program Documentation To Main.cpp

Should Still Compile!

49

Minimal Module Document For Each Function, EXCEPTmain, This Semester?

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ClearScreen //// //// Purpose : Clear the console screen/window. //// //// Written By : xxxxxxxxxxxxxxxxxxx                Environment : Windows 7    //// Date : xx/xx/xxxx                                  Compiler : Visual C++     //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Don't Use Tabs In Documentation! - Tabs Are Different In Different Environments!

Do Use Tabs To Indent Code!

Must Be Boxed!

Must Be Immediately Above The Code!

50

c

51

Add The Function Documentation To Main.cpp

Should Still Compile!

52

c

53

Add The Function Documentation To Main.cpp

Should Still Compile!

54

c

55

How Big Shall I Make My Functions?

1] 50 Lines Of Code vs. "No More Than Half Hour For

An Experienced Computer Scientist To Understand The

Complexity“ [ ½ Hr Better!]

* Each Module Should Have Well Defined Task!

* Write Them With Maintenance In Mind - Good

Variables, Well Indented, Well Documented, Easy To

Understand. [Solve General Problems!]

56

void ClearScreen(void){

system("cls");}

One Line Modules Are Not Often Appropriate! One CanDecompose The Modularization Process Too Much!

ClearScreen Is An Example Of A One Line Module That Is Well Defined - Does One Task?

Function ClearScreen Might Be Called 200 Times Within A Given Interactive System.

The Task Of Clearing The Screen Is Platform Dependent!

Having A Function, Such As ClearScreen, Makes EasierTo Port Programs To Other Environments - One Place ToChange Code! 57

More General ClearScreen Solution

void ClearScreen(void){#ifdef INTERACTIVE //===============================================================

#ifdef SYMANTEC_MAC //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ printf ("\f");#endif //SYMANTEC_MAC ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#ifdef MICROSOFT_VISUAL //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ system("cls");#endif //MICROSOFT_VISUAL ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#ifdef BORLAND_IBM //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ clrscr();#endif //BORLAND_IBM +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#ifdef LINUX //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ system("clear"); #endif //LINUX ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#ifdef CURSES //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ clear(); refresh();#endif //CURSES ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# endif // INTERACTIVE =============================================================}

//# define BORLAND_IBM//# define SYMANTEC_IBM//# define SYMANTEC_MAC//# define LINUX//# define CODE_WARRIOR_IBM//# define CODE_WARRIOR_MAC# define MICROSOFT_VISUAL

# define INTERACTIVE

58

stdio.h

void HitCarriageReturnToContinue (void) {char

JunkString[255];

cin.flush();cout << " <Hit Any Key To Continue>\n";

#ifdef INTERACTIVE //=============================================================cin.getline(JunkString,255);

#endif // INTERACTIVE ============================================================}

void HitCarriageReturnToContinue (void) {char

JunkString[255];

fflush (stdin);puts (" <Hit Any Key To Continue>)");

#ifdef INTERACTIVE //=============================================================gets(JunkString);

#endif // INTERACTIVE ============================================================}

void Delay (int NoSeconds) {#ifdef INTERACTIVE //=============================================================

-- You Do!#endif // INTERACTIVE ============================================================}

There Are Other Times One Might Want To Use # INTERACTIVE

Prog > Output.txt 59

iostream.h

stdio.h

c

60

Hold Down Go To Class Share

\\tucc-tiger\class\thicks\2320

61

We Will Soon Learn To Create .hpp & .cpp FilesWe Will Often Use These Files In Many Projects

I have created Utilities.hpp & Utilities.cpp for you to use; we will use them in most of our programs. COPY THEM TO YOUR PROJECT FOLDER

62

Add The Files To Our Project

63

Select Utilities.hpp & Utilities.cppPush/Select The Add Button

64

Utilities.hpp & Utilities.cppAre Now In The Project

65

Alter Your Main Program & Run!Leave All Documentation

Check Out All Of The Functions In Utilities This Week!They Are To Help You In C/C++ Programming

We Will Use Them In Almost All Of Our Programs 66

c

67

Remove All Of The Code In MainExcept The Documentation & The Following:

SAVE & EXIT WHEN DONE!68

Make 5 Copies Of Your Project FolderName Them As Illustrated Below Using Your Name

AT THE END OF CLASS, COPY FOLDERS TO YOUR FLASH DRIVE OR Y DRIVE

ALWAYS HAVE ONE AVAILABLE IN CLASS69

c

70

Normal Program Layout Order

1. Include Statements

2. Define Statements

3. Structs & Classes

4. Function Prototypes

5. Functions In Some Type Of Logical Order (Including main)• Declare Variables At Top

71

c

72

Includes Defines

Use Your Name

73

c

A Parameter is the variable which is part of the method's signature (method

declaration). An Argument is an expression used when calling the method.

Consider the following code

void Foo(int i, float f)

{

// Do things

}

void Bar()

{

int x = 1;

Foo (x, 2.0);

}

Here i & f are the Parameters, and x &2.0 are the Arguments.

74

c

75

DisplayInfo(); Call/Evoke Function DisplayInfo

void DisplayInfo(void){   printf ("Name = Dr. Thomas Hicks\n");  puts ("Phone = (210) 999-7483");}

Functions With No Explicit ReturnAlso Called "Non-Value Returning Functions"

• There are no formal parameters

• Function is called DisplayInfo

• There is no explicit return

• There are no local variable in function DisplayInfo

76

Calling/Evoking a Function

A function is Called (Evoked) by specifying its name followed by its arguments.

Non-value returning functions: function-name (data passed to function);DisplayInfo(); or ClearScreen();

Value returning functions: results = function-name (data passed to function);

NewMax = Max(23, 17);

77

c

78

Includes Defines Prototypes

2 Add The Prototype

1 Add The Function

3 Add The Function Call

Functions With No Explicit ReturnAlso Called "Non-Value Returning Functions"

79

Always Document Your FunctionsBut Don’t Take Time To Do So During Lecture

Update The Code At The Bottom Of The File

Place a copy above DisplayInfo and complete the documentation.

80

c

81

Functions With An Explicit ReturnAlso Called "Value-Returning Functions"

int Max(int x, int y){ int Maximum;

if(x >= y) Maximum = x; else Maximum = y;

return (Maximum);}

int No1 = 5, No2 = 7, Big;

We might call/evoke function Max by passing two variables.

Big = Max(No1, No2);

We might call/evoke function Max by passing two constants.

Big = Max(-3, -1);

We might call/evoke function Max by passing one contant and one variable.

Big = Max(No2, -1);Big = Max(-1, No2); 82

• Two parameters x & yPassed by Value – meaning any changes made to x or y will not effect the original arguments (No1, or No2).

Functions With An Explicit ReturnAlso Called "Value-Returning Functions"

int Max(int x, int y){ int Maximum;

if(x >= y) Maximum = x; else Maximum = y;

return (Maximum);}

• Function Is Called Max

• Return value will be an int

• Local Variable Maximum

int No1 = 5, No2 = 7, Big;

Big = Max(No1, No2);

83

c

84

Add Function Max To Your Program

2 Add The Prototype

1 Add Max

3 Add Variables To Main

85

Add Function Calls To Max

86

Why Function Prototypes?

The use of function prototypes Permits Error Checking of data types by the compiler.

It also Ensures Conversion of all Arguments passed to the function to the declared argument data type when the function is called.

87

c

88

About No1 (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

int main(int argc, char * argv[]){int No1 ;

Memory Value _______________________________________________________

Symbolic Name ______________________________________________________

No1

No1

Garbage ??

??

Assign Value 101 ___________________________________________________________________

Print The Value Contained In Memory _________________________________________

101

No1 = 101

printf ("No1 = %d\n", No1);

Address Where Stored __________________________________________________________&1000

&1000

Print The Address (10) Where Stored __________________________________________printf ("&No1 = %ld\n", &No1);

Print The Address (16) Where Stored __________________________________________printf ("&No1 = %X\n", &No1);

Print The Size (in bytes) _________________________________________________________printf ("sizeof( No1) = %ld\n", sizeof(No1));

4 bytes

89

c

90

About No2 (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

int main(int argc, char * argv[]){int No1, No2 = 12 ;

Memory Value _______________________________________________________

Symbolic Name ______________________________________________________

No1

Assign Value 202 ___________________________________________________________________

Print The Value Contained In Memory _________________________________________

101

printf ("No2 = %d\n", No2);

Address Where Stored __________________________________________________________

&1000

Print The Address (10) Where Stored __________________________________________printf ("&No2 = %ld\n", &No2);

Print The Address (16) Where Stored __________________________________________printf ("&No2 = %X\n", &No2);

Print The Size (in bytes) _________________________________________________________

4 bytes

No2

No2

12

12

No2 = 202

202

&1004

&1004

printf ("sizeof( No1) = %ld\n", sizeof(No1));

4 bytes

91

c

92

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ;

}

No15

&1000

2 bytes

No212

&1002

2 bytes

void SwapA (short int A, short int B){short int Temp;

Temp = A; A = B; B = Temp;}

There Is No Memory For A or B

Function Not Called!

[0] SwapA (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

93

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ;

printf ("No1 = %d\n", No1); ____________

printf ("No2 = %d\n", No2); ____________

}

No15

&1000

2 bytes

No212

&1002

2 bytes

No1 = 5

No2 = 12

void SwapA (short int A, short int B){short int Temp;

Temp = A; A = B; B = Temp;}

[1] SwapA (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

94

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ;

printf ("No1 = %d\n", No1); _____________

printf ("No2 = %d\n", No2); _____________

SwapA ( No1, No2);

}

No1 = 5

No2 = 12

A Is It Passed By Value Or By Reference? _____

Referenceint * AInt & A

ValueInt A

V

No15

&1000

2 bytes

No212

&1002

2 bytes

void SwapA (short int A, short int B){short int Temp;

Temp = A; A = B; B = Temp;}

[2] SwapA (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

95

void SwapA (short int A, short int B){short int Temp;

Temp = A; A = B; B = Temp;}

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ;

printf ("No1 = %d\n", No1); _____________

printf ("No2 = %d\n", No2); _____________

SwapA ( No1, No2);

}

No1 = 5

No2 = 12

Since A Is By Reference

Create A short int Variable Whose Symolic Name

Is AAnd Fill It With The Value Of The First Argument In The Function Call.

A5

&1004

2 bytes

No15

&1000

2 bytes

No212

&1002

2 bytes

[3] SwapA (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

96

void SwapA (short int A, short int B){short int Temp;

Temp = A; A = B; B = Temp;}

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ;

printf ("No1 = %d\n", No1); _____________

printf ("No2 = %d\n", No2); _____________

SwapA ( No1, No2);

}

No1 = 5

No2 = 12

Since B Is By Reference

Create An int Variable Whose Symolic Name Is BAnd Fill It With The Value Of The Second ArgumentIn The Function Call.

No15

&1000

2 bytes

No212

&1002

2 bytes

A5

&1004

2 bytes

B12

&1006

2 bytes

[4] SwapA (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

97

void SwapA (short int A, short int B){short int Temp;

Temp = A; A = B; B = Temp;}

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ;

printf ("No1 = %d\n", No1); _____________

printf ("No2 = %d\n", No2); _____________

SwapA ( No1, No2);

}

No1 = 5

No2 = 12

Temp is a local variable will be destroyed when function is finished.

Create An int Variable Whose Symolic Name

Is Temp Contains Garbage

No15

&1000

2 bytes

No212

&1002

2 bytes

A5

&1004

2 bytes

B12

&1006

2 bytes

Temp?

&1008

2 bytes

[5] SwapA (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

98

void SwapA (short int A, short int B){short int Temp;

printf ("No2 = %d\n", No2);

Temp = A; A = B; B = Temp;}

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ;

printf ("No1 = %d\n", No1); _____________

printf ("No2 = %d\n", No2); _____________

SwapA ( No1, No2);

}

No1 = 5

No2 = 12

Function SwapA can access• it’s parameters (A & B) , • global variables (bad practice), • defines, and • it’s local variables (Temp).

No15

&1000

2 bytes

No212

&1002

2 bytes

A5

&1004

2 bytes

B12

&1006

2 bytes

Temp?

&1008

2 bytes

[6] SwapA (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

99

void SwapA (short int A, short int B){short int Temp;

printf ("A = %d\n", A); _____________ printf ("B = %d\n", B); _____________ Temp = A; A = B; B = Temp;

printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); }

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________ SwapA ( No1, No2);

}

No1 = 5No2 = 12

No15

&1000

2 bytes

No212

&1002

2 bytes

A5

&1004

2 bytes

B12

&1006

2 bytes

Temp?

&1008

2 bytes

A = 5B = 12

[7] SwapA (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

100

void SwapA (short int A, short int B){short int Temp;

printf ("A = %d\n", A); _____________ printf ("B = %d\n", B); _____________ Temp = A; A = B; B = Temp;

printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); }

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________ SwapA ( No1, No2);

}

No1 = 5No2 = 12

No15

&1000

2 bytes

No212

&1002

2 bytes

A5

&1004

2 bytes

B12

&1006

2 bytes

Temp?

&1008

2 bytes

A = 5B = 12

5

[8] SwapA (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

101

void SwapA (short int A, short int B){short int Temp;

printf ("A = %d\n", A); _____________ printf ("B = %d\n", B); _____________ Temp = A; A = B; B = Temp;

printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); }

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________ SwapA ( No1, No2);

}

No1 = 5No2 = 12

No15

&1000

2 bytes

No212

&1002

2 bytes

A5

&1004

2 bytes

B12

&1006

2 bytes

Temp?

&1008

2 bytes

A = 5B = 12

512

[9] SwapA (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

102

void SwapA (short int A, short int B){short int Temp;

printf ("A = %d\n", A); _____________ printf ("B = %d\n", B); _____________ Temp = A; A = B; B = Temp;

printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); }

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________ SwapA ( No1, No2);

}

No1 = 5No2 = 12

No15

&1000

2 bytes

No212

&1002

2 bytes

A5

&1004

2 bytes

B12

&1006

2 bytes

Temp?

&1008

2 bytes

A = 5B = 12

512 5

[10] SwapA (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

103

void SwapA (short int A, short int B){short int Temp;

printf ("A = %d\n", A); _____________ printf ("B = %d\n", B); _____________ Temp = A; A = B; B = Temp;

printf ("A = %d\n", A); _____________ printf ("B = %d\n", B); _____________ printf ("Temp = %d\n", Temp); }

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________ SwapA ( No1, No2);

}

No1 = 5No2 = 12

No15

&1000

2 bytes

No212

&1002

2 bytes

A5

&1004

2 bytes

B12

&1006

2 bytes

Temp?

&1008

2 bytes

A = 5B = 12

512 5 B = 5A = 12

[11] SwapA (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

104

void SwapA (short int A, short int B){short int Temp;

printf ("A = %d\n", A); _____________ printf ("B = %d\n", B); _____________ Temp = A; A = B; B = Temp;

printf ("A = %d\n", A); _____________ printf ("B = %d\n", B); _____________ printf ("Temp = %d\n", Temp); }

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________ SwapA ( No1, No2);

}

No1 = 5No2 = 12

No15

&1000

2 bytes

No212

&1002

2 bytes

A5

&1004

2 bytes

B12

&1006

2 bytes

Temp?

&1008

2 bytes

A = 5B = 12

512 5B = 5A = 12

[12] SwapA (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

105

void SwapA (short int A, short int B){short int Temp;

Temp = A; A = B; B = Temp;}

[13] SwapA (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________ SwapA ( No1, No2); printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________

}

No1 = 5No2 = 12

No15

&1000

2 bytes

No212

&1002

2 bytes

No1 = 5No2 = 12

106

Calling A Function By Value

The function receives a copy of the actual arguments passed to the function as parameters.

The function can change the value of the parameter, but cannot change the values of the actual actual arguments.

107

c

108

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ;

}

No15

&1000

2 bytes

No212

&1002

2 bytes

void SwapB (short int * A, short int * B){short int Temp;

Temp = *A; *A = *B; *B = Temp;}

There Is No Memory For A or B

Function Not Called!

[0] SwapB(Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

109

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ;

printf ("No1 = %d\n", No1); _____________

printf ("No2 = %d\n", No2); _____________

}

No15

&1000

2 bytes

No212

&1002

2 bytes

No1 = 5

No2 = 12

void SwapB (short int * A, short int * B){short int Temp;

Temp = *A; *A = *B; *B = Temp;}

[1] SwapB (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

110

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ;

printf ("No1 = %d\n", No1); _____________

printf ("No2 = %d\n", No2); _____________

SwapB ( &No1, &No2);

}

No1 = 5

No2 = 12

A Is It Passed By Value Or By Reference? _____

Referenceint * AInt & A

ValueInt A

R

No15

&1000

2 bytes

No212

&1002

2 bytes

void SwapB (short int * A, short int * B){short int Temp;

Temp = *A; *A = *B; *B = Temp;}

[2] SwapB (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

The Only Way To Change No1 Is To Use A Pointer Variable Or A Reference Variable; We Are Using A Standard C Pointer Variable In ThisExample!

111

void SwapB (short int * A, short int * B){short int Temp;

Temp = *A; *A = *B; *B = Temp;}

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ;

printf ("No1 = %d\n", No1); _____________

printf ("No2 = %d\n", No2); _____________

SwapB ( &No1, &No2);

}

No1 = 5

No2 = 12

Since A Is By Reference

Create A Pointer To A short int Variable Whose

Symolic Name Is A And Fill It With The Address Passed In The First Argument In The Function Call.

A&1000

&1004

4 bytes

No15

&1000

2 bytes

No212

&1002

2 bytes

[3] SwapB (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

Store &No1 In Pointer A

112

void SwapB (short int * A, short int * B){short int Temp;

Temp = *A; *A = *B; *B = Temp;}

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ;

printf ("No1 = %d\n", No1); _____________

printf ("No2 = %d\n", No2); _____________

SwapB ( &No1, &No2);

}

No1 = 5

No2 = 12

Since B Is By Reference

No15

&1000

2 bytes

No212

&1002

2 bytes

[4] SwapB (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

A&1000

&1004

4 bytes

B&1002

&1008

4 bytes

Create A Pointer To A short int Variable Whose

Symolic Name Is B And Fill It With The Address Passed In The Second Argument In The Function Call.

Store &No2 In Pointer B

113

void SwapB (short int * A, short int * B){short int Temp;

Temp = *A; *A = *B; *B = Temp;}

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ;

printf ("No1 = %d\n", No1); _____________

printf ("No2 = %d\n", No2); _____________

SwapB ( &No1, &No2);

}

No1 = 5

No2 = 12

Temp is a local variable will be destroyed when function is finished.

Create An int Variable Whose Symolic Name

Is Temp Contains Garbage

No15

&1000

2 bytes

No212

&1002

2 bytes

Temp?

&1012

2 bytes

[5] SwapB (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

A&1000

&1004

4 bytes

B&1002

&1008

4 bytes

114

void SwapB (short int * A, short int * B){short int Temp;

printf ("No2 = %d\n", No2);

Temp = *A; *A = *B; *B = Temp;}

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ;

printf ("No1 = %d\n", No1); _____________

printf ("No2 = %d\n", No2); _____________

SwapB ( &No1, &No2);

}

No1 = 5

No2 = 12

Function SwapB can access• it’s parameters (A & B) , • global variables (bad practice), • defines, and • it’s local variables (Temp).

No15

&1000

2 bytes

No212

&1002

2 bytes

[6] SwapB (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

Temp?

&1012

2 bytes

A&1000

&1004

4 bytes

B&1002

&1008

4 bytes

115

void SwapB (short int * A, short int * B){short int Temp;

printf ("A = %d\n", A); ____________ printf ("B = %d\n", B); ____________ printf ("*A = %d\n", *A); ____________ printf ("*B = %d\n", *B); ____________ Temp = *A; *A = *B; *B = Temp;

printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); }

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________ SwapB ( &No1, &No2);

}

No1 = 5No2 = 12

No15

&1000

2 bytes

No212

&1002

2 bytes

A = 1000B = 1002

[7] SwapB (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

Temp?

&1012

2 bytes

A&1000

&1004

4 bytes

B&1002

&1008

4 bytes

*A = 5*B = 12

116

void SwapB (short int * A, short int * B){short int Temp;

printf ("A = %d\n", A); _____________ printf ("B = %d\n", B); _____________ printf ("*A = %d\n", *A); _____________ printf ("*B = %d\n", *B); _____________ Temp = *A; *A = *B; *B = Temp;

printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); }

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________ SwapB ( &No1, &No2);

}

No15

&1000

2 bytes

No212

&1002

2 bytes

[8] SwapB (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

Temp?

&1012

2 bytes

A&1000

&1004

4 bytes

B&1002

&1008

4 bytes

5

No1 = 5No2 = 12

A = 1000B = 1002*A = 5*B = 12

117

void SwapB (short int * A, short int * B){short int Temp;

printf ("A = %d\n", A); ____________ printf ("B = %d\n", B); ____________ printf ("*A = %d\n", *A); ____________ printf ("*B = %d\n", *B); ____________ Temp = *A; *A = *B; *B = Temp;

printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); }

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________ SwapB ( &No1, &No2);

}

No15

&1000

2 bytes

No212

&1002

2 bytes

[9] SwapB (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

Temp?

&1012

2 bytes

A&1000

&1004

4 bytes

B&1002

&1008

4 bytes

5

12

void SwapB (short int * A, short int * B){short int Temp;

printf ("A = %d\n", A); ____________ printf ("B = %d\n", B); ____________ printf ("*A = %d\n", *A); ____________ printf ("*B = %d\n", *B); ____________ Temp = *A; *A = *B; *B = Temp;

printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); }

No1 = 5No2 = 12

A = 1000B = 1002*A = 5*B = 12

118

void SwapB (short int * A, short int * B){short int Temp;

printf ("A = %d\n", A); ____________ printf ("B = %d\n", B); ____________ printf ("*A = %d\n", *A); ____________ printf ("*B = %d\n", *B); ____________ Temp = *A; *A = *B; *B = Temp;

printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); }

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________ SwapB ( &No1, &No2);

}

No15

&1000

2 bytes

No212

&1002

2 bytes

[10] SwapB (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

Temp?

&1012

2 bytes

A&1000

&1004

4 bytes

B&1002

&1008

4 bytes

5

12 5

void SwapB (short int * A, short int * B){short int Temp;

printf ("A = %d\n", A); ____________ printf ("B = %d\n", B); ____________ printf ("*A = %d\n", *A); ____________ printf ("*B = %d\n", *B); ____________ Temp = *A; *A = *B; *B = Temp;

printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); }

No1 = 5No2 = 12

A = 1000B = 1002*A = 5*B = 12

119

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); ____________ printf ("No2 = %d\n", No2); _____________ SwapB ( &No1, &No2);

}

[11] SwapB (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

Temp?

&1012

2 bytes

A&1000

&1004

4 bytes

B&1002

&1008

4 bytes

5

void SwapB (short int * A, short int * B){short int Temp;

printf ("A = %d\n", A); ____________ printf ("B = %d\n", B); ____________ printf ("*A = %d\n", *A); ____________ printf ("*B = %d\n", *B); ____________ Temp = *A; *A = *B; *B = Temp;

printf ("A = %d\n", A); __________ printf ("B = %d\n", B); __________ printf ("Temp = %d\n", Temp); __________}

No15

&1000

2 bytes

No212

&1002

2 bytes

12 5

No1 = 5No2 = 12

A = 1000B = 1002*A = 5*B = 12

A = 1000B = 1002

Temp = 5

120

[12] SwapB (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________ SwapB ( &No1, &No2);

}

Temp?

&1012

2 bytes

A&1000

&1004

4 bytes

B&1002

&1008

4 bytes

5

void SwapB (short int * A, short int * B){short int Temp;

printf ("A = %d\n", A); ______ printf ("B = %d\n", B); ______ printf ("*A = %d\n", *A); ______ printf ("*B = %d\n", *B); ______ Temp = *A; *A = *B; *B = Temp;

printf ("A = %d\n", A); ______ printf ("B = %d\n", B); ______ printf ("Temp = %d\n", Temp); ______}

No15

&1000

No212

&1002

12 5

No1 = 5No2 = 12

A = 1000B = 1002*A = 5*B = 12

A = 1000B = 1002

Temp = 5

121

void SwapB (short int * A, short int * B){short int Temp;

Temp = *A; *A = *B; *B = Temp;}

[13] SwapB (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________ SwapA ( No1, No2); printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________

}

No15

&1000

No212

&1002

12 5

No1 = 5No2 = 12

No1 = 5No2 = 12

122

Calling a function By Reference

Very useful when we need a function which "returns more than one value".

The formal parameter becomes an alias (a pointer to) for the actual parameter.

The function can change the values of the actual arguments.

123

c

124

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ;

}

No15

&1000

2 bytes

No212

&1002

2 bytes

void SwapC (short int & A, short & int B){short int Temp;

Temp = A; A = B; B = Temp;}

There Is No Memory For A or B

Function Not Called!

[0] SwapC(Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

125

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ;

printf ("No1 = %d\n", No1); _____________

printf ("No2 = %d\n", No2); _____________

}

No15

&1000

2 bytes

No212

&1002

2 bytes

void SwapC (short int & A, short & int B){short int Temp;

Temp = A; A = B; B = Temp;}

[1] SwapC (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

No1 = 5

No2 = 12

126

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ;

printf ("No1 = %d\n", No1); _____________

printf ("No2 = %d\n", No2); _____________

SwapC ( No1, No2);

}

A Is It Passed By Value Or By Reference? _____

Referenceint AInt & A

ValueInt A

R

No15

&1000

2 bytes

No212

&1002

2 bytes

void SwapC (short int & A, short & int B){short int Temp;

Temp = A; A = B; B = Temp;}

[2] SwapC (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

The Only Way To Change No1 Is To Use A Pointer Variable Or A Reference Variable; We Are Using A Reference Variable In ThisExample!

No1 = 5

No2 = 12

127

void SwapC (short int & A, short & int B){short int Temp;

Temp = A; A = B; B = Temp;}

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ;

printf ("No1 = %d\n", No1); _____________

printf ("No2 = %d\n", No2); _____________

SwapC ( No1, No2);

}

Since A Is By Reference

Create A Pointer To A short int Variable Whose

Symolic Name Is A And Fill It With The Address Passed In The First Argument In The Function Call.

A&1000

&1004

4 bytes

No15

&1000

2 bytes

No212

&1002

2 bytes

[3] SwapC (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

Store &No1 In Pointer A

No1 = 5

No2 = 12

128

void SwapC (short int & A, short & int B){short int Temp;

Temp = A; A = B; B = Temp;}

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ;

printf ("No1 = %d\n", No1); _____________

printf ("No2 = %d\n", No2); _____________

SwapC ( No1, No2);

}

Since B Is By Reference

No15

&1000

2 bytes

No212

&1002

2 bytes

[4] SwapC (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

A&1000

&1004

4 bytes

B&1002

&1008

4 bytes

Create A Pointer To A short int Variable Whose

Symolic Name Is B And Fill It With The Address Passed In The Second Argument In The Function Call.

Store &No2 In Pointer B

No1 = 5

No2 = 12

129

void SwapC (short int & A, short & int B){short int Temp;

Temp = A; A = B; B = Temp;}

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ;

printf ("No1 = %d\n", No1); _____________

printf ("No2 = %d\n", No2); _____________

SwapC ( No1, No2);

}

Temp is a local variable will be destroyed when function is finished.

Create An int Variable Whose Symolic Name

Is Temp Contains Garbage

No15

&1000

2 bytes

No212

&1002

2 bytes

Temp?

&1012

2 bytes

[5] SwapC (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

A&1000

&1004

4 bytes

B&1002

&1008

4 bytes

No1 = 5

No2 = 12

130

void SwapC (short int & A, short & int B){short int Temp;

printf ("No2 = %d\n", No2);

Temp = A; A = B; B = Temp;}

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ;

printf ("No1 = %d\n", No1); _____________

printf ("No2 = %d\n", No2); _____________

SwapC ( No1, No2);

}

Function SwapC can access• it’s parameters (A & B) , • global variables (bad practice), • defines, and • it’s local variables (Temp).

No15

&1000

2 bytes

No212

&1002

2 bytes

[6] SwapC (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

Temp?

&1012

2 bytes

A&1000

&1004

4 bytes

B&1002

&1008

4 bytes

No1 = 5

No2 = 12

131

void SwapC (short int & A, short & int B){short int Temp;

printf ("A = %d\n", A); _____________ printf ("B = %d\n", B); _____________ printf ("*A = %d\n", *A); _____________ printf ("*B = %d\n", *B); _____________ Temp = A; A = B; B = Temp;

printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); }

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________ SwapC ( No1, No2);

}

No15

&1000

2 bytes

No212

&1002

2 bytes

[7] SwapC (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

Temp?

&1012

2 bytes

A&1000

&1004

4 bytes

B&1002

&1008

4 bytes

No1 = 5No2 = 12

A = 1000B = 1002*A = 5*B = 12

132

void SwapC (short int & A, short & int B){short int Temp;

printf ("A = %d\n", A); ____________ printf ("B = %d\n", B); ____________ printf ("*A = %d\n", *A); ____________ printf ("*B = %d\n", *B); ____________ Temp = A; A = B; B = Temp;

printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); }

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________ SwapC ( No1, No2);

}

No15

&1000

2 bytes

No212

&1002

2 bytes

[8] SwapC (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

Temp?

&1012

2 bytes

A&1000

&1004

4 bytes

B&1002

&1008

4 bytes

5

No1 = 5No2 = 12

A = 1000B = 1002*A = 5*B = 12

133

void SwapC (short int & A, short & int B){short int Temp;

printf ("A = %d\n", A); ____________ printf ("B = %d\n", B); ____________ printf ("*A = %d\n", *A); ____________ printf ("*B = %d\n", *B); ____________ Temp = A; A = B; B = Temp;

printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); }

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________ SwapC ( No1, No2);

}

No15

&1000

2 bytes

No212

&1002

2 bytes

[9] SwapC (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

Temp?

&1012

2 bytes

A&1000

&1004

4 bytes

B&1002

&1008

4 bytes

5

12

No1 = 5No2 = 12

A = 1000B = 1002*A = 5*B = 12

134

void SwapC (short int & A, short & int B){short int Temp;

printf ("A = %d\n", A); _____________ printf ("B = %d\n", B); _____________ printf ("*A = %d\n", *A); _____________ printf ("*B = %d\n", *B); _____________ Temp = A; A = B; B = Temp;

printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); }

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________ SwapC ( No1, No2);

}

No15

&1000

2 bytes

No212

&1002

2 bytes

[10] SwapC (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

Temp?

&1012

2 bytes

A&1000

&1004

4 bytes

B&1002

&1008

4 bytes

5

12 5

No1 = 5No2 = 12

A = 1000B = 1002*A = 5*B = 12

135

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________ SwapC ( No1, No2);

}

[11] SwapC (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

Temp?

&1012

2 bytes

A&1000

&1004

4 bytes

B&1002

&1008

4 bytes

5

void SwapC (short int & A, short & int B){short int Temp;

printf ("A = %d\n", A); ____________ printf ("B = %d\n", B); ____________ printf ("*A = %d\n", *A); ____________ printf ("*B = %d\n", *B); ____________ Temp = A; A = B; B = Temp;

printf ("A = %d\n", A); ____________ printf ("B = %d\n", B); ____________ printf ("Temp = %d\n", Temp); ____________ }

No15

&1000

2 bytes

No212

&1002

2 bytes

12 5

No1 = 5No2 = 12

A = 1000B = 1002*A = 5*B = 12

A = 1000B = 1002Temp = 5

136

void SwapC (short int & A, short & int B){short int Temp;

printf ("A = %d\n", A); ____________ printf ("B = %d\n", B); ____________ printf ("*A = %d\n", *A); ____________ printf ("*B = %d\n", *B); ____________ Temp = A; A = B; B = Temp;

printf ("A = %d\n", A); ____________ printf ("B = %d\n", B); ____________ printf ("Temp = %d\n", Temp); ____________ }

[12] SwapC (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________ SwapC ( No1, No2);

}

Temp?

&1012

2 bytes

A&1000

&1004

4 bytes

B&1002

&1008

4 bytes

5

No15

&1000

No212

&1002

12 5

No1 = 5No2 = 12

A = 1000B = 1002*A = 5*B = 12

A = 1000B = 1002Temp = 5

137

void SwapC (short int & A, short & int B){short int Temp; Temp = A; A = B; B = Temp;}

[13] SwapC (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

int main(int argc, char * argv[]){short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________ SwapC ( No1, No2); printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________

}

No15

&1000

No212

&1002

12 5

No1 = 5No2 = 12

No1 = 5No2 = 12

138

c

139

int main(int argc, char * argv[]){short int No1 = 5;

printf ("No1 = %d\n", No1); _____________

}

No15

&1000

2 bytes

void Increment (short int & No1){ No1++;}

[0] Increment (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

No1 = 5

140

int main(int argc, char * argv[]){short int No1 = 5;

printf ("No1 = %d\n", No1); _________________

Increment(No1);

}

No15

&1000

2 bytes

void Increment (short int & No1){ No1++;}

[1] Increment (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

No1 = 5

No1&1000

&10042

4 bytes

Store &No1 In Pointer No1

There is a difference between main.No1 & Increment.No1

141

int main(int argc, char * argv[]){short int No1 = 5;

printf ("No1 = %d\n", No1); _________________

Increment(No1);

printf ("No1 = %d\n", No1); _________________

}

No15

&1000

2 bytes

void Increment (short int & No1){ No1++;}

[2] Increment (Assume Compiler Assigns Memory Starting At &1000 (Decimal) )

No1 = 5

No1 = 6

6

142

c

143

void ClearScreen(void){

system("cls");}

Arguments Passed By _?_ {Value/Reference/Both/NA} __________

void Sum (int No1, int No2, int & Sum){

Sum = No1 + No2;}

144

NA

Arguments Passed By _?_ {Value/Reference/Both/NA} __________Both

Passed Arguments By Value

void DisplayFraction (int Numerator, int Denominator){

printf (" %d/%d ", Numerator, Denominator);Numerator = Numerator + 1;Denominator = Denominator --;

}

# define C 4int A = 5, B = 6;

DisplayFraction(1,2); // passed 2 constantsDisplayFraction(C,2); // passed 2 constantsDisplayFraction(1,A); // passed constant and a variableDisplayFraction(B,C); // passed constant and a variableDisplayFraction(A,B); // passed 2 variables

Evoke/Call Procedure/Module/Function DisplayFraction

145

void Swap (int & No1, int & No2){int Temp; Temp = No1; No1 = No2; No2 = Temp;}

# define C 4int A = 5, B = 6;

Swap (A,B); // must be passed 2 variables// Swap (C,2); // May Not Pass Constants!

Evoke/Call Procedure/Module/Function Swap

Reference Variables &

146

Passed Arguments By Reference

void Swap (int * No1, int * No2){int Temp; Temp = * No1; * No1 = * No2; * No2 = Temp;}

# define C 4int A = 5, B = 6;

Swap (&A,&B); // must be passed 2 variables

Evoke/Call Procedure/Module/Function Swap

Pointer Variables

147

Passed Arguments By Reference With Pointers

void Sum (int No1, int No2, int & Sum){

Sum = No1 + No2;}

int Total, A = 5, B = 6;

Sum (1, 2, Total); // passed 2 constants & variable (by reference)Sum ( A, 2, Total ); // passed constant, variable (by value), & variable (by reference)Sum ( A, B, Total ); // passed two variable (by value), & variable (by reference)

Value Variables

Reference Variable &

148

Passed Arguments By Reference With Reference Variables

Often More Than One Way To Solve A Problem! Many Languages, Including C & C++, Have Explicit Functions - Functions That Explicitly Return Something! We, As Software Engineers, Have Choices How We Design The Functionality & Interface!

void AreaCircle1 (float Radius, float & Area){

Area = PI * Radius * Radius;}

float AreaCircle2 (float Radius){int Area;

Area = PI * Radius * Radius;return (Area);}

float AreaCircle3 (float Radius){ return (PI * Radius * Radius);}

<== Yes we can write it simpler!

AreaCircle1 (10.0, TempArea); VolumeCone = 5.0 * TempArea /3.;

VolumeCone = 5 * AreaCircle2 (10.0)/3;

Difference? Clarity of logicand ability to chain Functionsmathematically!

149

Explicit Functions

void AreaCircle1 (float Radius, float & Area){

Area = PI * Radius * Radius;}

float AreaCircle2 (float Radius){int Area;

Area = PI * Radius * Radius;return (Area);}

Some Programming Languages ReferTo These As Procedures

And These As Functions!

Both are modules/subroutines/sub programs!

Some C/C++ use the same terminology!

Other C/C++ call "all modules functions" - some are simply "void functions"

150

c

151

http://cermics.enpc.fr/~ts/C/FUNCTIONS/function.ref.html

C & C++ Functions References

http://www.infosys.utas.edu.au/info/documentation/C/CStdLib.html

152