Functions & Memory Maps Review C Programming Language

Slides:



Advertisements
Similar presentations
Programming and Data Structure
Advertisements

What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Kernighan/Ritchie: Kelley/Pohl:
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
C For Java Programmers Tom Roeder CS sp. Why C? The language of low-level systems programming  Commonly used (legacy code)  Trades off safety.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 10: Appendices.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
1. c 2 2 Check Schedule Page Daily! /2320/schedule.html We will often, but not always, discuss upcoming assignments.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
1. c 2 c 3 3 Microsoft Office Example 4 Structured Language Developed Windows Version First Mac Version Cost 88% of Windows Version Yuk! OOP Language.
Make a Copy Of File Main.cpp (of Student-Class). Place It On Your Desktop. Open it With A Text Editor 3.
1 2 2 Call The Project Dynamic-Memory 4 4 # include "Utilities.hpp" int main(int argc, char * argv[]) { short int *PtrNo; (*PtrNo) = 5; printf ("(*PtrNo)
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Lecture 3: Getting Started & Input / Output (I/O)
CS314 – Section 5 Recitation 9
Lecture 5 Pointers 1. Variable, memory location, address, value
Object Lifetime and Pointers
Chapter 10 : Implementing Subprograms
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
EGR 2261 Unit 11 Pointers and Dynamic Variables
Computer Organization and Design Pointers, Arrays and Strings in C
Lesson #8 Structures Linked Lists Command Line Arguments.
User-Written Functions
Overview 4 major memory segments Key differences from Java stack
C Primer.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
UNIT 5 C Pointers.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Review C Programming Language
Pointers & Dynamic Memory
Basic Elements of C++.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Pointers and Pointer-Based Strings
CSC113: Computer Programming (Theory = 03, Lab = 01)
Command-Line Arguments
C Basics.
Arrays in C.
Basic Elements of C++ Chapter 2.
Object Oriented Programming COP3330 / CGS5409
Overview 4 major memory segments Key differences from Java stack
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Pointers, Dynamic Data, and Reference Types
More About Data Types & Functions
Programming in C Pointer Basics.
Variables Title slide variables.
Outline Defining and using Pointers Operations on pointers
Programming in C Pointer Basics.
Simulating Reference Parameters in C
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Overloading functions
Pointers and Pointer-Based Strings
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Data Structures and Algorithms Introduction to Pointers
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Programming in C Pointers and Arrays.
Pointers and dynamic objects
ETE 132 Lecture 6 By Munirul Haque.
Functions Reasons Concepts Passing arguments to a function
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
15213 C Primer 17 September 2002.
Pointers, Dynamic Data, and Reference Types
Storage Classes.
Presentation transcript:

Functions & Memory Maps Review C Programming Language Data Abstractions CSCI-2320 Dr. Tom Hicks Computer Science Department

c Constants 2

# define PI 3.14 What Is A Constant? Constant  a Value that cannot be altered by the program during normal execution, i.e., the value is constant. When associated with an Identifier, a constant is said to be "named," although the terms "constant" and "named constant" are often used interchangeably. # define PI 3.14 PI is a "named constant" in C/C++ The C/C++ Pre-Processor assigns the value to the constant with the pre-processor directive at the beginning of the program; their values can not be changed during the execution of the program! 3

c Variables 4

int Age, No = 5; What Is A Variable? Variable  a storage location paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a Value; these values can be changed during program execution. int Age, No = 5; Age is an integer Variable whose Value is unknown; some say the "garbage" value assigned is determined by the contents of memory at that instant in time. No is an integer Variable whose Value is known because this statement both creates and initializes the variable. 5

Datatyping Static vs. Dynamic 6

Static & Dynamic Data Typing int No = 5; Static Datatyping  once No is defined to be an integer, only integer data may be stored in the container. It is not an appropriate container in which to store a FullName. Static Datatyping Languages Include: Ada, C, C++, C#, JADE, Java, Fortran, Haskell, ML, Pascal, and Scala. Dynamic Datatyping  the datatype is determined by whatever is assigned to the container at the moment; It may store a No at one moment and a FullName at the next. Dynamic Datatyping Languages Include: JavaScript, Python, Ruby, PHP, Lua and Perl. 7

c Why Functions? 8

List 3 Reasons Why We Need Functions 1] Reduce Duplication 2] Reduce Abstraction: Partition Task Into Major Ideas 3] Divide Task Into Smaller Portions That Can Be Written By A Team Of Programmers. 9

Recommended Ordering Of Components Both C/C++ 10

Recommended Program Layout Order Include Statements Define Statements Structs & Classes (classes are C++) Function Prototypes Functions In Some Type Of Logical Order (Including main at the top) Many Companies Require Developers To Declare Variables At Top 11

c Function Size? 12

