Learn point C language (35): function

Source: Internet
Author: User
Tags printf

1. Recursion is: The function itself calls itself

This is one of the simplest recursion, but it will always execute and can be terminated with CTRL + C.

#include <stdio.h>

void prn(void) {
  printf("C++Builder 2009\n");
  prn(); /* 自调用;注意它会一直执行,可用Ctrl+C终止执行 */
}

int main(void)
{
  prn();
  getchar();
  return 0;
}

2. Use recursion must have the conditions to jump out:

#include <stdio.h>

void prn(int num) {
  printf("%d\n", num);
  if (num > 0) prn(--num);
}

int main(void)
{
  prn(9);
  getchar();
  return 0;
}

3. Example: Flip a string

#include <stdio.h>

void revers(char *cs);

int main(void)
{
  revers("123456789");

  getchar();
  return 0;
}

void revers(char *cs)
{
  if (*cs)
  {
    revers(cs + 1);
    putchar(*cs);
  }
}

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.