Subroutines and Parameter Passing ECE2893 Lecture 5 ECE2893 - - PowerPoint PPT Presentation

subroutines and parameter passing
SMART_READER_LITE
LIVE PREVIEW

Subroutines and Parameter Passing ECE2893 Lecture 5 ECE2893 - - PowerPoint PPT Presentation

Subroutines and Parameter Passing ECE2893 Lecture 5 ECE2893 Subroutines and Parameter Passing Spring 2011 1 / 10 C/C++ Subroutines (Functions) ECE2893 Subroutines and Parameter Passing Spring 2011 2 / 10 C/C++ Subroutines (Functions)


slide-1
SLIDE 1

Subroutines and Parameter Passing

ECE2893 Lecture 5

ECE2893 Subroutines and Parameter Passing Spring 2011 1 / 10

slide-2
SLIDE 2

C/C++ Subroutines (Functions)

ECE2893 Subroutines and Parameter Passing Spring 2011 2 / 10

slide-3
SLIDE 3

C/C++ Subroutines (Functions)

1

Like almost all programming languages, C and C++ allow and encourage the use of subroutines, which are often called functions in C/C++.

ECE2893 Subroutines and Parameter Passing Spring 2011 2 / 10

slide-4
SLIDE 4

C/C++ Subroutines (Functions)

1

Like almost all programming languages, C and C++ allow and encourage the use of subroutines, which are often called functions in C/C++.

2

Functions in C/C++ allow decomposition of a problem into smaller sub–problems that are easier to comprehend and debug.

ECE2893 Subroutines and Parameter Passing Spring 2011 2 / 10

slide-5
SLIDE 5

C/C++ Subroutines (Functions)

1

Like almost all programming languages, C and C++ allow and encourage the use of subroutines, which are often called functions in C/C++.

2

Functions in C/C++ allow decomposition of a problem into smaller sub–problems that are easier to comprehend and debug.

3

Functions in C/C++ (optionally) return a value. This is analagous to an algebraic function F(x, y) = x2 + 2y + 3, that computes a value given inputs (independent variables) x and y.

ECE2893 Subroutines and Parameter Passing Spring 2011 2 / 10

slide-6
SLIDE 6

C/C++ Subroutines (Functions)

1

Like almost all programming languages, C and C++ allow and encourage the use of subroutines, which are often called functions in C/C++.

2

Functions in C/C++ allow decomposition of a problem into smaller sub–problems that are easier to comprehend and debug.

3

Functions in C/C++ (optionally) return a value. This is analagous to an algebraic function F(x, y) = x2 + 2y + 3, that computes a value given inputs (independent variables) x and y.

4

Functions must be declared before they can be called. More on this later.

ECE2893 Subroutines and Parameter Passing Spring 2011 2 / 10

slide-7
SLIDE 7

C/C++ Subroutines (Functions)

1

Like almost all programming languages, C and C++ allow and encourage the use of subroutines, which are often called functions in C/C++.

2

Functions in C/C++ allow decomposition of a problem into smaller sub–problems that are easier to comprehend and debug.

3

Functions in C/C++ (optionally) return a value. This is analagous to an algebraic function F(x, y) = x2 + 2y + 3, that computes a value given inputs (independent variables) x and y.

4

Functions must be declared before they can be called. More on this later.

5

Functions obviously can call other functions. This happens frequently in most C/C++ programming.

ECE2893 Subroutines and Parameter Passing Spring 2011 2 / 10

slide-8
SLIDE 8

C/C++ Subroutines (Functions)

1

Like almost all programming languages, C and C++ allow and encourage the use of subroutines, which are often called functions in C/C++.

2

Functions in C/C++ allow decomposition of a problem into smaller sub–problems that are easier to comprehend and debug.

3

Functions in C/C++ (optionally) return a value. This is analagous to an algebraic function F(x, y) = x2 + 2y + 3, that computes a value given inputs (independent variables) x and y.

4

Functions must be declared before they can be called. More on this later.

5

Functions obviously can call other functions. This happens frequently in most C/C++ programming.

6

A function can be called in C/C++ simply by having a single statement with the function name and arguments. More on this later.

ECE2893 Subroutines and Parameter Passing Spring 2011 2 / 10

slide-9
SLIDE 9

C/C++ Subroutines (Functions)

1

Like almost all programming languages, C and C++ allow and encourage the use of subroutines, which are often called functions in C/C++.

2

Functions in C/C++ allow decomposition of a problem into smaller sub–problems that are easier to comprehend and debug.

3

Functions in C/C++ (optionally) return a value. This is analagous to an algebraic function F(x, y) = x2 + 2y + 3, that computes a value given inputs (independent variables) x and y.

4

Functions must be declared before they can be called. More on this later.

5

Functions obviously can call other functions. This happens frequently in most C/C++ programming.

6

A function can be called in C/C++ simply by having a single statement with the function name and arguments. More on this later.

7

A function can also be called as a term in an expression. More on this later also.

ECE2893 Subroutines and Parameter Passing Spring 2011 2 / 10

slide-10
SLIDE 10

Declaring Functions

ECE2893 Subroutines and Parameter Passing Spring 2011 3 / 10

slide-11
SLIDE 11

Declaring Functions

1

A function must be declared before it can be called. This is analogous to the need to declare a variable before it is referenced.