How Big Shall You Make A Function? 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!] 13

How Big Shall You Make A Function? void ClearScreen(void) { system("cls"); } Function ClearScreen, above, will clear the screen on a Windows System Short Functions Are Sometimes OK! void ClearScreen(void) { system("clear"); } Function ClearScreen, above, will clear the screen on a Mac Terminal or a Linux System Since the screen might have to be cleared more than 200 times within our Interactive System  it would be a good idea to create the Function  this would simplify the process of porting the code to the alternate system. 14

More General ClearScreen Solution void ClearScreen(void) { #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 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ } //# define BORLAND_IBM //# define SYMANTEC_IBM //# define SYMANTEC_MAC //# define LINUX //# define CURSES //# define CODE_WARRIOR_IBM //# define CODE_WARRIOR_MAC # define MICROSOFT_VISUAL Some Of You Would Might Prefer To Make One define Change  which then makes all of the alterations necessary for the various operating systems. 15

I Am Going To Use Memory Maps To c I Am Going To Use Memory Maps To Review What Happens With Memory In Main, Memory In Functions, Arguments Passed, etc. 16

HitCarriageReturnToContinue() Function HitCarriageReturnToContinue() Write A Function Which Will Prompt The User To "Hit Return Key To Continue" Delay Processing Until The User Hits The Return Key 17

Function  No Arguments  No Explicit Return void HitCarriageReturnToContinue (void) { char JunkString[5000]; flush_stream(stdin); printf("--------- <Hit Enter/Return Key To Continue> ---------- "); gets_s(JunkString); } int main (int argc, char * argv[]) { puts ("------------------- Start Of Main -------------------\n"); puts ("-------------------- End Of Main --------------------\n"); HitCarriageReturnToContinue(); return (0); } Called From main 18

c Memory Maps #1 Examine What Happens When We Create An Un-Initialized Integer, Called No1, In Main. 19

About No1 (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) int main(int argc, char * argv[]) { 4 bytes int No1 ; No1 Garbage ??  unknown ?? 101 No1 = 101 &1000 What Is The Symbolic Name ________________________________________ What Value Is Currently Stored In No1 __________________________________ Write An Assignment To Change The Value In No1 to 101 ____________________________ printf ("No1 = %d\n", No1); Print The Value Contained In No1 _______________________________________________ Assume That The Address Where No1 Is Stored = __________________________________ Print The Address (base 10) Where No1 Is Stored ___________________________________ printf ("&No1 = %ld\n", &No1); printf ("&No1 = %X\n", &No1); Print The Address (base 16) Where No1 Is Stored __________________________________ printf ("sizeof( No1) = %ld\n", sizeof(No1)); Print The Size (in bytes) of No1 _________________________________________________ MM-1 20

c Memory Maps #2 Examine What Happens When We Create An Initialized Integer, Called No2, In Main. 21

About No2 (Assume Compiler Assigns Memory Starting At &1004 (Decimal) ) int main(int argc, char * argv[]) { int No1, No2 = 12 ; printf ("sizeof( No2) = %ld\n", sizeof(No2)); 4 bytes 4 bytes No2 101 12 No1 No2 = 202 202 &1000 &1004 What Is The Symbolic Name ________________________________________ What Value Is Currently Stored In No2 __________________________________ Write An Assignment To Change The Value In No2 to 202 ____________________________ printf ("No2 = %d\n", No2); Print The Value Contained In No2 _______________________________________________ Assume That The Address Where No2 Is Stored = __________________________________ Print The Address (base 10) Where No2 is Stored __________________________________ printf ("&No2 = %ld\n", &No2); printf ("&No2 = %X\n", &No2); Print The Address (base 16) Where No2 Is Stored __________________________________ Print The Size (in bytes) of No2 _________________________________________________ MM-2 22

DisplayAddition(Addend1, Addend2) c Function DisplayAddition(Addend1, Addend2) Write A Function Which Will Display Addition Problem: Addend1 + Addend2 = Sum 23

Function  No Arguments  No Explicit Return void DisplayAddition (int Addend1, int Addend2) { int Sum = Addend1 + Addend2; printf("%d + %d = %d\n", Addend1, Addend2, Sum); } Addend1 & Addend2 Are Passed By Value int main (int argc, char * argv[]) { int No1 = 5, No2 = 6; DisplayAddition(No1, No2); DisplayAddition(3,4); } Called From main Function DisplayAddition Has No Access To No1 Or No2 Unless Their Values Are Passed In As Arguments. 24

DisplayAddition Exists, But Is Not Evoked/Called Memory Maps #3 DisplayAddition Exists, But Is Not Evoked/Called 25

There Is No Memory For Addend1, Addend2, Or Sum [0] DisplayAdditon (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) int main(int argc, char * argv[ ]) { short int No1 = 5, No2 = 6 ; } 2 bytes 2 bytes 5 No1 6 No2 &1000 &1002 There Is No Memory For Addend1, Addend2, Or Sum Function Not Called! void DisplayAddition (int Addend1, int Addend2) { int Sum = Addend1 + Addend2; printf("%d + %d = %d\n", Addend1, Addend2, Sum); } MM-3 26

DisplayAddition Is Evoked/Called Pass Arguments By Value Memory Maps #4 DisplayAddition Is Evoked/Called Pass Arguments By Value 27

MM-4A int main(int argc, char * argv[ ]) { short int No1 = 5, [0] DisplayAdditon (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) int main(int argc, char * argv[ ]) { short int No1 = 5, No2 = 6 ; DisplayAddition(No1, No2); } 2 bytes 2 bytes 5 No1 6 No2 &1000 &1002 MM-4A 1  Function DisplayAddition Is Called void DisplayAddition (int Addend1, int Addend2) { int Sum; Sum = Addend1 + Addend2; printf("%d + %d = %d\n", Addend1, Addend2, Sum); } 2  Addend1 Is Passed By Value  Create The Short Int Container Addend1 &2000 2 bytes 5 3  Place A Copy Of The Information In No1 (first argument match) Into The Container 28

MM-4B int main(int argc, char * argv[ ]) { short int No1 = 5, [1] DisplayAdditon (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) int main(int argc, char * argv[ ]) { short int No1 = 5, No2 = 6 ; DisplayAddition(No1, No2); } 2 bytes 2 bytes 5 No1 6 No2 &1000 &1002 MM-4B Addend1 &2000 2 bytes 5 void DisplayAddition (int Addend1, int Addend2) { int Sum; Sum = Addend1 + Addend2; printf("%d + %d = %d\n", Addend1, Addend2, Sum); } 4  Addend2 Is Passed By Value  Create The Short Int Container Addend2 &2002 2 bytes 5  Place A Copy Of The Information In No2 (second argument match) Into The Container 6 29

MM-4C int main(int argc, char * argv[ ]) { short int No1 = 5, [2] DisplayAdditon (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) int main(int argc, char * argv[ ]) { short int No1 = 5, No2 = 6 ; DisplayAddition(No1, No2); } 2 bytes 2 bytes 5 No1 6 No2 &1000 &1002 2 bytes ?? Sum &2002 Addend1 &2000 2 bytes 5 void DisplayAddition (int Addend1, int Addend2) { int Sum; Sum = Addend1 + Addend2; printf("%d + %d = %d\n", Addend1, Addend2, Sum); } 6  Create The Short Int Container For Sum Addend2 &2002 2 bytes 6 MM-4C 30

MM-4D int main(int argc, char * argv[ ]) { short int No1 = 5, [3] DisplayAdditon (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) int main(int argc, char * argv[ ]) { short int No1 = 5, No2 = 6 ; DisplayAddition(No1, No2); } 2 bytes 2 bytes 5 No1 6 No2 &1000 &1002 2 bytes 7  Place Addend1 (5) + Addend2 (6) into Sum 11 ?? Sum &2002 Addend1 &2000 2 bytes 5 void DisplayAddition (int Addend1, int Addend2) { int Sum; Sum = Addend1 + Addend2; printf("%d + %d = %d\n", Addend1, Addend2, Sum); } Addend2 &2002 2 bytes 6 MM-4D 31

MM-4E int main(int argc, char * argv[ ]) { short int No1 = 5, [4] DisplayAdditon (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) int main(int argc, char * argv[ ]) { short int No1 = 5, No2 = 6 ; DisplayAddition(No1, No2); } 2 bytes 2 bytes 5 No1 6 No2 &1000 &1002 2 bytes ?? 11 Sum &2002 Addend1 &2000 2 bytes 5 void DisplayAddition (int Addend1, int Addend2) { int Sum; Sum = Addend1 + Addend2; printf("%d + %d = %d\n", Addend1, Addend2, Sum); } Addend2 &2002 2 bytes 6 8  The Print Output Goes To The Terminal Console MM-4E 32

MM-4F int main(int argc, char * argv[ ]) { short int No1 = 5, [5] DisplayAdditon (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) int main(int argc, char * argv[ ]) { short int No1 = 5, No2 = 6 ; DisplayAddition(No1, No2); } 2 bytes 2 bytes 5 No1 6 No2 &1000 &1002 9  At The End Of The Function } All Of The Memory Allocated For Arguments & Local Variables Is Returned To The Operating System For Reuse  Addend1, Addend2, & Sum Cease To Exist! 2 bytes 11 Sum &2002 Addend1 &2000 2 bytes 5 void DisplayAddition (int Addend1, int Addend2) { int Sum; Sum = Addend1 + Addend2; printf("%d + %d = %d\n", Addend1, Addend2, Sum); } Addend2 &2002 2 bytes 6 MM-4F 33

c Function SwapA (A,B) Write A Function Which Will Swap The Contents Of A & B THIS ATTEMP FAILS! 34

Function  No Arguments  No Explicit Return void SwapA (short int A, short int B) { short int Temp; Temp = A; A = B; B = Temp; } A & B Are Passed By Value 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); } Called From main Function SwapA Has No Access To No1 Or No2 Unless Their Values Are Passed In As Arguments. 35

SwapA Is Evoked/Called Pass Arguments By Value Memory Maps #5 SwapA Is Evoked/Called Pass Arguments By Value 36

MM-5A int main(int argc, char * argv[]) { short int No1 = 5, [1] 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); ____________ } 2 bytes 2 bytes 5 No1 12 No2 &1000 &1002 No1 = 5 No2 = 12 void SwapA (short int A, short int B) { short int Temp; Temp = A; A = B; B = Temp; } MM-5A 37

MM-5B int main(int argc, char * argv[]) { short int No1 = 5, [2] 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); } 2 bytes 2 bytes 5 No1 12 No2 &1000 &1002 Reference int * A Int & A Value Int A No1 = 5 No2 = 12 V A  Is It Passed By Value Or By Reference? _____ void SwapA (short int A, short int B) { short int Temp; Temp = A; A = B; B = Temp; } MM-5B 38

MM-5C int main(int argc, char * argv[]) { short int No1 = 5, [3] 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); } 2 bytes 2 bytes 5 No1 12 No2 &1000 &1002 No1 = 5 No2 = 12 Since A Is By Reference Create A short int Variable Whose Symbolic Name Is A And Fill It With The Value Of The First Argument In The Function Call. void SwapA (short int A, short int B) { short int Temp; Temp = A; A = B; B = Temp; } A 5 &1004 2 bytes MM-5C 39

MM-5D int main(int argc, char * argv[]) { short int No1 = 5, [4] 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); } 2 bytes 2 bytes 5 No1 12 No2 &1000 &1002 No1 = 5 No2 = 12 Since B Is By Reference Create An int Variable Whose Symbolic Name Is B And Fill It With The Value Of The Second Argument In The Function Call. void SwapA (short int A, short int B) { short int Temp; Temp = A; A = B; B = Temp; } A 5 &1004 2 bytes B 12 &1006 2 bytes MM-5D 40

MM-5E int main(int argc, char * argv[]) { short int No1 = 5, [5] 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); } 2 bytes 2 bytes 5 No1 12 No2 &1000 &1002 No1 = 5 No2 = 12 Temp is a local variable  will be destroyed when function is finished. Create An int Variable Whose Symbolic Name Is Temp Contains Garbage void SwapA (short int A, short int B) { short int Temp; Temp = A; A = B; B = Temp; } A 5 &1004 2 bytes B 12 &1006 2 bytes Temp ? &1008 2 bytes MM-5E 41

MM-5G int main(int argc, char * argv[]) { short int No1 = 5, [6] 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); } 2 bytes 2 bytes 5 No1 12 No2 &1000 &1002 No1 = 5 No2 = 12 void SwapA (short int A, short int B) { short int Temp; printf ("No2 = %d\n", No2); Temp = A; A = B; B = Temp; } Function SwapA can access it’s parameters (A & B) , global variables (bad practice), defines, and it’s local variables (Temp). A 5 &1004 2 bytes B 12 &1006 2 bytes Temp ? &1008 2 bytes MM-5G 42

