Download presentation
Presentation is loading. Please wait.
Published byAngelica Robertson Modified over 9 years ago
1
1
2
c 2 2
3
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
4
http://Carme.cs.trinity.edu 4 4
5
5 5
6
Printing! Use Computer Science Printers Only For Your "Computer Science Homework!!!!" Print Only What Is Necessary! 6 6
7
c 7
8
c 8 8
9
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
10
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
11
c 11 If You Don’t Know These, Come Back & Review Them! Know How To Declare & The Capacity Limitations!
12
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!
13
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, 767 4 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 0-255 1 charSee ASCII Table 0-255 bool1 bytetrue/false1 bytetrue/false 13
14
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,295 14 We are not going to use the 64 bit Integers
15
c 15
16
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
17
Some Way To Verify The Range? # include #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
18
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
19
c 19 Be In Your Seat & Logged On In Windows ByThe Time The Class Is Scheduled To Begin!
20
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!
21
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
22
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
23
c 23
24
Start Visual Studio Create A New Project 24
25
Start Visual Studio Create A New Project 25
26
Start Visual Studio Create A New Project You Can Double-Click To Start Visual Studio With This Application 26
27
c 27
28
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!
29
c 29
30
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
31
c 31
32
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
33
34
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
35
c 35
36
Let’s Add A New C++ Source File 36
37
Name The File Main.cpp.cpp vs.c 37
38
Add Code To Main.cpp 38
39
Compile & Run The Program 39
40
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
41
Many Folks Force The Compiler To Stop Until The User Enters Any Key By Using getchar()! Add This Line Of Code 41
42
c 42
43
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
44
c 44
45
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
46
c 46
47
////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// // // // 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
48
c 48
49
Add The Program Documentation To Main.cpp Should Still Compile! 49
50
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
51
c 51
52
Add The Function Documentation To Main.cpp Should Still Compile! 52
53
c 53
54
Add The Function Documentation To Main.cpp Should Still Compile! 54
55
c 55
56
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
57
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
58
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
59
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
60
c 60
61
Hold DownGo To Class Share \\tucc-tiger\class\thicks\2320 61
62
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
63
Add The Files To Our Project 63
64
Select Utilities.hpp & Utilities.cpp Push/Select The Add Button 64
65
Utilities.hpp & Utilities.cpp Are Now In The Project 65
66
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
67
c 67
68
Remove All Of The Code In Main Except The Documentation & The Following: 68
69
Make 5 Copies Of Your Project Folder Name Them As Illustrated Below Using Your Name 69
70
c 70
71
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
72
c 72
73
Includes Defines Use Your Name 73
74
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
75
c 75
76
DisplayInfo(); Call/Evoke Function DisplayInfo void DisplayInfo(void) { printf ("Name = Dr. Thomas Hicks\n"); puts ("Phone = (210) 999-7483"); } 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
77
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
78
c 78
79
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
80
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
81
c 81
82
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
83
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
84
c 84
85
Add Function Max To Your Program 2 Add The Prototype 1 Add Max 3 Add Variables To Main 85
86
Add Function Calls To Max 86
87
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
88
c 88
89
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
90
c 90
91
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 = 202 202 &1004 printf ("sizeof( No1) = %ld\n", sizeof(No1)); 4 bytes 91
92
c 92
93
int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; } No15 &1000 2 bytes No2 12 &1002 2 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
94
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 No2 12 &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
95
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 &1000 2 bytes No2 12 &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
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 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 &1004 2 bytes No15 &1000 2 bytes No2 12 &1002 2 bytes [3] SwapA (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) 96
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 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 &1000 2 bytes No2 12 &1002 2 bytes A5 &1004 2 bytes B12 &1006 2 bytes [4] SwapA (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) 97
98
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 No2 12 &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
99
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 No2 12 &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
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 &1000 2 bytes No2 12 &1002 2 bytes A5 &1004 2 bytes B12 &1006 2 bytes Temp? &1008 2 bytes A = 5 B = 12 [7] SwapA (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) 100
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 &1000 2 bytes No2 12 &1002 2 bytes A5 &1004 2 bytes B12 &1006 2 bytes Temp? &1008 2 bytes A = 5 B = 12 5 [8] SwapA (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) 101
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 &1000 2 bytes No2 12 &1002 2 bytes A5 &1004 2 bytes B12 &1006 2 bytes Temp? &1008 2 bytes A = 5 B = 12 512 [9] SwapA (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) 102
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 &1000 2 bytes No2 12 &1002 2 bytes A5 &1004 2 bytes B12 &1006 2 bytes Temp? &1008 2 bytes A = 5 B = 12 5 125 [10] SwapA (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) 103
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 &1000 2 bytes No2 12 &1002 2 bytes A5 &1004 2 bytes B12 &1006 2 bytes Temp? &1008 2 bytes A = 5 B = 12 5 125 B = 5 A = 12 [11] SwapA (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) 104
105
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 &1000 2 bytes No2 12 &1002 2 bytes A5 &1004 2 bytes B12 &1006 2 bytes Temp? &1008 2 bytes A = 5 B = 12 5 125 B = 5 A = 12 [12] SwapA (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) 105
106
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 &1000 2 bytes No2 12 &1002 2 bytes No1 = 5 No2 = 12 106
107
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
108
c 108
109
int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; } No15 &1000 2 bytes No2 12 &1002 2 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
110
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 No2 12 &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
111
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 &1000 2 bytes No2 12 &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 This Example! 111
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 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 No2 12 &1002 2 bytes [3] SwapB (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) Store &No1 In Pointer A 112
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 Since B Is By Reference No15 &1000 2 bytes No2 12 &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
114
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 No2 12 &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
115
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 No2 12 &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
116
void SwapB (short int * A, short int * B) { short int Temp; printf ("A = %d\n", A); ____________ printf ("B = %d\n", B); ____________ printf ("*A = %d\n", *A); ____________ printf ("*B = %d\n", *B); ____________ Temp = *A; *A = *B; *B = Temp; printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); } int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________ SwapB ( &No1, &No2); } No1 = 5 No2 = 12 No15 &1000 2 bytes No2 12 &1002 2 bytes A = 1000 B = 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
117
void SwapB (short int * A, short int * B) { short int Temp; printf ("A = %d\n", A); _____________ printf ("B = %d\n", B); _____________ printf ("*A = %d\n", *A); _____________ printf ("*B = %d\n", *B); _____________ Temp = *A; *A = *B; *B = Temp; printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); } int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________ SwapB ( &No1, &No2); } No15 &1000 2 bytes No2 12 &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 = 5 No2 = 12 A = 1000 B = 1002 *A = 5 *B = 12 117
118
void SwapB (short int * A, short int * B) { short int Temp; printf ("A = %d\n", A); ____________ printf ("B = %d\n", B); ____________ printf ("*A = %d\n", *A); ____________ printf ("*B = %d\n", *B); ____________ Temp = *A; *A = *B; *B = Temp; printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); } int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________ SwapB ( &No1, &No2); } No15 &1000 2 bytes No2 12 &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 = 5 No2 = 12 A = 1000 B = 1002 *A = 5 *B = 12 118
119
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 No2 12 &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 125 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 = 12 119
120
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 No2 12 &1002 2 bytes 125 No1 = 5 No2 = 12 A = 1000 B = 1002 *A = 5 *B = 12 A = 1000 B = 1002 Temp = 5 120
121
[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 No2 12 &1002 125 No1 = 5 No2 = 12 A = 1000 B = 1002 *A = 5 *B = 12 A = 1000 B = 1002 Temp = 5 121
122
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 &1002 125 No1 = 5 No2 = 12 No1 = 5 No2 = 12 122
123
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
124
c 124
125
int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; } No15 &1000 2 bytes No2 12 &1002 2 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
126
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 No2 12 &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
127
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 &1000 2 bytes No2 12 &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 This Example! No1 = 5 No2 = 12 127
128
void SwapC (short int & A, short & int B) { short int Temp; Temp = A; A = B; B = Temp; } int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________ SwapC ( No1, No2); } Since 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 No2 12 &1002 2 bytes [3] SwapC (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) Store &No1 In Pointer A No1 = 5 No2 = 12 128
129
void SwapC (short int & A, short & int B) { short int Temp; Temp = A; A = B; B = Temp; } int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________ SwapC ( No1, No2); } Since B Is By Reference No15 &1000 2 bytes No2 12 &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
130
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 No2 12 &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
131
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 No2 12 &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
132
void SwapC (short int & A, short & int B) { short int Temp; printf ("A = %d\n", A); _____________ printf ("B = %d\n", B); _____________ printf ("*A = %d\n", *A); _____________ printf ("*B = %d\n", *B); _____________ Temp = A; A = B; B = Temp; printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); } int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________ SwapC ( No1, No2); } No15 &1000 2 bytes No2 12 &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 = 5 No2 = 12 A = 1000 B = 1002 *A = 5 *B = 12 132
133
void SwapC (short int & A, short & int B) { short int Temp; printf ("A = %d\n", A); ____________ printf ("B = %d\n", B); ____________ printf ("*A = %d\n", *A); ____________ printf ("*B = %d\n", *B); ____________ Temp = A; A = B; B = Temp; printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); } int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________ SwapC ( No1, No2); } No15 &1000 2 bytes No2 12 &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 = 5 No2 = 12 A = 1000 B = 1002 *A = 5 *B = 12 133
134
void SwapC (short int & A, short & int B) { short int Temp; printf ("A = %d\n", A); ____________ printf ("B = %d\n", B); ____________ printf ("*A = %d\n", *A); ____________ printf ("*B = %d\n", *B); ____________ Temp = A; A = B; B = Temp; printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); } int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); _____________ printf ("No2 = %d\n", No2); _____________ SwapC ( No1, No2); } No15 &1000 2 bytes No2 12 &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 = 5 No2 = 12 A = 1000 B = 1002 *A = 5 *B = 12 134
135
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 No2 12 &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 125 No1 = 5 No2 = 12 A = 1000 B = 1002 *A = 5 *B = 12 135
136
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 No2 12 &1002 2 bytes 125 No1 = 5 No2 = 12 A = 1000 B = 1002 *A = 5 *B = 12 A = 1000 B = 1002 Temp = 5 136
137
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 No2 12 &1002 125 No1 = 5 No2 = 12 A = 1000 B = 1002 *A = 5 *B = 12 A = 1000 B = 1002 Temp = 5 137
138
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 &1002 125 No1 = 5 No2 = 12 No1 = 5 No2 = 12 138
139
c 139
140
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
141
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
142
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
143
c 143
144
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
145
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
146
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
147
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
148
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
149
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
150
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
151
c 151
152
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.