函数是什么

比如你写:

1
printf("hello");

printf() 本身就是一个函数。
你只需要告诉它:帮我打印这个东西

它具体怎么打印,你不用管。
我们自己也可以写函数:

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
void hello()
{
printf("Hello!\n");
}
int main()
{
hello();
return 0;
}

输出就是Hello!

这里:
void hello()
就是定义了一个叫 hello 的函数。
然后:
hello();
就是调用它。

我们升级一下难度
来写一个很简单的计算两个数字相加

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
int sum(int a,int b){
return a + b ;
}
int main(){
int c;
int d;
printf("input your first number\n");
scanf("%d",&c);
printf("input your second number\n");
scanf("%d",&d);
printf("%d",sum(c,d));
}

也可以将两个字符串拼起来

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <string.h>
void caito(char a[20],char b[20]){
strcat(a,b);
}
int main(){
char a[20];
char b[20];
scanf("%s",a);
scanf("%s",b);
caito(a,b);
printf("%s",a);
}

哈哈哈这样是不是很多此一举