# Command line Argument, recursive and static constructor

Page 1 of 1

## 8 Replies - 1986 Views - Last Post: 18 November 2009 - 08:04 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=139781&amp;s=ad96227f303b45bad703a13941307fc8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 kenryuakuma

• D.I.C Regular

Reputation: 4
• Posts: 408
• Joined: 14-December 08

# Command line Argument, recursive and static constructor

Posted 17 November 2009 - 03:58 PM

1) Well...I understand and know how a method works within C#, but when it comes to recursive method, I am a bit confused...I hope you guys could explain to me step by step how the codes work.

```using System;

class Factorial {
// This is a recursive function.
public int factR(int n) {
int result;

if(n==1) return 1;
result = factR(n-1) * n;
return result;
}
}

public class Recursion {
public static void Main() {
Factorial f = new Factorial();

Console.WriteLine("Factorials using recursive method.");
Console.WriteLine("Factorial of 3 is " + f.factR(3));
Console.WriteLine("Factorial of 4 is " + f.factR(4));
Console.WriteLine("Factorial of 5 is " + f.factR(5));
Console.WriteLine();
```

2) What is command line argument used within main in C#? What does it look like? I am a visual learner, so it would be great if there are pictures or something for illustration. here is the code:

```class TestClass
{
static void Main(string[] args)
{
// Display the number of command line arguments:
System.Console.WriteLine(args.Length);
}
}
```

3) So far I am on the static constructor and method, but I already find it confusing to understand. As far as I know, you cannot use static members, methods and so on with instance method and variables. YOU can only use them separately or if you want to use them together, you must create an object. So for instance,

```public static void staticMeth (MyClass ob) {
ob.NonStaticMeth();
}  // This one is ok since we already have an object named ob and NonStaticMeth is an instance method.
```

However, I don't really understand this code here because public CountInst is a instance constructor and not a static constructor but how come that works? Isn't that the static and the instance have to work independently and separately and they cannot be used together without the object being qualified?

```// Use a static field to count instances.

using System;

class CountInst {
static int count = 0;

// increment count when object is created
public CountInst() {
count++;
}

// decrement count when object is destroyed
~CountInst() {
count--;
}

public static int getcount() {
return count;
}
}

public class CountDemo {
public static void Main() {
CountInst ob;

for(int i=0; i < 10; i++) {
ob = new CountInst();
Console.WriteLine("Current count: " +
CountInst.getcount());
}

}
}
```

Is This A Good Question/Topic? 0

## Replies To: Command line Argument, recursive and static constructor

### #2 Momerath

• D.I.C Lover

Reputation: 1020
• Posts: 2,463
• Joined: 04-October 09

## Re: Command line Argument, recursive and static constructor

Posted 17 November 2009 - 04:13 PM

1) Recursion is when a method calls itself to perform a task. In your case, let's see what happens when you compute factorial 3:

