Home > Backend Development > C#.Net Tutorial > What are recursive method calls in C#?

What are recursive method calls in C#?

WBOY
Release: 2023-09-09 09:13:04
forward
1158 people have browsed it

C# 中的递归方法调用是什么?

Recursive method calls in C# are called recursion. Let's look at an example of calculating powers of numbers using recursion.

Here, if the power is not equal to 0, a function call occurs, which ends up being recursive -

if (p!=0) {
   return (n * power(n, p - 1));
}
Copy after login

Above, n is the number itself, and the power is reduced with each iteration, as shown below -

Example

using System;
using System.IO;

public class Demo {
   public static void Main(string[] args) {
      int n = 5;
      int p = 2;
      long res;
      res = power(n, p);
      Console.WriteLine(res);
   }

   static long power (int n, int p) {
      if (p!=0) {
         return (n * power(n, p - 1));
      }
      return 1;
   }
}
Copy after login

The above is the detailed content of What are recursive method calls in C#?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template