C# 0 The Complete Reference


FactR( )  is a bit more complex. When  FactR( )



Download 4,07 Mb.
Pdf ko'rish
bet279/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   275   276   277   278   279   280   281   282   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

FactR( )

 is a bit more complex. When 



FactR( )

 is called with 

an argument of 1, the method returns 1; otherwise, it returns the product of 

FactR(n–1)*n

. To 


evaluate this expression

FactR( )

 is called with 



n–1

. This process repeats until 



n

 equals 1 and 

the calls to the method begin returning. For example, when the factorial of 2 is calculated, the 

first call to 



FactR( )

 will cause a second call to be made with an argument of 1. This call will 

return 1, which is then multiplied by 2 (the original value of 

n

). The answer is then 2. You 

might find it interesting to insert 

WriteLine( )

 statements into 



FactR( )

 that show the level of 

recursion of each call and what the intermediate results are.

When a method calls itself, new local variables and parameters are allocated storage on 

the system stack, and the method code is executed with these new variables from the start. 

A recursive call does not make a new copy of the method. Only the arguments are new. As 

each recursive call returns, the old local variables and parameters are removed from the 

stack, and execution resumes at the point of the call inside the method. Recursive methods 

could be said to “telescope” out and back.

Here is another example of recursion. The 



DisplayRev( )

 method uses recursion to 

display its string argument backward.

// Display a string in reverse by using recursion.

using System;

class RevStr {

  // Display a string backward.

  public void DisplayRev(string str) {

    if(str.Length > 0)

      DisplayRev(str.Substring(1, str.Length-1));

    else

      return;

    Console.Write(str[0]);

  }


}

class RevStrDemo {

  static void Main() {

    string s = "this is a test";

    RevStr rsOb = new RevStr();

    Console.WriteLine("Original string: " + s);

    Console.Write("Reversed string: ");

    rsOb.DisplayRev(s);

    Console.WriteLine();

  }


}

Here is the output:

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   275   276   277   278   279   280   281   282   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish