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

函数(子程序):

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

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>

intmain()

{

    chararr1[20] = { 0 };

    chararr2[20] ="hello world";

    strcpy(arr1, arr2);

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

    return0;

}


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>

intmain()

{

    chararr[] ="hello world";

    memset(arr,'x', 5);

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

    return0;

}

自定义函数:

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

函数的组成:

ret_type fun_name(para, *)

{

    statemate;         //语句项

} 

ret_type  返回类型

fun_name  函数名

para1     函数参数


例子:

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

#include<stdio.h>

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

{

    intz = 0;

    if(x>y)

         z =x;

    else

         z =y;

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

}

intmain()

{

    inta = 5;

    intb = 51;

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

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

    return0;

}


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

#include<stdio.h>

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

{

    intz = 0;

    z =x;

    x=y;

    y= z;

}

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

intmain()

{

    inta = 10;

    intb = 20;

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

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

    swap(a, b);

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

    return0;

}

改正后:

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

{

    intz = 0;

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

    *pa= *pb//交换位置

    *pb= z;

}

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

intmain()

{

    inta = 12;

    intb = 14;

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

    swap(&a, &b);

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

   

    return0;

}


实际参数(实参):

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

形式参数(形参):

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

函数的调用:

传值调用:

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

传址调用:

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


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

#include<stdio.h>

intis_prime(int n)

{

    intj = 0;           

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

    {

         if(n% j == 0)

             return0;

    }

    return1;

}

intmain()

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

    inti = 0;

    intcount = 0;

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

    {

         if(is_prime(i) == 1)

         {

             count++;

             printf("%d ", i);

         }

    }

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

    return0;

}


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

例:

#include<stdio.h>

voidtest3()

{

    printf("hello");

}

inttest2()

{

    test3();

    return0;

}

intmain()

{

    test2();

    return0;

}

例2:

#include<stdio.h>

#include<string.h>

intmain()

{

    intlen = strlen("abc");

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

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

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

    return0;

}

链式访问例3:

原代码:

#include<stdio.h>

#include<string.h>

intmain()

{

    chararr1[20] = { 0 };

    chararr2[] ="hello";

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

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

    return0;

}

修改成链式访问:

#include<stdio.h>

#include<string.h>

intmain()

{

    chararr1[20] = { 0 };

    chararr2[] ="bit";

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

    return0;

}

练习:

#include<stdio.h>

intmain()

{

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

    return0;

}

以上运行结果为:

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>

intmain()

{

    inta = 10;

    intb = 20;

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

    intAdd(int,int);

    intc = Add(a, b);

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

    return0;

}

//函数的定义

intAdd(int x,int y)

{

    return x+y;

}



版权声明:本文为qq_42357733原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。