ECE2893 Subroutines and Parameter Passing Spring 2011 3 / 10

slide-12
SLIDE 12

Declaring Functions

1

A function must be declared before it can be called. This is analogous to the need to declare a variable before it is referenced.

2

Functions can be declared in one of two ways.

ECE2893 Subroutines and Parameter Passing Spring 2011 3 / 10

slide-13
SLIDE 13

Declaring Functions

1

A function must be declared before it can be called. This is analogous to the need to declare a variable before it is referenced.

2

Functions can be declared in one of two ways.

1

Use a function prototype, such as: int Average(int d[], int L); Notice we specfiy the return type (int in this case), the name of the function (Average in this case), the list of parameters to the function, followed by a semicolon. This tells the compiler that such a function exists and can be called, but the actual implementation of Average will be done elsewhere.

ECE2893 Subroutines and Parameter Passing Spring 2011 3 / 10

slide-14
SLIDE 14

Declaring Functions

1

A function must be declared before it can be called. This is analogous to the need to declare a variable before it is referenced.

2

Functions can be declared in one of two ways.

1

Use a function prototype, such as: int Average(int d[], int L); Notice we specfiy the return type (int in this case), the name of the function (Average in this case), the list of parameters to the function, followed by a semicolon. This tells the compiler that such a function exists and can be called, but the actual implementation of Average will be done elsewhere.

2