int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; [7] 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); } 2 bytes 2 bytes 5 No1 12 No2 &1000 &1002 No1 = 5 No2 = 12 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); } A = 5 B = 12 A 5 &1004 2 bytes B 12 &1006 2 bytes Temp ? &1008 2 bytes 43

int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; [8] 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); } 2 bytes 2 bytes 5 No1 12 No2 &1000 &1002 No1 = 5 No2 = 12 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); } A = 5 B = 12 A 5 &1004 2 bytes B 12 &1006 2 bytes Temp ? &1008 2 bytes 5 44

int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; [9] 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); } 2 bytes 2 bytes 5 No1 12 No2 &1000 &1002 No1 = 5 No2 = 12 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); } A = 5 B = 12 A 5 &1004 2 bytes B 12 &1006 2 bytes Temp ? &1008 2 bytes 12 5 45

int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; [10] 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); } 2 bytes 2 bytes 5 No1 12 No2 &1000 &1002 No1 = 5 No2 = 12 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); } A = 5 B = 12 A 5 &1004 2 bytes B 12 &1006 2 bytes Temp ? &1008 2 bytes 12 5 5 46

int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; [11] 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); } 2 bytes 2 bytes 5 No1 12 No2 &1000 &1002 No1 = 5 No2 = 12 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 ("Temp = %d\n", Temp); } A = 5 B = 12 A 5 &1004 2 bytes B 12 &1006 2 bytes Temp ? &1008 2 bytes A = 12 B = 5 12 5 5 47