1. Call FactR(3).
1.1 is n = 1 (it's 3) so no
1.2 set result to 3 * FactR(2). Look, we are calling this method again, but with a different value
1.2.1 is n = 1 (it's 2) so no
1.2.2 set result to 2 * FactR(1). Again, we are calling the method with a different value
1.2.2.1 is n = 1 (it's 1) so YES, Return 1.
1.2.2 (cont) now we have the return value from FactR(1) (which is 1) so finish off the calculation, set result = 2*1 = 2.
1.2.3 return result (2)
1.2 (cont) We now have the value of FactR(2) (which is 2) so finish off the calculation, set result = 3 * 2 = 6
1.3 return result (6)

and we are done.

2) Command line arguments are the text you include after the name of the executable, most commonly in DOS programs. For example, copy file1.txt file2.txt. In this case, 'file1.txt' and 'file2.txt' are the command line arguments.

3) static methods do not have access to instance methods/variables but instance methods/variable have access to static methods/variables. Does that help?

### #3 kenryuakuma

• D.I.C Regular

Reputation: 4
• Posts: 408
• Joined: 14-December 08

## Re: Command line Argument, recursive and static constructor

Posted 17 November 2009 - 05:35 PM

Thanks Momerath, you are such a great programmer. But I still don't understand the recursive method, what confused me is the return statement, which is return 1. Does it return 1 to the caller f.factR(3) or to the FactR(), which is the one within the public int factR(int n) method?

I assume it returns 1 to the caller FactR(), which is the one within the public int factR(int n) method. However, if that is true, how do you get 2 and 3?

### #4 Momerath

• D.I.C Lover

Reputation: 1020
• Posts: 2,463
• Joined: 04-October 09

## Re: Command line Argument, recursive and static constructor

Posted 17 November 2009 - 06:16 PM

We'll try again then Using 4 this time, since we don't want to repeat ourselves.

Let's say we have the following line int y = FactR(4);. What happens is this:

(a) We call the function FactR, passing 4 as the argument. The function uses 'n' as the parameter name, so n = 4.
It then checks to see if n == 1. If it is, it returns a 1. In this case it's not (since n = 4), so we move on to the next line.
Now we set result = FactR(n-1) * n. Since we don't know what FactR(n-1) is, the system sets aside all it is doing, and calls the FactR method passing 3 (n-1, 4-1, 3).

(b)Now we are back in FactR (but you can consider it a brand new FactR, it knows nothing about the previous call). n is 3. It's not 1, so now we set result = FactR(n-1) * n. These are different 'result' and 'n' from the previous call, the computer is 'smart' enough to understand this. Again, we don't know what FactR(n-1) is so the system sets aside all it is doing, and calls FactR(2).

©Again, back into FactR(), n = 2, so it's not 1. result = FactR(n-1) * n. Again, it sets aside all it is doing and calls FactR(1).

Ah, now we have a call to FactR where n = 1. We know the answer! It's 1. So we tell the system that the answer is 1.

Now look back at ©. We have the answer to FactR(1) (it being 1), so we can continue with what we were doing. Remember, in © n = 2, so we have FactR(n-1) * n = FactR(1) * 2 = 1 * 2 = 2. So we set result to 2. The next line tells the system that the answer to FactR(2) is 2.

Now we are back in (, where we had n = 3, and result = FactR(n-1) * n = FactR(2) * 3 = 2 * 3 = 6. We tell the system (with the return result; that the answer is 6

Finally we are back in (1) where we had n = 4, and result = FactR(n-1) * n = FactR(3) * 4 = 6 * 4 = 24. We return that answer.

We are out of calls that we have made, so now we are all the way back at the int y = FactR(4). We've figured out that the answer is 24, so y = 24 and we are done.

### #5 kenryuakuma

• D.I.C Regular

Reputation: 4
• Posts: 408
• Joined: 14-December 08

## Re: Command line Argument, recursive and static constructor

Posted 17 November 2009 - 07:26 PM

Well...If we set something new to a variable, doesn't the old value get replaced by the new one and then the old value is gone?

```if(n==1) return 1;
result = factR(n-1) * n;
return result;
```

so if we have the caller with value 4 in there, which is FactR(4), for example. When the method is called

```public int factR(int n) {
int result;
}
```

the value 4 argument is passed to the parameter of the fact®, the method factR() with public accessor, and then with the body of the method, the value of the parameter is used to do the calculation of factR(). So first, it goes to the IF statement, and the result is false, so nothing executes, and then it goes to the next line, and assign 4 to factR(4-1), which becomes factR(3) * 4, and this is assigned to the variable result.

Now, the method calls itself again, factR(3) * 4, is 3 == 1 and the answer is no, so assign factR(3-1, which is 2) * 3 to result. Because when the method is being called the argument 3 within factR() is passed to the parameter, which makes the N of the factR() * N 3. This is keeping repeating until the N within factR() is 1, so 1 == 1 is true, and then return 1 to the caller.

But the question is since the method with the public accessor, the parameter of which is 1 in the final call, shouldn't the factR(n - 1) * n become factR(1) * 1, why is it factR(1) * 2? Because the parameter in the factR() with the public accessor is 1, where the n variable holds the value, and since factR(n - 1) * n, and the n after * is the same as the n in the parameter, so it should be one as well. I just don't understand why it would be 2?

In addition, since once the value has been assigned to the variable, it is fixed and permanent until the new value replace it, so the result should be 1 since 1 * 1 = 1. Why 1? because of the one returned by the if statement. I just don't how why the variable result could hold a bunch of the factR(n - 1) * n?

### #6 Momerath

• D.I.C Lover

Reputation: 1020
• Posts: 2,463
• Joined: 04-October 09

## Re: Command line Argument, recursive and static constructor

Posted 17 November 2009 - 07:40 PM

kenryuakuma, on 17 Nov, 2009 - 06:26 PM, said:

Well...If we set something new to a variable, doesn't the old value get replaced by the new one and then the old value is gone?

Yes, but remember that the variables in a method (method variables) are independant of each other. When you call FactR from within FactR, it creates a brand new 'n' and 'result' that are not the same as the previous ones. That's why I said that it sets aside what it is doing.

### #7 kenryuakuma

• D.I.C Regular

Reputation: 4
• Posts: 408
• Joined: 14-December 08

## Re: Command line Argument, recursive and static constructor

Posted 17 November 2009 - 08:09 PM

With another post found on the internet and your posts combined, I think I know what it is. Phew! that's tough man!

Another question about method. I just wanna make sure if that is correct and confirm if I have really learned.
BTW, you could become an instructor in college or university if you have the master or Phd. Thanks a bunch! You have really helped a lot of people. Anyway, putting this aside.

```class Factorial {
// This is a recursive function.
public int factR(int n) {
int result;

if(n==1) return 1;
result = factR(n-1) * n;
return result;
```

Well...When a value, for example 3 is assigned to N located within on the top of the public int FactR(int n)...Will the N of result = factR(n-1) * n automatically change in a flash or they change only the program reaches there?

### #8 Momerath

• D.I.C Lover

Reputation: 1020
• Posts: 2,463
• Joined: 04-October 09

## Re: Command line Argument, recursive and static constructor

Posted 17 November 2009 - 09:49 PM

What happens (simplified) is something like this:

The program says "I need space for an int", so the system gives it space to hold an int. Then it says "put 3 in that spot". When it gets down to the 'result ...' line it says "I need the value from that location you gave me earlier, and I want to subtract one from it. After that, call FactR with that value, and when it returns, get the value from that memory location again, and multiply it by what the FactR method gave me. Then store the value in the location where you told me to put 'result' stuff".

What it actually looks like is much more complicated:

```	public int factR(int n) {
00000000  mov		 dword ptr [rsp+10h],edx
00000004  mov		 qword ptr [rsp+8],rcx
00000009  sub		 rsp,48h
0000000d  mov		 dword ptr [rsp+20h],0
00000015  mov		 dword ptr [rsp+24h],0
0000001d  mov		 byte ptr [rsp+28h],0
00000022  mov		 rax,7FF001B2018h
0000002c  mov		 eax,dword ptr [rax]
0000002e  test		eax,eax
00000030  je		  0000000000000037
00000032  call		FFFFFFFFF0CF7F30
00000037  nop
int result;

if (n == 1)
00000038  xor		 eax,eax
0000003a  cmp		 dword ptr [rsp+58h],1
0000003f  setne	   al
00000042  mov		 dword ptr [rsp+2Ch],eax
00000046  movzx	   eax,byte ptr [rsp+2Ch]
0000004b  mov		 byte ptr [rsp+28h],al
0000004f  movzx	   eax,byte ptr [rsp+28h]
00000054  test		eax,eax
00000056  jne		 0000000000000062
return 1;
00000058  mov		 dword ptr [rsp+24h],1
00000060  jmp		 000000000000008E
result = factR(n - 1) * n;
00000062  mov		 edx,dword ptr [rsp+58h]
00000066  sub		 edx,1
00000069  mov		 rcx,qword ptr [rsp+50h]
0000006e  call		FFFFFFFFFFEC9410
00000073  mov		 dword ptr [rsp+30h],eax
00000077  mov		 eax,dword ptr [rsp+30h]
0000007b  imul		eax,dword ptr [rsp+58h]
00000080  mov		 dword ptr [rsp+20h],eax
return result;
00000084  mov		 eax,dword ptr [rsp+20h]
00000088  mov		 dword ptr [rsp+24h],eax
0000008c  jmp		 000000000000008E
}
0000008e  mov		 eax,dword ptr [rsp+24h]
00000092  jmp		 0000000000000094
00000098  rep ret
```

This post has been edited by Momerath: 17 November 2009 - 09:52 PM

### #9 kenryuakuma

• D.I.C Regular

Reputation: 4
• Posts: 408
• Joined: 14-December 08

## Re: Command line Argument, recursive and static constructor

Posted 18 November 2009 - 08:04 PM

Well...Momerath's example and lucid explanation has cleared all things up...But to novice, it is still very difficult to understand. Having understood a bit of what recursive method is, I have been thinking if there is a way to make this a bit easier to understand, or make myself fully grasp the concept. Finally, I have come up an easy way to eliminate this doubt and once for all.

According to what Momerath's explanation, the variables within the method are independent of one another. For instance, we could have two methods with same identifier but they must have different parameters or different values or arguments and they can also have same variables as long as the variables are declared within the methods themselves.

```public int value(int n) {
int a, b;

// statement
}

public int value(double n) {
int a, b;

// this is what so called the overloaded method
}
```

This is applied to the case of recursive method. ANYBODY CORRECTS ME IF I AM WRONG. But it seems to me that the recursive method is the overloaded method because of the same identifier and same variables but with different arguments sent to the parameter. Instead of using a recursive, you could use the for and if statements as well. No big deal. But for recursive method, if the method calls itself three times, rather than using recursion, you could use three different methods to replace it. This is what clears that up by using three methods that return the value to the caller in executive order.

This post has been edited by kenryuakuma: 18 November 2009 - 08:06 PM

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }