类型标识符 函数名([形式参数类型 形式参数, ...]){
//函数逻辑
return 返回值;
}
int add(int i, int j)
{
return i + j;
}
#include <stdio.h>
int add(int a,int b);
int main() {
int d=8;
int e=4;
int res=add(d,e);
printf("%d\n",res);
return 0;
}
int add(int a,int b){
return a+b;
}
//求和函数的定义
#include <stdio.h>
int Add(int a,int b){
return a+b;
}
int main() {
int d=8;
int e=4;
int res=Add(d,e);
printf("%d\n",res);
return 0;
}
#include <stdio.h>
void swap(int *a, int *b) // 1.声明并定义
{
int t = *a;
*a = *b;
*b = t;
}
int main(void)
{
int a = 2;
int b = 3;
printf("%d,%d\n", a, b);
swap(&a, &b); // 2.使用
printf("%d,%d\n", a, b);
return 0;
}
#include <stdio.h>
int sum0(const int *arr, const int len);
int sum1(const int *arr, const int len);
int main(void)
{
int str[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int res;
res = sum0(str, sizeof(str) / sizeof(int));
printf("sum=%d\n", res);
res = sum1(str, sizeof(str) / sizeof(int));
printf("sum=%d\n", res);
return 0;
}
int sum0(const int *arr, const int len)
{
int sum = 0;
for (int i = 0; i < len; i++)
sum += *(arr + i);
return sum;
}
int sum1(const int arr[], const int len)
{
int sum = 0;
for (int i = 0; i < len; i++)
// sum += arr[i];
sum += *(arr + i);
return sum;
}
#include <stdio.h>
#define M 2
#define N 3
void dis(int *p, int len);
int main(void)
{
int a[M][N] = {1, 2, 3, 7, 8, 9};
int i, j;
dis(&(a[0][0]), M * N);
printf("\n");
}
void dis(int *p, int len)
{
int i;
for (i = 0; i < len; i++)
{
printf("%d\t", p[i]);
}
}
#include <stdio.h>
#define M 2
#define N 3
void disM(int (*p)[N], int m, int n);
int main(void)
{
int a[M][N] = {{1, 2, 3}, {7, 8, 9}};
disM(a, M, N);
}
void disM(int (*p)[N], int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d\t", p[i][j]);
}
printf("\n");
}
printf("\n");
}
#include <stdio.h>
#define M 2
#define N 3
void disRow(int (*p)[N], int m);
int main(void)
{
int a[M][N] = {{1, 2, 3}, {7, 8, 9}};
disRow(a, 0);
}
void disRow(int p[][N], int m)
{
int i;
for (i = 0; i < N; i++)
{
printf("%d\t", p[m][i]);
}
printf("\n");
}
#include <stdio.h>
char *strcopy(char *dest, const char *src);
int main(void)
{
char strDest[128];
char strSrc[] = "helloworld";
strcopy(strDest, strSrc);
puts(strDest);
}
char *strcopy(char *dest, const char *src)
{
while ((*dest++ = *src++) != '\0')
;
return dest;
}
#include <stdio.h>
int lowerNum(const char *p); // 1.声明
int main(void)
{
char str[] = "helloMOTO.";
int len;
len = lowerNum(str); // 2.使用
printf("num=%d\n", len);
return 0;
}
int lowerNum(const char *p) // 3.定义
{
int n = 0;
while (*p != '\0')
{
if (*p >= 'a' && *p <= 'z')
{
n++;
}
p++; //注意位置
}
return n;
}
#include <stdio.h>
void spaceDel(char *str);
int main(void)
{
char str[] = "he llo, cn pla man"; //如果使用*str又如何?
printf("str=%s\n", str);
spaceDel(str);
printf("str=%s\n", str);
return 0;
}
void spaceDel(char *str)
{
char *p = str;
while (*str)
{
if (*str == ' ')
{
str++;
}
else
{
*p = *str;
p++;
str++;
}
}
*p = '\0'; //如果没有,结果会是如何?
}
类型标识符 *函数名(函数参数){
函数体;
}
//指针函数
#include <stdio.h>
int *Max(int *a, int *b)
{
return *a>*b? a : b;
}
void main()
{
int a=10;
int b=20;
int *c=Max(&a,&b);
printf("%d\n",*c);
}
//普通函数
#include <stdio.h>
int Max(int *a, int *b)
{
return *a>*b? *a : *b;
}
void main()
{
int a=10;
int b=20;
int c=Max(&a,&b);
printf("%d\n",c);
}
int main(int argc, char *argv[])
{
int i = 0;
for (; i < argc; i++)
{
printf("argc[%d]=%s\n", i, argv[i]);
}
return 1;
}
//执行
PS D:\MinGW> .\a.exe aa bb cc dd
argc[0]=D:\MinGW\a.exe // 函数的调用也算是一个参数
argc[1]=aa
argc[2]=bb
argc[3]=cc
argc[4]=dd
套娃
int nn(int num)
{
int res ;
if (num > 0)
{
res = num * nn(num - 1);
}
else
{
res = 1;
}
return res;
}
int peach(int n)
{
return n == 10 ? 1 : peach(n + 1) * 2 + 2;
}
int age(int n)
{
return n == 1 ? 10 : age(n - 1) + 2;
}