int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; [12] 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); } 2 bytes 2 bytes 5 No1 12 No2 &1000 &1002 No1 = 5 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 ("Temp = %d\n", Temp); } No2 = 12 A = 5 B = 12 A = 12 A 5 &1004 2 bytes B 12 &1006 2 bytes Temp ? &1008 2 bytes B = 5 12 5 5 48

int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; [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); } 2 bytes 2 bytes 5 No1 12 No2 &1000 &1002 No1 = 5 No2 = 12 No1 = 5 No2 = 12 void SwapA (short int A, short int B) { short int Temp; Temp = A; A = B; B = Temp; } 49

Passing Arguments 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. 50

c Function SwapB (A,B) Write A Function Which Will Swap The Contents Of A & B THIS ATTEMP WORKS! 51

Function  No Arguments  No Explicit Return void SwapB (short int * A, short int * B) { short int Temp; Temp = *A; *A = *B; *B = Temp; } A & B Are Passed By Reference 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); } Called From main Since Function SwapB Uses Pointers  This Swap Will Be Able To Change The Values Of No1 & No2. 52

SwapB Is Evoked/Called Pass Arguments By Reference  POINTERS! Memory Maps #6 SwapB Is Evoked/Called Pass Arguments By Reference  POINTERS! 53

int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; [1] 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); ________ } 2 bytes 2 bytes 5 No1 12 No2 &1000 &1002 No1 = 5 No2 = 12 void SwapB (short int * A, short int * B) { short int Temp; Temp = *A; *A = *B; *B = Temp; } 54

