Learn point C language (33): function

Source: Internet
Author: User
Tags printf

1. Pass value parameter (not pointer parameter):

#include <stdio.h>

int inc(int x);

int main(void)
{
  int num = 1;

  printf("%d\n",inc(num)); /* 2 */
  printf("%d\n",num);   /* 1; num 并没有改变,用作函数参数时只是复制过去 */

  getchar();
  return 0;
}

int inc(int x) {
  x++;
  return x;
}

2. Address: Parameters are pointers, parameters are addresses

#include <stdio.h>

int inc(int *p);

int main(void)
{
  int num = 1;

  printf("%d\n",inc(&num)); /* 2 */
  printf("%d\n",num);    /* 2; num 已被修改 */

  getchar();
  return 0;
}

int inc(int *p) {
  *p = *p + 1; /* 通过地址修改了值 */
  return *p;
}

3. Examples to address but not be modified:

#include <stdio.h>

int inc(int *p);

int main(void)
{
  int num = 1;

  printf("%d\n",inc(&num)); /* 2 */
  printf("%d\n",num);    /* 1 */
  /* 虽然函数是传址,但这里的 num 并没有改变; 因为下面的函数中并没有给指针赋值 */

  getchar();
  return 0;
}

int inc(int *p) {
  return *p + 1;
}

4. Formal participation in the actual parameter:

It's just not much of a name, like in the following example

X and Y are the formal parameters of the SUM function;

I and 22 are arguments to the SUM function.

#include <stdio.h>

int sum(int x,int y);

int main(void)
{
  int i = 11;

  i = sum(i,22);

  printf("%d\n",i);
  getchar();
  return 0;
}

int sum(int x,int y) {
  return x + y;
}

Back to "Learn point C-Directory"

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.