1. c 2 2 check schedule page daily! /2320/schedule.html we will often, but not always, discuss...

152
1

Upload: angelica-robertson

Post on 11-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

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

1

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

c

22

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

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

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

http://Carme.cs.trinity.edu4

4

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

5

5

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

Printing!

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

Print Only What Is Necessary!

6

6

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

c

7

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

c

88

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

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

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

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

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

c

11

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

Know How To Declare & The Capacity Limitations!

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

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!

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

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

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

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

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

c

15

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

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

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

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

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

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

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

c

19

Be In Your Seat & Logged On In Windows

ByThe Time The Class Is Scheduled To Begin!

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

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!

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

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

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

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

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

c

23

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

Start Visual Studio Create A New Project

24

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

Start Visual Studio Create A New Project

25

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

Start Visual Studio Create A New Project

You Can Double-Click To Start Visual StudioWith This Application

26

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

c

27

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

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!

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

c

29

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

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

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

c

31

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

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

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

33

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

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

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

c

35

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

Let’s Add A New C++ Source File

36

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

Name The File Main.cpp

.cpp vs .c

37

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

Add Code To Main.cpp

38

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

Compile & Run The Program

39

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

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

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

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

Add This Line Of Code

41

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

c

42

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

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

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

c

44

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

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

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

c

46

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

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////                                                                              ////                            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

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

c

48

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

Add The Program Documentation To Main.cpp

Should Still Compile!

49

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

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

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

c

51

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

Add The Function Documentation To Main.cpp

Should Still Compile!

52

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

c

53

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

Add The Function Documentation To Main.cpp

Should Still Compile!

54

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

c

55

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

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

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

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

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

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

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

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

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

c

60

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

Hold Down Go To Class Share

\\tucc-tiger\class\thicks\2320

61

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

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

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

Add The Files To Our Project

63

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

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

64

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

Utilities.hpp & Utilities.cppAre Now In The Project

65

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

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

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

c

67

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

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

SAVE & EXIT WHEN DONE!68

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

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

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

c

70

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

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

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

c

72

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

Includes Defines

Use Your Name

73

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

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

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

c

75

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

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

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

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

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

c

78

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

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

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

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

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

c

81

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

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

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

• 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

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

c

84

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

Add Function Max To Your Program

2 Add The Prototype

1 Add Max

3 Add Variables To Main

85

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

Add Function Calls To Max

86

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

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

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

c

88

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

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

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

c

90

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

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

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

c

92

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

c

108

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

[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

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

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

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

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

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

c

124

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

c

139

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

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

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

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

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

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

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

c

143

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

c

151

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

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