C语言学习笔记:自定义函数、函数的调用、嵌套和链式访问、函数的声明和定义

本文详细介绍了C语言中的函数,包括库函数和自定义函数的使用。库函数如printf、scanf、strcpy等用于常见操作,而自定义函数允许程序员根据需求定制功能。文章通过示例展示了如何定义和调用函数,以及函数参数的传值和传址调用。此外,还讲解了函数的嵌套调用和链式访问,并提供了判断素数的函数作为实践案例。最后,讨论了函数声明和定义的区别。
摘要由CSDN通过智能技术生成

函数(子程序):

是一个大型程序中的某部分代码,由一个或多个语句块组成。它负责完成某项特定任务,而且相较于其他代码,具备相对的独立性。一般会有输入参数并有返回值,提供对过程的封装和细节的隐藏。这些代码通常被集成为软件库。

C语言中函数的分类:1.库函数 2.自定义函数

常用的库函数:

IO函数:printf scanf getchar putchar

字符串操作函数: strcmp strlen

字符操作函数:toupper

内存操作函数:memcpy memcmp meset

时间/日期函数:time

数学函数:sqrt pow

其他库函数:……


例子:

strcpy:Copy a string.                        //头文件为:string.h

char *strcpy( char *strDestination, const char *strSource );

#include<stdio.h>

#include<string.h>

int main()

{

    char arr1[20] = { 0 };

    char arr2[20] = "hello world";

    strcpy(arr1, arr2);

    printf("%s", arr1);       //打印arr1这个字符串,以字符串的格式打印--用%s

    return 0;

}


memsetSets buffers to a specified character.

void *memset( void *dest, int c, size_t count );

其中:

dest:Pointer to destination

c:Character to set

count:Number of characters


#include<stdio.h>

#include<string.h>

int main()

{

    char arr[] = "hello world";

    memset(arr, 'x', 5);

    printf("%s\n", arr);

    return 0;

}

自定义函数:

和库函数一样,有函数名,返回值类型和函数参数。但是是程序员自己来设计的。

函数的组成:

ret_type fun_name(para, *)

{

    statemate;          //语句项

} 

ret_type   返回类型

fun_name   函数名

para1      函数参数


例子:

写一个函数找出两个整数中的最大值。

#include<stdio.h>

int get_max(int x, int y)          //定义函数,圆括号里面是函数参数; int和z的类型是前后呼应的,z是整数,所以用int

{

    int z = 0;

    if (x > y)

         z = x;

    else

         z = y;

    return z;      //返回z---返回较大值

}

int main()

{

    int a = 5;

    int b = 51;

    int max=get_max(a,b);         //函数的调用

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

    return 0;

}


例2:写一个函数可以交换两个整型变量的内容

#include<stdio.h>

void swap(int x, int y)   //函数返回类型的地方写出:void,表示这个函数不返回任何值,也不需要返回

{

    int z = 0;

    z = x;

    x = y;

    y = z;

}

//以上这个代码有问题,交换不了。因为,交换的是形参而不是实参

int main()

{

    int a = 10;

    int b = 20;

    //写一个函数-交换两个整型变量的值

    printf("交换前:a=%d b=%d\n", a, b);

    swap(a, b);

    printf("交换后:a=%d b=%d\n", a, b);

    return 0;

}

改正后:

void swap(int* pa, int* pb)            //*pa访问到的是a的地址;*pb是b的地址

{

    int z = 0;

    z = *pa//通过*pa找到了a

    *pa = *pb//交换位置

    * pb = z;

}

//指针存的是地址,当把&a传上去之后,加星号就是访问这个地址的空间,接着就可以修改影响到主函数的a和b

int main()

{

    int a = 12;

    int b = 14;

    printf("交换前:a=%d b=%d\n", a, b);

    swap(&a, &b);

    printf("交换后:a=%d b=%d\n", a, b);

   

    return 0;

}


实际参数(实参):

真实传给函数的参数。实参可以是:常量、变量、表达式、函数等。无论实参是什么类型的量,在进行函数调用时,它们必须有确定的值,以便把这些值传给形参。

形式参数(形参):

指函数名后括号中的变量,因为形式参数只有函数被调用的过程中才实例化(分配内存单元),所以叫形式参数。形式参数当函数调用完成后就自动销毁了,因此形式参数只在函数中有效。

函数的调用:

传值调用:

函数的形参和实参分别占有不通内存块,对形参的修改不会影响实参。

传址调用:

是把函数外部创建变量的内存地址传递给函数参数的一种调用函数的方式。这种传参方式可以让函数和函数外边的变量建立起真的联系,也就是函数内部可以直接操作函数外部的变量。


练习:写一个函数判断一个数是不是素数

#include<stdio.h>

int is_prime(int n)

{

    int j = 0;           

    for (j = 2; j < n; j++)   //for (j = 2; j <= sqrt(n); j++)

    {

         if (n % j == 0)

             return 0;

    }

    return 1;

}

int main()

{                      //100-200之间的素数/质数

    int i = 0;

    int count = 0;

    for (i = 100; i <= 200; i++)

    {

         if (is_prime(i) == 1)

         {

             count++;

             printf("%d ", i);

         }

    }

    printf("\ncount = %d\n", count);

    return 0;

}


函数的嵌套调用和链式访问:

例:

#include<stdio.h>

void test3()

{

    printf("hello");

}

int test2()

{

    test3();

    return 0;

}

int main()

{

    test2();

    return 0;

}

例2:

#include<stdio.h>

#include<string.h>

int main()

{

    int len = strlen("abc");

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

    //链式访问: 把一个函数的返回值作为另外一个函数的参数

    printf("%d\\n", strlen("abc"));

    return 0;

}

链式访问例3:

原代码:

#include<stdio.h>

#include<string.h>

int main()

{

    char arr1[20] = { 0 };

    char arr2[] = "hello";

    strcpy(arr1, arr2);       //如果strcpy报错,所以加上“_s”。一般是strcpy

    printf("%s\n", arr1);

    return 0;

}

修改成链式访问:

#include<stdio.h>

#include<string.h>

int main()

{

    char arr1[20] = { 0 };

    char arr2[] = "bit";

    printf("%s\n", strcpy(arr1, arr2));

    return 0;

}

练习:

#include<stdio.h>

int main()

{

    printf("%d", printf("%d", printf("%d", 43)));

    return 0;

}

以上运行结果为:

A.43 43 43

B.  43

C.  4321

D.  43 43

                                                                                                                        答案:C

解析:这里注意printf的概念是什么:

Return Value

Each of these functions returns the number of characters printed, or a negative value if an error occurs.

函数的声明和定义:

函数声明:

1.告诉编译器有一个函数叫什么,参数是什么,返回类型是什么。但是具体是不是讯在,无关紧要。

2.函数的声明一般出现在函数的使用之前。要满足先声明后使用。 函数的声明一般要放在头文件中的。

函数定义:

指函数的具体实现,交代函数的功能实现。


例1:

原代码:

 这个时候需要声明一下函数。

修改后的代码:

#include<stdio.h>

int main()

{

    int a = 10;

    int b = 20;

    //函数声明一下-告知系统

    int Add(int, int);

    int c = Add(a, b);

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

    return 0;

}

//函数的定义

int Add(int x, int y)

{

    return x + y;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值