int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; [2] 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); } 2 bytes 2 bytes 5 No1 12 No2 &1000 &1002 Reference int * A Int & A Value Int A No1 = 5 No2 = 12 R A  Is It Passed By Value Or By Reference? _____ void SwapB (short int * A, short int * B) { short int Temp; Temp = *A; *A = *B; *B = Temp; } 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! 55

Store &No1 In Pointer A Ptrs Are 4 Bytes [3] 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); } 2 bytes 2 bytes 5 No1 12 No2 &1000 &1002 Store &No1 In Pointer A 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. void SwapB (short int * A, short int * B) { short int Temp; Temp = *A; *A = *B; *B = Temp; } A &1000 &1004 4 bytes Ptrs Are 4 Bytes 56

int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; [4] 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); } 2 bytes 2 bytes 5 No1 12 No2 &1000 &1002 Store &No2 In Pointer B No1 = 5 No2 = 12 Since B Is By Reference 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. void SwapB (short int * A, short int * B) { short int Temp; Temp = *A; *A = *B; *B = Temp; } A &1000 &1004 4 bytes B &1002 &1008 4 bytes 57

int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; [5] 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); } 2 bytes 2 bytes 5 No1 12 No2 &1000 &1002 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 void SwapB (short int * A, short int * B) { short int Temp; Temp = *A; *A = *B; *B = Temp; } A &1000 &1004 4 bytes B &1002 &1008 4 bytes Temp ? &1012 2 bytes 58

int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; [6] 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); } 2 bytes 2 bytes 5 No1 12 No2 &1000 &1002 No1 = 5 No2 = 12 void SwapB (short int * A, short int * B) { short int Temp; printf ("No2 = %d\n", No2); Temp = *A; *A = *B; *B = Temp; } Function SwapB can access it’s parameters (A & B) , global variables (bad practice), defines, and it’s local variables (Temp). A &1000 &1004 4 bytes B &1002 &1008 4 bytes Temp ? &1012 2 bytes 59

int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; [7] 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); } 2 bytes 2 bytes 5 No1 12 No2 &1000 &1002 No1 = 5 No2 = 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); } A = 1000 B = 1002 *A = 5 *B = 12 A &1000 &1004 4 bytes B &1002 &1008 4 bytes Temp ? &1012 2 bytes 60

int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; [8] 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); } 2 bytes 2 bytes 5 No1 12 No2 &1000 &1002 No1 = 5 No2 = 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); } A = 1000 B = 1002 *A = 5 *B = 12 A &1000 &1004 4 bytes B &1002 &1008 4 bytes Temp ? &1012 2 bytes 5 61

int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; [9] 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); } 2 bytes 2 bytes 12 5 No1 12 No2 &1000 &1002 No1 = 5 No2 = 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); } 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); } A = 1000 B = 1002 *A = 5 *B = 12 A &1000 &1004 4 bytes B &1002 &1008 4 bytes Temp ? &1012 2 bytes 5 62

