1
c 2 2
Check Schedule Page Daily! /2320/schedule.html We will often, but not always, discuss upcoming assignments check the page! Hints-Reading Assignments-Written Assignments/Labs! 3 3
4 4
5 5
Printing! Use Computer Science Printers Only For Your "Computer Science Homework!!!!" Print Only What Is Necessary! 6 6
c 7
c 8 8
Microsoft Office Example 9 Structured Language Developed Windows Version First Mac Version Cost 88% of Windows Version Yuk! OOP Language Developed Windows Version First Wrote 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 int1 bytes-128 to 1272 bytes-32,768 to 32, 767 int2 bytes-32,768 to 32, bytes-2,147,483,648 to 2,147,483,647 long int4 bytes-2,147,483,648 to 2,147,483,647 4 bytes-2,147,483,648 to 2,147,483,647 float4 bytes7 digits accuracy4 bytes7 digits accuracy double8 bytes15 digits accuracy8 bytes15 digits accuracy long double 12 bytes19 digits accuracy8 bytesNot Supported Compiles char1 charSee ASCII Table charSee ASCII Table bool1 bytetrue/false1 bytetrue/false 13
Other Basic Data Types ? unsigned short int a = 1; unsigned int b = 2; unsigned long int c = 3; Data TypeMinimal Standard Range / Precision Visual C++ Compiler Range / Precision unsigned short int1 bytes0 to 2552 bytes0 to 65,535 unsigned int2 bytes0 to 65,5354 bytes0 to 4,294,967,295 unsigned long int4 bytes0 to 4,294,967,2964 bytes0 to 4,294,967, We are not going to use the 64 bit Integers
c 15
How can I find out the limits on a given compiler? # include 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 #define SHRT_MIN (-32768) /* minimum (signed) short value */ #define SHRT_MAX /* maximum (signed) short value */ #define USHRT_MAX 0xffff /* maximum unsigned short value */ #define INT_MIN ( ) /* minimum (signed) int value */ #define INT_MAX /* maximum (signed) int value */ #define UINT_MAX 0xffffffff /* maximum unsigned int value */ #define LONG_MIN ( L - 1) /* minimum (signed) long value */ #define LONG_MAX L /* maximum (signed) long value */ #define ULONG_MAX 0xffffffffUL /* maximum unsigned long value */ etc. 17
Can We Display These If Defined? # include 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 I Have Taught This Class Using A "Lecture Oriented Approach" Only I Use The Computer I Can Get Through A Lot Of 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 Notes 2] 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 Studio With This Application 26
c 27
c # include 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 cout > X; cout > FirstName; cout << endl << "First Name = " << FirstName << endl; cout > PayRate; cout << "\nPay Rate = " << PayRate << endl; cout << "Enter Full Name : "; cin.getline(FullName); cout << "\nFull Name = " << FullName << endl; Required By Visual Studio Net # include # include using namespace std; Required To Set Format Size # include 30
c 31
Constants vs. Variables int A, B = 10; A is Variable whose contents Is garbage/unknown! It is a container/object whose value can be changed during the execution of the program! B is Variable whose contents at the moment is 10 - Can be changed during 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/objects whose values can not be changed during the execution of the program! B is Variable Whose Contents At The Moment is 10 - Can Be Changed During 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 Library Include User Defined String Class # include 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 Box And 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, EXCEPT main, 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 Can Decompose 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 Easier To Port Programs To Other Environments - One Place To Change 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 \n"; #ifdef INTERACTIVE //============================================================= cin.getline(JunkString,255); #endif // INTERACTIVE ============================================================ } void HitCarriageReturnToContinue (void) { char JunkString[255]; fflush (stdin); puts (" )"); #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 DownGo To Class Share \\tucc-tiger\class\thicks\
We Will Soon Learn To Create.hpp &.cpp Files We 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.cpp Push/Select The Add Button 64
Utilities.hpp & Utilities.cpp Are 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 Main Except The Documentation & The Following: 68
Make 5 Copies Of Your Project Folder Name Them As Illustrated Below Using Your Name 69
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) "); } Functions With No Explicit Return Also 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 Return Also Called "Non-Value Returning Functions" 79
Always Document Your Functions But 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 Return Also 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 & y Passed by Value – meaning any changes made to x or y will not effect the original arguments (No1, or No2). Functions With An Explicit Return Also 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 Garbage ?? ?? Assign Value 101 ___________________________________________________________________ Print The Value Contained In Memory _________________________________________ 101 No1 = 101 printf ("No1 = %d\n", No1); Address Where Stored __________________________________________________________ &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 12 No2 = &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 & bytes No2 12 & bytes void SwapA (short int A, short int B) { short int Temp; Temp = A; A = B; B = Temp; } [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 & bytes No2 12 & 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? _____ Reference int * A Int & A Value Int A V No15 & bytes No2 12 & 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 A And Fill It With The Value Of The First Argument In The Function Call. A5 & bytes No15 & bytes No2 12 & 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 B And Fill It With The Value Of The Second Argument In The Function Call. No15 & bytes No2 12 & bytes A5 & bytes B12 & 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 & bytes No2 12 & bytes A5 & bytes B12 & bytes Temp? & 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 & bytes No2 12 & bytes A5 & bytes B12 & bytes Temp? & 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 = 5 No2 = 12 No15 & bytes No2 12 & bytes A5 & bytes B12 & bytes Temp? & bytes A = 5 B = 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 = 5 No2 = 12 No15 & bytes No2 12 & bytes A5 & bytes B12 & bytes Temp? & bytes A = 5 B = 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 = 5 No2 = 12 No15 & bytes No2 12 & bytes A5 & bytes B12 & bytes Temp? & bytes A = 5 B = [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 = 5 No2 = 12 No15 & bytes No2 12 & bytes A5 & bytes B12 & bytes Temp? & bytes A = 5 B = [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 = 5 No2 = 12 No15 & bytes No2 12 & bytes A5 & bytes B12 & bytes Temp? & bytes A = 5 B = B = 5 A = 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 = 5 No2 = 12 No15 & bytes No2 12 & bytes A5 & bytes B12 & bytes Temp? & bytes A = 5 B = B = 5 A = 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 = 5 No2 = 12 No15 & bytes No2 12 & bytes No1 = 5 No2 =
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 & bytes No2 12 & bytes void SwapB (short int * A, short int * B) { short int Temp; Temp = *A; *A = *B; *B = Temp; } [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 & bytes No2 12 & 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? _____ Reference int * A Int & A Value Int A R No15 & bytes No2 12 & 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 This Example! 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 & bytes No15 & bytes No2 12 & 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 & bytes No2 12 & bytes [4] SwapB (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) A&1000 & bytes B&1002 & 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 & bytes No2 12 & bytes Temp? & bytes [5] SwapB (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) A&1000 & bytes B&1002 & 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 & bytes No2 12 & bytes [6] SwapB (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) Temp? & bytes A&1000 & bytes B&1002 & 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 = 5 No2 = 12 No15 & bytes No2 12 & bytes A = 1000 B = 1002 [7] SwapB (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) Temp ? & bytes A&1000 & bytes B&1002 & bytes *A = 5 *B =
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 & bytes No2 12 & bytes [8] SwapB (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) Temp? & bytes A&1000 & bytes B&1002 & bytes 5 No1 = 5 No2 = 12 A = 1000 B = 1002 *A = 5 *B =
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 & bytes No2 12 & bytes [9] SwapB (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) Temp? & bytes A&1000 & bytes B&1002 & 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 = 5 No2 = 12 A = 1000 B = 1002 *A = 5 *B =
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 & bytes No2 12 & bytes [10] SwapB (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) Temp? & bytes A&1000 & bytes B&1002 & bytes 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 = 5 No2 = 12 A = 1000 B = 1002 *A = 5 *B =
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? & bytes A&1000 & bytes B&1002 & 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 & bytes No2 12 & bytes 125 No1 = 5 No2 = 12 A = 1000 B = 1002 *A = 5 *B = 12 A = 1000 B = 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? & bytes A&1000 & bytes B&1002 & 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 No2 12 & No1 = 5 No2 = 12 A = 1000 B = 1002 *A = 5 *B = 12 A = 1000 B = 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 No2 12 & No1 = 5 No2 = 12 No1 = 5 No2 =
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 & bytes No2 12 & bytes void SwapC (short int & A, short & int B) { short int Temp; Temp = A; A = B; B = Temp; } [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 & bytes No2 12 & 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 =
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? _____ Reference int A Int & A Value Int A R No15 & bytes No2 12 & 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 This Example! No1 = 5 No2 =
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 & bytes No15 & bytes No2 12 & bytes [3] SwapC (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) Store &No1 In Pointer A No1 = 5 No2 =
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 & bytes No2 12 & bytes [4] SwapC (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) A&1000 & bytes B&1002 & 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 =
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 & bytes No2 12 & bytes Temp? & bytes [5] SwapC (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) A&1000 & bytes B&1002 & bytes No1 = 5 No2 =
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 & bytes No2 12 & bytes [6] SwapC (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) Temp? & bytes A&1000 & bytes B&1002 & bytes No1 = 5 No2 =
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 & bytes No2 12 & bytes [7] SwapC (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) Temp ? & bytes A&1000 & bytes B&1002 & bytes No1 = 5 No2 = 12 A = 1000 B = 1002 *A = 5 *B =
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 & bytes No2 12 & bytes [8] SwapC (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) Temp? & bytes A&1000 & bytes B&1002 & bytes 5 No1 = 5 No2 = 12 A = 1000 B = 1002 *A = 5 *B =
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 & bytes No2 12 & bytes [9] SwapC (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) Temp? & bytes A&1000 & bytes B&1002 & bytes 5 12 No1 = 5 No2 = 12 A = 1000 B = 1002 *A = 5 *B =
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 & bytes No2 12 & bytes [10] SwapC (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) Temp? & bytes A&1000 & bytes B&1002 & bytes No1 = 5 No2 = 12 A = 1000 B = 1002 *A = 5 *B =
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? & bytes A&1000 & bytes B&1002 & 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 & bytes No2 12 & bytes 125 No1 = 5 No2 = 12 A = 1000 B = 1002 *A = 5 *B = 12 A = 1000 B = 1002 Temp = 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? & bytes A&1000 & bytes B&1002 & bytes 5 No15 &1000 No2 12 & No1 = 5 No2 = 12 A = 1000 B = 1002 *A = 5 *B = 12 A = 1000 B = 1002 Temp = 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 No2 12 & No1 = 5 No2 = 12 No1 = 5 No2 =
c 139
int main(int argc, char * argv[]) { short int No1 = 5; printf ("No1 = %d\n", No1); _____________ } No15 & 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 & bytes void Increment (short int & No1) { No1++; } [1] Increment (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) No1 = 5 No1&1000 & 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 & bytes void Increment (short int & No1) { No1++; } [2] Increment (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) No1 = 5 No1 =
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 4 int A = 5, B = 6; DisplayFraction(1,2); // passed 2 constants DisplayFraction(C,2); // passed 2 constants DisplayFraction(1,A); // passed constant and a variable DisplayFraction(B,C); // passed constant and a variable DisplayFraction(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 4 int 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 4 int 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 logic and ability to chain Functions mathematically! 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 Refer To 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
C & C++ Functions References 152