19.md 3.8 KB
Newer Older
W
wizardforcel 已提交
1
# C 用户定义的函数
W
init  
wizardforcel 已提交
2 3 4 5 6 7 8

> 原文: [https://www.programiz.com/c-programming/c-user-defined-functions](https://www.programiz.com/c-programming/c-user-defined-functions)

#### 在本教程中,您将借助示例学习在 C 编程中创建用户定义的函数。

函数是执行特定任务的代码块。

W
wizardforcel 已提交
9
C 允许您根据需要定义函数。 这些函数称为用户定义函数。 例如:
W
init  
wizardforcel 已提交
10 11 12

假设您需要创建一个圆并根据半径和颜色为其着色。 您可以创建两个函数来解决此问题:

W
wizardforcel 已提交
13 14
*   `createCircle()`函数
*   `color()`函数
W
init  
wizardforcel 已提交
15 16 17

* * *

W
wizardforcel 已提交
18
## 示例:用户定义的函数
W
init  
wizardforcel 已提交
19

W
wizardforcel 已提交
20
这是相加两个整数的示例。 为了执行此任务,我们创建了一个用户定义的`addNumbers()`
W
init  
wizardforcel 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

```c
#include <stdio.h>
int addNumbers(int a, int b);         // function prototype

int main()
{
    int n1,n2,sum;

    printf("Enters two numbers: ");
    scanf("%d %d",&n1,&n2);

    sum = addNumbers(n1, n2);        // function call
    printf("sum = %d",sum);

    return 0;
}

int addNumbers(int a, int b)         // function definition   
{
    int result;
    result = a+b;
    return result;                  // return statement
}

```

* * *

W
wizardforcel 已提交
50
## 函数原型
W
init  
wizardforcel 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

函数原型只是函数的声明,用于指定函数的名称,参数和返回类型。 它不包含函数体。

函数原型向编译器提供信息,该函数以后可以在程序中使用。

### 函数原型的语法

```c
returnType functionName(type1 argument1, type2 argument2, ...);
```

在上面的示例中,`int addNumbers(int a, int b);`是函数原型,它向编译器提供以下信息:

1.  该函数的名称为`addNumbers()`
2.  函数的返回类型为`int`
3.  类型为`int`的两个参数传递给函数

如果在`main()`函数之前定义了用户定义的函数,则不需要函数原型。

* * *

## 调用函数

程序的控制权通过调用转移到用户定义的函数。

### 函数调用的语法

```c
functionName(argument1, argument2, ...);
```

在上面的示例中,使用`main()`函数内部的`addNumbers(n1, n2);`语句进行函数调用。

* * *

W
wizardforcel 已提交
86
## 函数定义
W
init  
wizardforcel 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

函数定义包含执行特定任务的代码块。 在我们的示例中,将两个数字相加并返回。

#### 函数定义的语法

```c
returnType functionName(type1 argument1, type2 argument2, ...)
{
    //body of the function
}

```

调用函数时,程序的控制权将转移到函数定义中。 并且,编译器开始在函数体内执行代码。

* * *

## 将参数传递给函数

W
wizardforcel 已提交
106
在编程中,参数是指传递给函数的变量。 在上面的示例中,在函数调用期间传递了两个变量`n1``n2`
W
init  
wizardforcel 已提交
107

W
wizardforcel 已提交
108
参数`a``b`接受函数定义中传递的参数。 这些参数称为函数的形式参数。
W
init  
wizardforcel 已提交
109 110 111 112 113

![Passing arguments to a function](img/278b9f4435b5f422ec65ac4cc6a4d4fa.png "Passing arguments to a function")

传递给函数的参数类型和形式参数必须匹配,否则,编译器将引发错误。

W
wizardforcel 已提交
114
如果`n1``char`类型,则`sum`也应为`char`类型。 如果`n2`为浮点型,则变量`b`也应为浮点型。
W
init  
wizardforcel 已提交
115 116 117 118 119

也可以在不传递参数的情况下调用函数。

* * *

W
wizardforcel 已提交
120
## `return`语句
W
init  
wizardforcel 已提交
121

W
wizardforcel 已提交
122
`return`语句终止函数的执行并将值返回给调用函数。 `return`语句之后,程序控制权将转移到调用函数。
W
init  
wizardforcel 已提交
123

W
wizardforcel 已提交
124
在上面的示例中,`result`变量的值返回到主函数。 为`main()`函数中的`总和`变量分配了该值。
W
init  
wizardforcel 已提交
125 126 127

![Return statement of a function](img/a0d917534f1ad258ea920ed2a8a67808.png "Return statement of a function")

W
wizardforcel 已提交
128
### `return`语句的语法
W
init  
wizardforcel 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

```c
return (expression);     

```

例如,

```c
return a;
return (a+b);
```

从函数返回的值的类型与函数原型和函数定义中指定的返回类型必须匹配。

访问此页面以了解有关[传递参数和从函数](/c-programming/types-user-defined-functions "Passing argument and returning value")返回值的更多信息。