int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; [10] 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); } 2 bytes 2 bytes 5 5 12 No1 12 No2 &1000 &1002 No1 = 5 No2 = 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); } 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); } A = 1000 B = 1002 *A = 5 *B = 12 A &1000 &1004 4 bytes B &1002 &1008 4 bytes Temp ? &1012 2 bytes 5 63

int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; [11] 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); } 2 bytes 2 bytes 5 12 No1 12 5 No2 &1000 &1002 No1 = 5 No2 = 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); ______ } A = 1000 B = 1002 *A = 5 *B = 12 A &1000 &1004 4 bytes B &1002 &1008 4 bytes 2 bytes A = 1000 5 ? Temp B = 1002 Temp = 5 &1012 64

int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; [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); } 5 12 No1 12 5 No2 &1000 &1002 No1 = 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); ______ } No2 = 12 A = 1000 B = 1002 *A = 5 *B = 12 A = 1000 A &1000 &1004 4 bytes B &1002 &1008 4 bytes 2 bytes B = 1002 Temp = 5 ? 5 Temp &1012 65

int main(int argc, char * argv[]) { short int No1 = 5, No2 = 12 ; [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); ________ SwapB( No1, No2); } 5 12 No1 12 5 No2 &1000 &1002 No1 = 5 No2 = 12 No2 = 12 No1 = 5 void SwapB (short int * A, short int * B) { short int Temp; Temp = *A; *A = *B; *B = Temp; } 66

Passing Arguments 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. 67

Function Square (No1) Write A Function Which Will Explicitly Return The Square Of No1 68

Function  No Arguments  No Explicit Return long int Square (long int No1) { long Product = No1 * No1; return (Product); } No1 Is Passed By Value int main(int argc, char * argv[]) { long int No1 = 5; printf ("Square(No1) = %d\n", Square(No1)); printf("Square(10) = %ld\n", Square(10)); No1 = Square(12); printf("No1 = %ld\n", No1); } Called From main 69

SwapB Is Evoked/Called Pass Arguments By Reference  POINTERS! Memory Maps #7 SwapB Is Evoked/Called Pass Arguments By Reference  POINTERS! 70

