Recursive invocation of 2.3 functions
Turbo C2.0 allows functions to call themselves, that is, recursive calls to functions, recursive calls can make the program concise, code compact, but at the expense of memory space for processing of the stack.
If a n! is required The value of (n's factorial) can be invoked recursively as follows:
Example 8:
#include <stdio.h>
Unsigned ling mul (int n);
int main ()
{
int m;
Puts ("Calculate n! N=?\n ");
scanf ("%d", &m); /* Keyboard input data * *
printf ("%d!=%ld\n", M, Mul (m));/* Call subroutine COMPUTE and OUTPUT * *
Getch ();
Retun 0;
}
unsigned long mul (int n)
{
unsigned long p;
if (n>1)
P=n*mul (n-1); /* Recursive call calculation n!*/
Else
p=1l;
return (p); /* Return result * *
}
Run Result:
Calculate n! N=?
Enter 5 o'clock the result is:
5!=120
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.