Learn point C language (32): function

Source: Internet
Author: User
Tags printf

A function in C can return any type other than an array (excluding array pointers).

Does not specify the return type, in the previous C language version is the default return int, now C99, C + + is not supported.

void means that no return value is required; If the other statement must have a return statement.

In the main function (just the main function) if you forget return, some compilers will automatically add return 0;

1. Each execution return statement function will exit:

#include <stdio.h>

int main(void)
{
  printf("111\n");
  printf("222\n");

  return 0; /* 之后的语句将不会执行 */

  printf("333\n");
  printf("444\n");

  getchar();
  return 0;
}

2. If the function does not return a value, you can borrow the returned without parameters:

#include <stdio.h>

void prn(void);

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

void prn(void) {
  printf("111\n");
  printf("222\n");

  return; /* 无参数的 return */

  printf("333\n");
  printf("444\n");
}

3. Return is not followed by an "equal sign", but the returned value can also be written in parentheses:

#include <stdio.h>

long long MySqr(int x);

int main(void)
{
  int i = 9;

  i = MySqr(i);
  printf("%d\n", i);

  getchar();
  return 0;
}

long long MySqr(int x) {
  return(x*x); /* 返回值在括号中 */
}

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.