int main(int argc, char * argv[]) { long int No1 = 5; [1] Square (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) int main(int argc, char * argv[]) { long int No1 = 5; printf ("No1 = %ld\n", No1); ____________ } 4 bytes 5 No1 &1000 No1 = 5 long int Square (long int No1) { return (No1 * No1); } 71

int main(int argc, char * argv[]) { long int No1 = 5; [2] Square (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) int main(int argc, char * argv[]) { long int No1 = 5; printf ("No1 = %ld\n", No1); ____________ printf("Square(No1) = %ld\n", Square(No1)); } 4 bytes 5 No1 &1000 No1 = 5 Create A long int Variable Whose Symbolic Name Is No1 And Fill It With The Value Of The First Argument In The Function Call. long int Square (long int No1) { long Product = No1 * No1; return (Product); } No1 5 &1004 4 bytes 72

int main(int argc, char * argv[]) { long int No1 = 5; [3] Square (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) int main(int argc, char * argv[]) { long int No1 = 5; printf ("No1 = %ld\n", No1); ____________ printf("Square(No1) = %ld\n", Square(No1)); } 4 bytes 5 No1 &1000 No1 = 5 Create A long int Variable Whose Symbolic Name Is Product And Fill It With The No1 * No1 long int Square (long int No1) { long Product = No1 * No1; return (Product); } No1 5 &1004 4 bytes Product 25 &1008 4 bytes 73

int main(int argc, char * argv[]) { long int No1 = 5; [4] Square (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) int main(int argc, char * argv[]) { long int No1 = 5; printf ("No1 = %ld\n", No1); ____________ printf("Square(No1) = %ld\n", Square(No1)); } 4 bytes 5 No1 &1000 No1 = 5 Return The Product to each and every place where Function Square Is Evoked/Called long int Square (long int No1) { long Product = No1 * No1; return (Product); } No1 5 &1004 4 bytes Product 25 &1008 4 bytes 74

int main(int argc, char * argv[]) { long int No1 = 5; [2] Square (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) int main(int argc, char * argv[]) { long int No1 = 5; printf ("No1 = %ld\n", No1); ____________ printf("Square(No1) = %ld\n", Square(No1)); } 4 bytes 5 No1 &1000 No1 = 5  Displays 25 On Terminal Create A long int Variable Whose Symbolic Name Is Product And Fill It With The No1 * No1 long int Square (long int No1) { long Product = No1 * No1; return (Product); } No1 5 &1004 4 bytes Product 25 &1008 4 bytes 75

int main(int argc, char * argv[]) { long int No1 = 5; [5] Square (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) int main(int argc, char * argv[]) { long int No1 = 5; printf("Square(No1) = %ld\n", Square(No1)); printf("Square(10) = %ld\n", Square(10)); No1 = Square(12); printf("No1 = %ld\n", No1); } 4 bytes 5 No1 &1000  Displays 25 On Terminal  Displays 100 On Terminal  Fills No1 With 144  Displays 144 On Terminal Return The Product to each and every place where Function Square Is Evoked/Called long int Square (long int No1) { long Product = No1 * No1; return (Product); } No1 &1004 4 bytes Product 25 &1008 4 bytes 144 76

Function Increment (No1) Write A Function Which Will Add 1 To The Short Integer Whose Address Is Passed REFERENCE VARIABLE  Preview Of Things To Come  C++ 77

Function  No Arguments  No Explicit Return void Increment (short int & No1) { No1++; } No1 Is Passed By Reference int main(int argc, char * argv[]) { short int No1 = 5; printf ("No1 = %d\n", No1); Increment(No1); } Called From main Function Increment Has No Access To No1 Unless It's Values Are Passed In As Arguments. 78

Increment Is Evoked/Called Preview Of C++ Reference Variables Memory Maps #8 Increment Is Evoked/Called Preview Of C++ Reference Variables 79

int main(int argc, char * argv[]) { short int No1 = 5; [0] Increment (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) int main(int argc, char * argv[]) { short int No1 = 5; printf ("No1 = %d\n", No1); _____________ } 2 bytes 5 No1 &1000 No1 = 5 void Increment (short int & No1) { No1++; } 80

int main(int argc, char * argv[]) { short int No1 = 5; [1] Increment (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) int main(int argc, char * argv[]) { short int No1 = 5; printf ("No1 = %d\n", No1); _________________ Increment(No1); } 2 bytes 5 No1 &1000 Store &No1 In Pointer No1 No1 = 5 void Increment (short int & No1) { No1++; } No1 &1000 &10042 4 bytes 81

Reference Variables An Opportunity To Avoid The Dreaded Pointers [2] Increment (Assume Compiler Assigns Memory Starting At &1000 (Decimal) ) 2 bytes int main(int argc, char * argv[]) { short int No1 = 5; printf ("No1 = %d\n", No1); _________________ Increment(No1); } 5 6 No1 &1000 No1 = 5 No1 = 6 Reference Variables An Opportunity To Avoid The Dreaded Pointers void Increment (short int & No1) { No1++; } 82

Passing Arrays To Functions? 83

void FillIntArray(int Array[], int StartingNo, int NoElements) { Arrays Are Always Passed As Pointers  Sizeof Cannot Be Used To Determine Capacity void FillIntArray(int Array[], int StartingNo, int NoElements) { printf("FillIntArray--> sizeof(Array)= %ld\n", sizeof(Array)); } int main (int argc, char * argv[]) int Nos[4]; printf("Main--> sizeof(Nos)= %ld\n", sizeof(Nos)); FillIntArray(Nos, 5, 4); Array &2000 4 bytes &1000 StartingNo &2004 5 NoElements &2008 4 Nos 20 bytes ?? I Have To Pass The NoToFill In Order To Know How Many Elements To Put Into Array 84

void FillIntArray(int Array[], int StartingNo, int NoElements) { Arrays Are Always Passed As Pointers  Function Call Did Not Include &Nos? void FillIntArray(int Array[], int StartingNo, int NoElements) { printf("FillIntArray--> Array = %ld\n\n", Array); } int main (int argc, char * argv[]) int Nos[4]; printf("FillIntArray--> Array = %ld\n\n", Array); FillIntArray(Nos, 5, 4); Array &2000 4 bytes &19921736 StartingNo &2004 5 NoElements &2008 4 Nos 20 bytes ?? Note That The int Array[] Parameter Does Not Have A Value  Such As [4] 85

void FillIntArray(int Array[], int StartingNo, int NoElements) { Arrays Are Always Passed As Pointers  Function Call Could Pass &Nos[0] void FillIntArray(int Array[], int StartingNo, int NoElements) { printf("FillIntArray--> Array = %ld\n\n", Array); } int main (int argc, char * argv[]) int Nos[4]; printf("FillIntArray--> Array = %ld\n\n", Array); FillIntArray(&Nos[0], 5, 4); Array &2000 4 bytes &15726676 StartingNo &2004 5 NoElements &2008 4 Nos 20 bytes ?? 86

Arrays Are Passed By Reference Arrays may have thousands/millions of elements; it would slow the program down if the compiler automatically copied all of the elements. We can manually make a copy of those elements in the small number of situations that this would be required. Sizeof cannot be used to determine capacity. The capacity/Max will have to be passed or included in some type of struct or class. 87

Arrays Are Always Passed As Pointers  Changing Nos[0] void FillIntArray(int Array[], int StartingNo, int NoElements) { Array[0] = StartingNo; } int main (int argc, char * argv[]) int Nos[4]; FillIntArray(Nos, 5, 4); Array &2000 4 bytes &15726676 StartingNo &2004 5 NoElements &2008 4 Nos 20 bytes ?? I Don't Want You To Take It On Faith That The Change In Nos[0] Has Occurred! 5 88

Arrays Are Always Passed As Pointers  Confirm The Change void DisplayIntArray(int Array[], int NoElements) { for (int Pos = NoElements - 1; Pos >= 0; Pos--) printf(" |------------|\n"); printf("%4ld | %10ld |\n", Pos, Array[Pos]); } printf(" |------------|\n\n"); int main (int argc, char * argv[]) int Nos[4]; FillIntArray(Nos, 5, 4); DisplayIntArray(Nos, 4); void FillIntArray(int Array[], int StartingNo, int NoElements) { Array[0] = StartingNo; } Not A Very Good Display! 89

Arrays Are Always Passed As Pointers  Finish My FillIntArray void FillIntArray(int Array[], int StartingNo, int NoElements) { for (int Pos = 0 - 1; Pos < NoElements; Pos++) Array[Pos] = StartingNo + Pos; } int main (int argc, char * argv[]) int Nos[4]; FillIntArray(Nos, 5, 4); DisplayIntArray(Nos, 4); void FillIntArray(int Array[], int StartingNo, int NoElements) Objective: Place StartingNo in Element 0  StartingNo + 1 in Element 1  StartingNo + 2 in Element 2  Continue Until All Elements Have Been Filled 90

Character Strings Are Passed By Reference Character Strings are Arrays Of Character Character Strings Are Passed By Reference void ToLowerCase (char String[]) { long int Pos; for (Pos = 0; Pos <= strlen(String); Pos ++) if ((String[Pos] >= 'A') && (String[Pos] <= 'Z')) String[Pos] = String[Pos] + 32; } 91

c The C Program 92

There May Be A Number Of Includes As You Add The A Complete Program - 1 //--------------------------- Includes --------------------------- # include <iostream> # include <fstream> # include <iomanip> # include <stdio.h> # include <string.h> # include <stdlib.h> using namespace std; There May Be A Number Of Includes As You Add The Libraries Necessary For You Program 93

Some Programs Have No Defines A Complete Program - 2 //--------------------------- Includes --------------------------- # include <iostream> # include <fstream> # include <iomanip> # include <stdio.h> # include <string.h> # include <stdlib.h> using namespace std; //--------------------------- Defines --------------------------- # define PI 3.14 Some Programs Have No Defines 94

Classes Are A C++ Improvement Over Structs We Will Do Them Shortly A Complete Program - 3 //--------------------------- Includes --------------------------- # include <iostream> # include <fstream> # include <iomanip> # include <stdio.h> # include <string.h> # include <stdlib.h> using namespace std; //--------------------------- Defines --------------------------- # define PI 3.14 //---------------------- Structs & Classes ---------------------- struct Student { char FirstName[15], LastName[20]; long No; }; Classes Are A C++ Improvement Over Structs We Will Do Them Shortly 95

Add The Function Prototypes A Complete Program - 4 //--------------------------- Includes --------------------------- # include <iostream> # include <fstream> # include <iomanip> # include <stdio.h> # include <string.h> # include <stdlib.h> using namespace std; //--------------------------- Defines --------------------------- # define PI 3.14 //---------------------- Structs & Classes ---------------------- struct Student { char FirstName[15], LastName[20]; long No; }; //--------------------- Function Prototypes --------------------- void HitCarriageReturnToContinue(void); void Set(Student * ThisStudent, char NewFirst[], char NewLast[], long NewNo); void Display(Student ThisStudent); Add The Function Prototypes 96

A Complete Program - 5 //-------------- Main & The Other Functions --------------- int main (int argc, char * argv[]) { puts("---------------------- Start Of Main ---------------------\n"); Student Jane; Set(&Jane, "Jane", "Doe", 1234); Display(Jane); puts ("----------------------- End Of Main ----------------------\n"); HitCarriageReturnToContinue(); return (0); } 97

A Complete Program - 6 //-------------- Main & The Other Functions --------------- void Set(Student * ThisStudent, char NewFirst[], char NewLast[], long NewNo) { ThisStudent->No = NewNo; strcpy_s(ThisStudent->FirstName, NewFirst); strcpy_s(ThisStudent->LastName, NewLast); } void Display(Student ThisStudent) printf("No = %d\n", ThisStudent.No); printf("First = %s\n", ThisStudent.FirstName); printf("Second = %s\n\n", ThisStudent.LastName); 98