Provide a function implementation, such as: int Average(int d[], int L) { int i = 0; // Index variable // Remainder omitted for brevity }

ECE2893 Subroutines and Parameter Passing Spring 2011 3 / 10

slide-15
SLIDE 15

Declaring Functions

1

A function must be declared before it can be called. This is analogous to the need to declare a variable before it is referenced.

2

Functions can be declared in one of two ways.

1

Use a function prototype, such as: int Average(int d[], int L); Notice we specfiy the return type (int in this case), the name of the function (Average in this case), the list of parameters to the function, followed by a semicolon. This tells the compiler that such a function exists and can be called, but the actual implementation of Average will be done elsewhere.

2

Provide a function implementation, such as: int Average(int d[], int L) { int i = 0; // Index variable // Remainder omitted for brevity }

3

If function A calls function B, and B also calls A, then a prototype Must be used for one of the two.

ECE2893 Subroutines and Parameter Passing Spring 2011 3 / 10

slide-16
SLIDE 16

Declaring Functions

1

A function must be declared before it can be called. This is analogous to the need to declare a variable before it is referenced.

2

Functions can be declared in one of two ways.

1

Use a function prototype, such as: int Average(int d[], int L); Notice we specfiy the return type (int in this case), the name of the function (Average in this case), the list of parameters to the function, followed by a semicolon. This tells the compiler that such a function exists and can be called, but the actual implementation of Average will be done elsewhere.

2

Provide a function implementation, such as: int Average(int d[], int L) { int i = 0; // Index variable // Remainder omitted for brevity }

3

If function A calls function B, and B also calls A, then a prototype Must be used for one of the two.

4

A function can be declared with void return type, indicating the function in fact does not return a value.

ECE2893 Subroutines and Parameter Passing Spring 2011 3 / 10

slide-17
SLIDE 17

Calling Functions

ECE2893 Subroutines and Parameter Passing Spring 2011 4 / 10

slide-18
SLIDE 18

Calling Functions

1

A function can be called by simply using the function name in a program, such as: PrintLn("GCD(", m[i], "," ,n[i], ") = ", gcd); The PrintLn function has been declared and implemented in ece2893.h.

ECE2893 Subroutines and Parameter Passing Spring 2011 4 / 10

slide-19
SLIDE 19

Calling Functions

1

A function can be called by simply using the function name in a program, such as: PrintLn("GCD(", m[i], "," ,n[i], ") = ", gcd); The PrintLn function has been declared and implemented in ece2893.h.

2

A function that returns a value (a return type of anything except void) can be used anywhere a variable name or constant is expected, such as: PrintLn("GCD(", m[i], ",", n[i], " = ", Euclid(m[i], n[i])); Notice the call to the Euclid function as a parameter to the PrintLn function.

ECE2893 Subroutines and Parameter Passing Spring 2011 4 / 10

slide-20
SLIDE 20

Calling Functions

1

A function can be called by simply using the function name in a program, such as: PrintLn("GCD(", m[i], "," ,n[i], ") = ", gcd); The PrintLn function has been declared and implemented in ece2893.h.

2

A function that returns a value (a return type of anything except void) can be used anywhere a variable name or constant is expected, such as: PrintLn("GCD(", m[i], ",", n[i], " = ", Euclid(m[i], n[i])); Notice the call to the Euclid function as a parameter to the PrintLn function.

3

Another example of function calls in expressions is: double x = R * cos(theta); double y = R * sin(theta); In this example, both cos and sin are pre–defined functions that can be called anywhere a value is expected.

ECE2893 Subroutines and Parameter Passing Spring 2011 4 / 10

slide-21
SLIDE 21

Passing Parameters

ECE2893 Subroutines and Parameter Passing Spring 2011 5 / 10

slide-22
SLIDE 22

Passing Parameters

1

In C and C++, all parameters (with one exception discussed later) are passed by value.

ECE2893 Subroutines and Parameter Passing Spring 2011 5 / 10

slide-23
SLIDE 23

Passing Parameters

1

In C and C++, all parameters (with one exception discussed later) are passed by value.

2

This means that when calling a function, the current value of the arguments passed to the function are copied to the Stack.

ECE2893 Subroutines and Parameter Passing Spring 2011 5 / 10

slide-24
SLIDE 24

Passing Parameters

1

In C and C++, all parameters (with one exception discussed later) are passed by value.

2

This means that when calling a function, the current value of the arguments passed to the function are copied to the Stack.

3

A function is free to read or change the value of any parameter, but this does not affect the value in the caller’s variable.

ECE2893 Subroutines and Parameter Passing Spring 2011 5 / 10

slide-25
SLIDE 25

Passing Parameters

1

In C and C++, all parameters (with one exception discussed later) are passed by value.

2

This means that when calling a function, the current value of the arguments passed to the function are copied to the Stack.

3

A function is free to read or change the value of any parameter, but this does not affect the value in the caller’s variable.

4

An example is illustrated on the next slide.

ECE2893 Subroutines and Parameter Passing Spring 2011 5 / 10

slide-26
SLIDE 26

Pass by Value Example

/ / Pass by Value i l l u s t r a t i o n / / George F . Riley , Georgia Tech , ECE2893 int Func ( int x , int y ) { PrintLn ( " Hello from Func , x i s " , x , " y i s " , y ) ; x = 5; / / " Func " can change the value

  • f x and y

y = 10; PrintLn ( " Hello from Func , x i s " , x , " y i s " , y ) ; } int main ( ) { int x = 100; int y = 45; PrintLn ( " Hello from main , x i s " , x , " y i s " , y ) ; / / Call Func , passing the value

  • f x and y

Func ( x , y ) ; PrintLn ( " Hello from main , x i s " , x , " y i s " , y ) ; } The resulting output from this example would be: Hello from main, x is 100 y is 45 Hello from Func, x is 100 y is 45 Hello from Func, x is 5 y is 10 Hello from main, x is 100 y is 45

ECE2893 Subroutines and Parameter Passing Spring 2011 6 / 10

slide-27
SLIDE 27

Array Arguments Passed by Reference

ECE2893 Subroutines and Parameter Passing Spring 2011 7 / 10

slide-28
SLIDE 28

Array Arguments Passed by Reference

1

The exception to the pass by value rule is when the argument is an array.

ECE2893 Subroutines and Parameter Passing Spring 2011 7 / 10

slide-29
SLIDE 29

Array Arguments Passed by Reference

1

The exception to the pass by value rule is when the argument is an array.

2

In this case, the array argument is passed by reference.

ECE2893 Subroutines and Parameter Passing Spring 2011 7 / 10

slide-30
SLIDE 30

Array Arguments Passed by Reference

1

The exception to the pass by value rule is when the argument is an array.

2

In this case, the array argument is passed by reference.

3

This means that the function with an array argument can change the contents of the array (as in the pass by value case). However, in this case, the value of the array from the caller will be changed.

ECE2893 Subroutines and Parameter Passing Spring 2011 7 / 10

slide-31
SLIDE 31

Array Arguments Passed by Reference

1

The exception to the pass by value rule is when the argument is an array.

2

In this case, the array argument is passed by reference.

3

This means that the function with an array argument can change the contents of the array (as in the pass by value case). However, in this case, the value of the array from the caller will be changed.

4

This makes sense from an efficiency standpoint. If the array contains hundreds of thousands of elements, it would clearly be inefficient to copy the entire array contents to the stack on each call.

ECE2893 Subroutines and Parameter Passing Spring 2011 7 / 10

slide-32
SLIDE 32

Example Pass–by–Reference

/ / I l l u s t r a t e passing array arguments by reference / / George F . Riley , Georgia Tech , ECE2893 #include " ece2893 . h" void Sub1( int d [ ] , int n ) { / / Sub1 can change array d , and the c a l l e r " sees " the changes int i = 0; while ( i < n ) { d [ i ] = d [ i ] ∗ 10; / / Change each element in d i = i + 1; } } int main ( ) { const int sz = 5; / / Size

  • f

array int d [ sz ] = { 1 , 2 , 3 , 4 , 5 } ; int i = 0; while ( i < sz ) { PrintLn ( " Before Sub1 , d [ " , i , " ] i s " , d [ i ] ) ; i = i + 1; } Sub1(d , sz ) ; i = 0; while ( i < sz ) { PrintLn ( " After Sub1 , d [ " , i , " ] i s " , d [ i ] ) ; i = i + 1; } }

The resulting output from this program is: Before Sub1, d[0] is 1 Before Sub1, d[1] is 2 Before Sub1, d[2] is 3 Before Sub1, d[3] is 4 Before Sub1, d[4] is 5 After Sub1, d[0] is 10 After Sub1, d[1] is 20 After Sub1, d[2] is 30 After Sub1, d[3] is 40 After Sub1, d[4] is 50

ECE2893 Subroutines and Parameter Passing Spring 2011 8 / 10

slide-33
SLIDE 33

Stack Usage Example

1 void Sub2(int k) 2 { 3 k = 4; 4 } 5 6 void Sub1(int j) 7 { 8 int k1 = 5; 9 Sub2(k1); 10 } 11 12 void Sub0(int i,int j) 13 { 14 int d[4]; 15 int x; 16 char ch[]="Test"; 17 Sub1(i); 18 Sub2(j); 19 } 21 22 int main() 23 { 24 PC → Sub0(1, 2); 25 } Stack Memory SP →

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

slide-34
SLIDE 34

Stack Usage Example

1 void Sub2(int k) 2 { 3 k = 4; 4 } 5 6 void Sub1(int j) 7 { 8 int k1 = 5; 9 Sub2(k1); 10 } 11 12 void Sub0(int i,int j) 13 { 14 PC → int d[4]; 15 int x; 16 char ch[]="Test"; 17 Sub1(i); 18 Sub2(j); 19 } 21 22 int main() 23 { 24 Sub0(1, 2); 25 } Stack Memory SP → Addr of Line 25 Sub0::i = 1 Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

slide-35
SLIDE 35

Stack Usage Example

1 void Sub2(int k) 2 { 3 k = 4; 4 } 5 6 void Sub1(int j) 7 { 8 int k1 = 5; 9 Sub2(k1); 10 } 11 12 void Sub0(int i,int j) 13 { 14 int d[4]; 15 PC → int x; 16 char ch[]="Test"; 17 Sub1(i); 18 Sub2(j); 19 } 21 22 int main() 23 { 24 Sub0(1, 2); 25 } Stack Memory SP → Sub0::d[0] = ? Sub0::d[1] = ? Sub0::d[2] = ? Sub0::d[3] = ? Addr of Line 25 Sub0::i = 1 Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

slide-36
SLIDE 36

Stack Usage Example

1 void Sub2(int k) 2 { 3 k = 4; 4 } 5 6 void Sub1(int j) 7 { 8 int k1 = 5; 9 Sub2(k1); 10 } 11 12 void Sub0(int i,int j) 13 { 14 int d[4]; 15 int x; 16 PC → char ch[]="Test"; 17 Sub1(i); 18 Sub2(j); 19 } 21 22 int main() 23 { 24 Sub0(1, 2); 25 } Stack Memory SP → Sub0::x = ? Sub0::d[0] = ? Sub0::d[1] = ? Sub0::d[2] = ? Sub0::d[3] = ? Addr of Line 25 Sub0::i = 1 Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

slide-37
SLIDE 37

Stack Usage Example

1 void Sub2(int k) 2 { 3 k = 4; 4 } 5 6 void Sub1(int j) 7 { 8 int k1 = 5; 9 Sub2(k1); 10 } 11 12 void Sub0(int i,int j) 13 { 14 int d[4]; 15 int x; 16 char ch[]="Test"; 17 PC → Sub1(i); 18 Sub2(j); 19 } 21 22 int main() 23 { 24 Sub0(1, 2); 25 } Stack Memory SP → Sub0::ch[0] = ’T’ Sub0::ch[1] = ’e’ Sub0::ch[2] = ’s’ Sub0::ch[3] = ’t’ Sub0::ch[4] = 0 Sub0::x = ? Sub0::d[0] = ? Sub0::d[1] = ? Sub0::d[2] = ? Sub0::d[3] = ? Addr of Line 25 Sub0::i = 1 Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

slide-38
SLIDE 38

Stack Usage Example

1 void Sub2(int k) 2 { 3 k = 4; 4 } 5 6 void Sub1(int j) 7 { 8 PC → int k1 = 5; 9 Sub2(k1); 10 } 11 12 void Sub0(int i,int j) 13 { 14 int d[4]; 15 int x; 16 char ch[]="Test"; 17 Sub1(i); 18 Sub2(j); 19 } 21 22 int main() 23 { 24 Sub0(1, 2); 25 } Stack Memory SP → Addr of Line 18 Sub1::j = 1 Sub0::ch[0] = ’T’ Sub0::ch[1] = ’e’ Sub0::ch[2] = ’s’ Sub0::ch[3] = ’t’ Sub0::ch[4] = 0 Sub0::x = ? Sub0::d[0] = ? Sub0::d[1] = ? Sub0::d[2] = ? Sub0::d[3] = ? Addr of Line 25 Sub0::i = 1 Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

slide-39
SLIDE 39

Stack Usage Example

1 void Sub2(int k) 2 { 3 k = 4; 4 } 5 6 void Sub1(int j) 7 { 8 int k1 = 5; 9 PC → Sub2(k1); 10 } 11 12 void Sub0(int i,int j) 13 { 14 int d[4]; 15 int x; 16 char ch[]="Test"; 17 Sub1(i); 18 Sub2(j); 19 } 21 22 int main() 23 { 24 Sub0(1, 2); 25 } Stack Memory SP → Sub1::k1 = 5 Addr of Line 18 Sub1::j = 1 Sub0::ch[0] = ’T’ Sub0::ch[1] = ’e’ Sub0::ch[2] = ’s’ Sub0::ch[3] = ’t’ Sub0::ch[4] = 0 Sub0::x = ? Sub0::d[0] = ? Sub0::d[1] = ? Sub0::d[2] = ? Sub0::d[3] = ? Addr of Line 25 Sub0::i = 1 Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

slide-40
SLIDE 40

Stack Usage Example

1 void Sub2(int k) 2 { 3 PC → k = 4; 4 } 5 6 void Sub1(int j) 7 { 8 int k1 = 5; 9 Sub2(k1); 10 } 11 12 void Sub0(int i,int j) 13 { 14 int d[4]; 15 int x; 16 char ch[]="Test"; 17 Sub1(i); 18 Sub2(j); 19 } 21 22 int main() 23 { 24 Sub0(1, 2); 25 } Stack Memory SP → Addr of Line 10 Sub2::k = 5 Sub1::k1 = 5 Addr of Line 18 Sub1::j = 1 Sub0::ch[0] = ’T’ Sub0::ch[1] = ’e’ Sub0::ch[2] = ’s’ Sub0::ch[3] = ’t’ Sub0::ch[4] = 0 Sub0::x = ? Sub0::d[0] = ? Sub0::d[1] = ? Sub0::d[2] = ? Sub0::d[3] = ? Addr of Line 25 Sub0::i = 1 Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

slide-41
SLIDE 41

Stack Usage Example

1 void Sub2(int k) 2 { 3 k = 4; 4 PC → } 5 6 void Sub1(int j) 7 { 8 int k1 = 5; 9 Sub2(k1); 10 } 11 12 void Sub0(int i,int j) 13 { 14 int d[4]; 15 int x; 16 char ch[]="Test"; 17 Sub1(i); 18 Sub2(j); 19 } 21 22 int main() 23 { 24 Sub0(1, 2); 25 } Stack Memory SP → Addr of Line 10 Sub2::k = 4 Sub1::k1 = 5 Addr of Line 18 Sub1::j = 1 Sub0::ch[0] = ’T’ Sub0::ch[1] = ’e’ Sub0::ch[2] = ’s’ Sub0::ch[3] = ’t’ Sub0::ch[4] = 0 Sub0::x = ? Sub0::d[0] = ? Sub0::d[1] = ? Sub0::d[2] = ? Sub0::d[3] = ? Addr of Line 25 Sub0::i = 1 Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

slide-42
SLIDE 42

Stack Usage Example

1 void Sub2(int k) 2 { 3 k = 4; 4 } 5 6 void Sub1(int j) 7 { 8 int k1 = 5; 9 Sub2(k1); 10 PC → } 11 12 void Sub0(int i,int j) 13 { 14 int d[4]; 15 int x; 16 char ch[]="Test"; 17 Sub1(i); 18 Sub2(j); 19 } 21 22 int main() 23 { 24 Sub0(1, 2); 25 } Stack Memory SP → Sub1::k1 = 5 Addr of Line 18 Sub1::j = 1 Sub0::ch[0] = ’T’ Sub0::ch[1] = ’e’ Sub0::ch[2] = ’s’ Sub0::ch[3] = ’t’ Sub0::ch[4] = 0 Sub0::x = ? Sub0::d[0] = ? Sub0::d[1] = ? Sub0::d[2] = ? Sub0::d[3] = ? Addr of Line 25 Sub0::i = 1 Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

slide-43
SLIDE 43

Stack Usage Example

1 void Sub2(int k) 2 { 3 k = 4; 4 } 5 6 void Sub1(int j) 7 { 8 int k1 = 5; 9 Sub2(k1); 10 } 11 12 void Sub0(int i,int j) 13 { 14 int d[4]; 15 int x; 16 char ch[]="Test"; 17 Sub1(i); 18 PC → Sub2(j); 19 } 21 22 int main() 23 { 24 Sub0(1, 2); 25 } Stack Memory SP → Sub0::ch[0] = ’T’ Sub0::ch[1] = ’e’ Sub0::ch[2] = ’s’ Sub0::ch[3] = ’t’ Sub0::ch[4] = 0 Sub0::x = ? Sub0::d[0] = ? Sub0::d[1] = ? Sub0::d[2] = ? Sub0::d[3] = ? Addr of Line 25 Sub0::i = 1 Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

slide-44
SLIDE 44

Stack Usage Example

1 void Sub2(int k) 2 { 3 PC → k = 4; 4 } 5 6 void Sub1(int j) 7 { 8 int k1 = 5; 9 Sub2(k1); 10 } 11 12 void Sub0(int i,int j) 13 { 14 int d[4]; 15 int x; 16 char ch[]="Test"; 17 Sub1(i); 18 Sub2(j); 19 } 21 22 int main() 23 { 24 Sub0(1, 2); 25 } Stack Memory SP → Addr of Line 19 Sub2::k = 2 Sub0::ch[0] = ’T’ Sub0::ch[1] = ’e’ Sub0::ch[2] = ’s’ Sub0::ch[3] = ’t’ Sub0::ch[4] = 0 Sub0::x = ? Sub0::d[0] = ? Sub0::d[1] = ? Sub0::d[2] = ? Sub0::d[3] = ? Addr of Line 25 Sub0::i = 1 Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

slide-45
SLIDE 45

Stack Usage Example

1 void Sub2(int k) 2 { 3 k = 4; 4 PC → } 5 6 void Sub1(int j) 7 { 8 int k1 = 5; 9 Sub2(k1); 10 } 11 12 void Sub0(int i,int j) 13 { 14 int d[4]; 15 int x; 16 char ch[]="Test"; 17 Sub1(i); 18 Sub2(j); 19 } 21 22 int main() 23 { 24 Sub0(1, 2); 25 } Stack Memory SP → Addr of Line 19 Sub2::k = 4 Sub0::ch[0] = ’T’ Sub0::ch[1] = ’e’ Sub0::ch[2] = ’s’ Sub0::ch[3] = ’t’ Sub0::ch[4] = 0 Sub0::x = ? Sub0::d[0] = ? Sub0::d[1] = ? Sub0::d[2] = ? Sub0::d[3] = ? Addr of Line 25 Sub0::i = 1 Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

slide-46
SLIDE 46

Stack Usage Example

1 void Sub2(int k) 2 { 3 k = 4; 4 } 5 6 void Sub1(int j) 7 { 8 int k1 = 5; 9 Sub2(k1); 10 } 11 12 void Sub0(int i,int j) 13 { 14 int d[4]; 15 int x; 16 char ch[]="Test"; 17 Sub1(i); 18 Sub2(j); 19 PC → } 21 22 int main() 23 { 24 Sub0(1, 2); 25 } Stack Memory SP → Sub0::ch[0] = ’T’ Sub0::ch[1] = ’e’ Sub0::ch[2] = ’s’ Sub0::ch[3] = ’t’ Sub0::ch[4] = 0 Sub0::x = ? Sub0::d[0] = ? Sub0::d[1] = ? Sub0::d[2] = ? Sub0::d[3] = ? Addr of Line 25 Sub0::i = 1 Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

slide-47
SLIDE 47

Stack Usage Example

1 void Sub2(int k) 2 { 3 k = 4; 4 } 5 6 void Sub1(int j) 7 { 8 int k1 = 5; 9 Sub2(k1); 10 } 11 12 void Sub0(int i,int j) 13 { 14 int d[4]; 15 int x; 16 char ch[]="Test"; 17 Sub1(i); 18 Sub2(j); 19 } 21 22 int main() 23 { 24 Sub0(1, 2); 25 PC → } Stack Memory SP →

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

slide-48
SLIDE 48

Stack Usage Example (more realistic)

1 void Sub2(int k) 2 { 3 k = 4; 4 } 5 6 void Sub1(int j) 7 { 8 int k1 = 5; 9 Sub2(k1); 10 } 11 12 void Sub0(int i,int j) 13 { 14 int d[4]; 15 int x; 16 char ch[]="Test"; 17 Sub1(i); 18 Sub2(j); 19 } 21 22 int main() 23 { 24 PC → Sub0(1, 2); 25 } Stack Memory ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? SP → ?

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10

slide-49
SLIDE 49

Stack Usage Example (more realistic)

1 void Sub2(int k) 2 { 3 k = 4; 4 } 5 6 void Sub1(int j) 7 { 8 int k1 = 5; 9 Sub2(k1); 10 } 11 12 void Sub0(int i,int j) 13 { 14 PC → int d[4]; 15 int x; 16 char ch[]="Test"; 17 Sub1(i); 18 Sub2(j); 19 } 21 22 int main() 23 { 24 Sub0(1, 2); 25 } Stack Memory ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? SP → ? Addr of Line 25 Sub0::i = 1 Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10

slide-50
SLIDE 50

Stack Usage Example (more realistic)

1 void Sub2(int k) 2 { 3 k = 4; 4 } 5 6 void Sub1(int j) 7 { 8 int k1 = 5; 9 Sub2(k1); 10 } 11 12 void Sub0(int i,int j) 13 { 14 int d[4]; 15 PC → int x; 16 char ch[]="Test"; 17 Sub1(i); 18 Sub2(j); 19 } 21 22 int main() 23 { 24 Sub0(1, 2); 25 } Stack Memory ? ? ? ? ? ? ? ? ? ? ? ? ? SP → ? Sub0::d[0] = ? Sub0::d[1] = ? Sub0::d[2] = ? Sub0::d[3] = ? Addr of Line 25 Sub0::i = 1 Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10

slide-51
SLIDE 51

Stack Usage Example (more realistic)

1 void Sub2(int k) 2 { 3 k = 4; 4 } 5 6 void Sub1(int j) 7 { 8 int k1 = 5; 9 Sub2(k1); 10 } 11 12 void Sub0(int i,int j) 13 { 14 int d[4]; 15 int x; 16 PC → char ch[]="Test"; 17 Sub1(i); 18 Sub2(j); 19 } 21 22 int main() 23 { 24 Sub0(1, 2); 25 } Stack Memory ? ? ? ? ? ? ? ? ? ? ? ? SP → ? Sub0::x = ? Sub0::d[0] = ? Sub0::d[1] = ? Sub0::d[2] = ? Sub0::d[3] = ? Addr of Line 25 Sub0::i = 1 Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10

slide-52
SLIDE 52

Stack Usage Example (more realistic)

1 void Sub2(int k) 2 { 3 k = 4; 4 } 5 6 void Sub1(int j) 7 { 8 int k1 = 5; 9 Sub2(k1); 10 } 11 12 void Sub0(int i,int j) 13 { 14 int d[4]; 15 int x; 16 char ch[]="Test"; 17 PC → Sub1(i); 18 Sub2(j); 19 } 21 22 int main() 23 { 24 Sub0(1, 2); 25 } Stack Memory ? ? ? ? ? ? ? SP → ? Sub0::ch[0] = ’T’ Sub0::ch[1] = ’e’ Sub0::ch[2] = ’s’ Sub0::ch[3] = ’t’ Sub0::ch[4] = 0 Sub0::x = ? Sub0::d[0] = ? Sub0::d[1] = ? Sub0::d[2] = ? Sub0::d[3] = ? Addr of Line 25 Sub0::i = 1 Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10

slide-53
SLIDE 53

Stack Usage Example (more realistic)

1 void Sub2(int k) 2 { 3 k = 4; 4 } 5 6 void Sub1(int j) 7 { 8 PC → int k1 = 5; 9 Sub2(k1); 10 } 11 12 void Sub0(int i,int j) 13 { 14 int d[4]; 15 int x; 16 char ch[]="Test"; 17 Sub1(i); 18 Sub2(j); 19 } 21 22 int main() 23 { 24 Sub0(1, 2); 25 } Stack Memory ? ? ? ? ? SP → ? Addr of Line 18 Sub1::j = 1 Sub0::ch[0] = ’T’ Sub0::ch[1] = ’e’ Sub0::ch[2] = ’s’ Sub0::ch[3] = ’t’ Sub0::ch[4] = 0 Sub0::x = ? Sub0::d[0] = ? Sub0::d[1] = ? Sub0::d[2] = ? Sub0::d[3] = ? Addr of Line 25 Sub0::i = 1 Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10

slide-54
SLIDE 54

Stack Usage Example (more realistic)

1 void Sub2(int k) 2 { 3 k = 4; 4 } 5 6 void Sub1(int j) 7 { 8 int k1 = 5; 9 PC → Sub2(k1); 10 } 11 12 void Sub0(int i,int j) 13 { 14 int d[4]; 15 int x; 16 char ch[]="Test"; 17 Sub1(i); 18 Sub2(j); 19 } 21 22 int main() 23 { 24 Sub0(1, 2); 25 } Stack Memory ? ? ? ? SP → ? Sub1::k1 = 5 Addr of Line 18 Sub1::j = 1 Sub0::ch[0] = ’T’ Sub0::ch[1] = ’e’ Sub0::ch[2] = ’s’ Sub0::ch[3] = ’t’ Sub0::ch[4] = 0 Sub0::x = ? Sub0::d[0] = ? Sub0::d[1] = ? Sub0::d[2] = ? Sub0::d[3] = ? Addr of Line 25 Sub0::i = 1 Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10

slide-55
SLIDE 55

Stack Usage Example (more realistic)

1 void Sub2(int k) 2 { 3 PC → k = 4; 4 } 5 6 void Sub1(int j) 7 { 8 int k1 = 5; 9 Sub2(k1); 10 } 11 12 void Sub0(int i,int j) 13 { 14 int d[4]; 15 int x; 16 char ch[]="Test"; 17 Sub1(i); 18 Sub2(j); 19 } 21 22 int main() 23 { 24 Sub0(1, 2); 25 } Stack Memory ? ? SP → ? Addr of Line 10 Sub2::k = 5 Sub1::k1 = 5 Addr of Line 18 Sub1::j = 1 Sub0::ch[0] = ’T’ Sub0::ch[1] = ’e’ Sub0::ch[2] = ’s’ Sub0::ch[3] = ’t’ Sub0::ch[4] = 0 Sub0::x = ? Sub0::d[0] = ? Sub0::d[1] = ? Sub0::d[2] = ? Sub0::d[3] = ? Addr of Line 25 Sub0::i = 1 Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10

slide-56
SLIDE 56

Stack Usage Example (more realistic)

1 void Sub2(int k) 2 { 3 k = 4; 4 PC → } 5 6 void Sub1(int j) 7 { 8 int k1 = 5; 9 Sub2(k1); 10 } 11 12 void Sub0(int i,int j) 13 { 14 int d[4]; 15 int x; 16 char ch[]="Test"; 17 Sub1(i); 18 Sub2(j); 19 } 21 22 int main() 23 { 24 Sub0(1, 2); 25 } Stack Memory ? ? SP → ? Addr of Line 10 Sub2::k = 4 Sub1::k1 = 5 Addr of Line 18 Sub1::j = 1 Sub0::ch[0] = ’T’ Sub0::ch[1] = ’e’ Sub0::ch[2] = ’s’ Sub0::ch[3] = ’t’ Sub0::ch[4] = 0 Sub0::x = ? Sub0::d[0] = ? Sub0::d[1] = ? Sub0::d[2] = ? Sub0::d[3] = ? Addr of Line 25 Sub0::i = 1 Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10

slide-57
SLIDE 57

Stack Usage Example (more realistic)

1 void Sub2(int k) 2 { 3 k = 4; 4 } 5 6 void Sub1(int j) 7 { 8 int k1 = 5; 9 Sub2(k1); 10 PC → } 11 12 void Sub0(int i,int j) 13 { 14 int d[4]; 15 int x; 16 char ch[]="Test"; 17 Sub1(i); 18 Sub2(j); 19 } 21 22 int main() 23 { 24 Sub0(1, 2); 25 } Stack Memory ? ? ? Addr of Line 10 SP → Sub2::k = 4 Sub1::k1 = 5 Addr of Line 18 Sub1::j = 1 Sub0::ch[0] = ’T’ Sub0::ch[1] = ’e’ Sub0::ch[2] = ’s’ Sub0::ch[3] = ’t’ Sub0::ch[4] = 0 Sub0::x = ? Sub0::d[0] = ? Sub0::d[1] = ? Sub0::d[2] = ? Sub0::d[3] = ? Addr of Line 25 Sub0::i = 1 Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10

slide-58
SLIDE 58

Stack Usage Example (more realistic)

1 void Sub2(int k) 2 { 3 k = 4; 4 } 5 6 void Sub1(int j) 7 { 8 int k1 = 5; 9 Sub2(k1); 10 } 11 12 void Sub0(int i,int j) 13 { 14 int d[4]; 15 int x; 16 char ch[]="Test"; 17 Sub1(i); 18 PC → Sub2(j); 19 } 21 22 int main() 23 { 24 Sub0(1, 2); 25 } Stack Memory ? ? ? Addr of Line 10 Sub2::k = 4 Sub1::k1 = 5 Addr of Line 18 SP → Sub1::j = 1 Sub0::ch[0] = ’T’ Sub0::ch[1] = ’e’ Sub0::ch[2] = ’s’ Sub0::ch[3] = ’t’ Sub0::ch[4] = 0 Sub0::x = ? Sub0::d[0] = ? Sub0::d[1] = ? Sub0::d[2] = ? Sub0::d[3] = ? Addr of Line 25 Sub0::i = 1 Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10

slide-59
SLIDE 59

Stack Usage Example (more realistic)

1 void Sub2(int k) 2 { 3 PC → k = 4; 4 } 5 6 void Sub1(int j) 7 { 8 int k1 = 5; 9 Sub2(k1); 10 } 11 12 void Sub0(int i,int j) 13 { 14 int d[4]; 15 int x; 16 char ch[]="Test"; 17 Sub1(i); 18 Sub2(j); 19 } 21 22 int main() 23 { 24 Sub0(1, 2); 25 } Stack Memory ? ? ? Addr of Line 10 Sub2::k = 4 SP → Sub1::k1 = 5 Addr of Line 19 Sub2::k = 2 Sub0::ch[0] = ’T’ Sub0::ch[1] = ’e’ Sub0::ch[2] = ’s’ Sub0::ch[3] = ’t’ Sub0::ch[4] = 0 Sub0::x = ? Sub0::d[0] = ? Sub0::d[1] = ? Sub0::d[2] = ? Sub0::d[3] = ? Addr of Line 25 Sub0::i = 1 Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10

slide-60
SLIDE 60

Stack Usage Example (more realistic)

1 void Sub2(int k) 2 { 3 k = 4; 4 PC → } 5 6 void Sub1(int j) 7 { 8 int k1 = 5; 9 Sub2(k1); 10 } 11 12 void Sub0(int i,int j) 13 { 14 int d[4]; 15 int x; 16 char ch[]="Test"; 17 Sub1(i); 18 Sub2(j); 19 } 21 22 int main() 23 { 24 Sub0(1, 2); 25 } Stack Memory ? ? ? Addr of Line 10 Sub2::k = 4 SP → Sub1::k1 = 5 Addr of Line 19 Sub2::k = 4 Sub0::ch[0] = ’T’ Sub0::ch[1] = ’e’ Sub0::ch[2] = ’s’ Sub0::ch[3] = ’t’ Sub0::ch[4] = 0 Sub0::x = ? Sub0::d[0] = ? Sub0::d[1] = ? Sub0::d[2] = ? Sub0::d[3] = ? Addr of Line 25 Sub0::i = 1 Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10

slide-61
SLIDE 61

Stack Usage Example (more realistic)

1 void Sub2(int k) 2 { 3 k = 4; 4 } 5 6 void Sub1(int j) 7 { 8 int k1 = 5; 9 Sub2(k1); 10 } 11 12 void Sub0(int i,int j) 13 { 14 int d[4]; 15 int x; 16 char ch[]="Test"; 17 Sub1(i); 18 Sub2(j); 19 PC → } 21 22 int main() 23 { 24 Sub0(1, 2); 25 } Stack Memory ? ? ? Addr of Line 10 Sub2::k = 4 Sub1::k1 = 5 Addr of Line 19 SP → Sub2::k = 4 Sub0::ch[0] = ’T’ Sub0::ch[1] = ’e’ Sub0::ch[2] = ’s’ Sub0::ch[3] = ’t’ Sub0::ch[4] = 0 Sub0::x = ? Sub0::d[0] = ? Sub0::d[1] = ? Sub0::d[2] = ? Sub0::d[3] = ? Addr of Line 25 Sub0::i = 1 Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10

slide-62
SLIDE 62

Stack Usage Example (more realistic)

1 void Sub2(int k) 2 { 3 k = 4; 4 } 5 6 void Sub1(int j) 7 { 8 int k1 = 5; 9 Sub2(k1); 10 } 11 12 void Sub0(int i,int j) 13 { 14 int d[4]; 15 int x; 16 char ch[]="Test"; 17 Sub1(i); 18 Sub2(j); 19 } 21 22 int main() 23 { 24 Sub0(1, 2); 25 PC → } Stack Memory ? ? ? Addr of Line 10 Sub2::k = 4 Sub1::k1 = 5 Addr of Line 19 Sub2::k = 4 Sub0::ch[0] = ’T’ Sub0::ch[1] = ’e’ Sub0::ch[2] = ’s’ Sub0::ch[3] = ’t’ Sub0::ch[4] = 0 Sub0::x = ? Sub0::d[0] = ? Sub0::d[1] = ? Sub0::d[2] = ? Sub0::d[3] = ? Addr of Line 25 Sub0::i = 1 SP → Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10