函数的定义和使用

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>

 

/*

返回值类型  函数名  (参数列表)

{

  代码体

  return 0;

*/

//返回值类型(int)要和返回值(30)对应

//函数名(1、不能使用系统关键字。2、不允许数字开头。3、标识符区分大小写。4、允许使用数字、字母、下划线。5、见名知义,简名扼要)

//参数列表:参数有多个需用逗号隔开,且参数要对应。形参可用可无

//在不同函数中,变量名可以重名,因为作用域不同

//函数定义:在函数定义过程中,参数称为形参(形式参数:(告诉数据类型,但没有具体的值))

//在定义函数时指定的形参,在未出现函数调用时,它们并不占内存中的存储单元,形参里的变量不能赋值(int add(int a=10, int b=20))

 int add(int a,int b)

{

  int sum=a+b;

  return sum;

}

void(print)

{

  printf("hello world\n")

}

int main01()

{

  int a=10;

  int b=20;

  int c;

//函数调用:在函数调用的过程中,传递的参数称为实参(实际参数:(有具体的值))

//在函数调用的过程中,将实参传递给实参

//在函数调用结束时会在内存中销毁(栈区自动销毁)

  c=add(a,b);

  printf("%d\n",c);

 

  print();//调用void print()

  return 0;

}

 

void swap(int a,int b)

{

  int temp=a;

  a=b;

  b=temp;

}

int main()

{

  int a=10;

  int b=20;

  printf("交换前数据\n");

  printf("a=%d\n",a);

  printf("b=%d\n",b);

//函数调用

  swap(a,b);

//实参变量对形参变量的数据传递是“值传递”,即单向传递,只能由实参传给形参,则不能形参传给实参

//在执行一个被调函数时,形参的值如果发生改变,并不会改变主调函数上实参的值

  printf("交换后数据:\n");

  printf("a=%d\n", a);
  printf("b=%d\n", b);

  

  return 0;

}

 

posted @ 2020-08-18 22:10  wh19991213  阅读(180)  评论(0编辑  收藏  举报