Download presentation
Presentation is loading. Please wait.
1
Parameter Passing & Array Examples – Part 1
Chalermsak Chatdokmaiprai Computer and Programming Department of Computer Engineering Kasetsart University Cliparts are taken from
2
Example 1 Program's Stack Space output Main()'s Stack i 10 8 j 50 14
class MyClass { public static void Main () { int i = 10; int j = Foo( i, i+5 ); Console.WriteLine("{0},{1}", i, j); Console.WriteLine("{0},{1}", Bar(6, out i), i); } static int Foo (int x, int y) { int i; x = Bar( x, out i ) + 1; return x + y + i; static int Bar (int j, out int k) { k = j + 2; return j + k; i 10 8 j 50 50 10 15 14 50 Bar()'s Stack Foo()'s Stack j x 10 6 23 14 6 8 k y 15 i 12 22 10 23 1 22 Bar()'s Stack j 10 23 50 15 12 k 10 6 12 8 2 output 10 22 12 10,50 14,8 6 14 8
3
Example 2 Program's Stack Space output
using C=System.Console; class MyClass { static void Main () { int i = 22; C.WriteLine("At A: {0}", i); AlterInt(i); C.WriteLine("At B: {0}", i); } static void AlterInt (int i) { C.WriteLine("At C: {0}", i); i = 55; C.WriteLine("At D: {0}", i); Main()'s Stack i 22 AlterInt()'s Stack i 55 22 output At A: 22 At C: 22 At D: 55 At B: 22
4
Example 3 using C=System.Console; class MyClass { static void Main () { int[] i = new int[2] {22, 99}; C.WriteLine("At A: {0}", i[0]); AlterInt(i); C.WriteLine("At B: {0}", i[0]); } static void AlterInt (int[] i) { C.WriteLine("At C: {0}", i[0]); i[0] = 55; C.WriteLine("At D: {0}", i[0]); output At A: 22 At C: 22 At D: 55 At B: 55
5
Array Example Program's Stack Program's Heap Main()'s Stack [0] [1] using C=System.Console; class MyClass { static void Main () { double[] a; //uninitialized a = null; //initialized to null a = new double[2];//allocated double[] b = new double[] {1.1, 2.2, 3.3}; a = b; int m = (int) a[1]; b[(m*10)%3] = b[m-1] + 10; C.WriteLine("{0}, {1}, {2}", a[0],a[1],a[2]); } a garbage b [0] 1.1 [1] 2.2 [2] 3.3 m 2 12.2 output 2 b[2] 12.2 1.1, 2.2, 12.2 2.2 10
6
Example 3 dissected output Program's Stack Program's Heap
using C=System.Console; class MyClass { static void Main () { int[] i = new int[2] {22, 99} ; C.WriteLine("At A: {0}", i[0]); AlterInt(i); C.WriteLine("At B: {0}", i[0]); } static void AlterInt (int[] i) { C.WriteLine("At C: {0}", i[0]); i[0] = 55; C.WriteLine("At D: {0}", i[0]); Program's Stack Program's Heap Main()'s Stack i = AlterInt()'s Stack [0] [1] 55 22 i 99 output At A: 22 At C: 22 At D: 55 At B: 55
7
Example 3 plus output Program's Stack Program's Heap
using C=System.Console; class MyClass { static void Main () { int[] i = new int[2] {22, 99} ; C.WriteLine("At A: {0}", i[0]); AlterInt(i); C.WriteLine("At B: {0}", i[0]); } static void AlterInt (int[] i) { C.WriteLine("At C: {0}", i[0]); i[0] = 55; C.WriteLine("At D: {0}", i[0]); i = new int[3] {66,77,88}; C.WriteLine("At E: {0}", i[0]); Program's Stack Program's Heap Main()'s Stack i = AlterInt()'s Stack [0] [1] 55 22 i 99 [0] 66 [1] 77 [2] 88 output i= At A: 22 At C: 22 At D: 55 At E: 66 At B: 55
8
Example 4 a output Stack Heap i 55 66 k 99 n 10 3 20 x 30 i 2 1 3
Main() using C=System.Console; class MyClass { static void Main () { int[] i = new int[2] {55,66}; i[0], i[1]); int[] k = i; k[1] = 99; i[0], i[1]); i = GetData(3); i[0], i[1], i[2]); k[0], k[1]); } static int[] GetData (int n) { int[] x = new int[n]; for (int i = 0; i < n; i++) x[i] = (i+1)*10; x[0], x[1], x[2]); return x; i [0] 55 [1] 66 k 99 GetData() [0] [1] [2] n 10 3 20 x 30 i 2 1 3 output @A:i=55,66 @B:i=55,99 @E:x=10,20,30 @C:i=10,20,30 @D:k=55,99
9
Example 4 b Example 4 a output Stack Heap i 55 66 k 99 n 10 3 20 x 30
Main() using C=System.Console; class MyClass { static void Main () { int[] i = new int[2] {55,66}; i[0], i[1]); int[] k = i; k[1] = 99; i[0], i[1]); GetData(3, out i); i[0], i[1], i[2]); k[0], k[1]); } static void GetData (int n, out int[] x) { x = new int[n]; for (int i = 0; i < n; i++) x[i] = (i+1)*10; x[0], x[1], x[2]); i [0] 55 [1] 66 k 99 GetData() [0] [1] [2] n 10 3 20 x 30 Example 4 a i 3 1 2 i = GetData(3); } static int[] GetData (int n) { int[] x = new int[n]; for (int i = 0; i < n; i++) x[i] = (i+1)*10; return x; output @A:i=55,66 @B:i=55,99 @E:x=10,20,30 @C:i=10,20,30 @D:k=55,99
10
The End
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.