#include <stdio.h>
#include <stdlib.h>
int sum(int c, int d);
int main()
{
int a, b, s;
a = 123; b = 456;
s = sum(a, b);
printf("%d\n", s);
return 0;
}
int sum(int c, int d)
{
int t;
t = c + d;
return t;
#include <stdlib.h>
int sum(int c, int d);
int main()
{
int a, b, s;
a = 123; b = 456;
s = sum(a, b);
printf("%d\n", s);
return 0;
}
int sum(int c, int d)
{
int t;
t = c + d;
return t;
}
函数定义:函数首部和函数体。可以分为有参数和无参数,有返回值和无返回值。
函数中实参和形参完成数据传送,传送前实参必须先赋值。
单向值传递,如下面编程,实参传递给形参,但形参互换,不影响实参输入。
#include <stdio.h>
#include <stdlib.h>
void swap(int a, int b);
int main()
{
int a, b;
scanf("%d%d", &a, &b);
printf("%d%d\n", a, b);
swap(a, b);
printf("%d%d\n", a, b);
return 0;
}
void swap(int a, int b)
{
printf("%d%d\n", a, b);
int t;
t = a; a = b; b = t;
printf("%d%d\n", a, b);
}
在无返回值的函数中,return可以缺省;同时,return可以有多个,执行哪个,就返回哪个。