C函数简介
函数是我们将代码结构化为子例程的方式,我们可以:
- 给...起个名字
- 需要我们时打电话
从您的第一个程序“ Hello,World!”开始,您立即使用C功能:
#include <stdio.h>
int main(void) {
printf("Hello, World!");
}
这main()
函数是非常重要的函数,因为它是C程序的入口点。
这是另一个功能:
void doSomething(int value) {
printf("%u", value);
}
功能具有4个重要方面:
- 它们有一个名字,所以我们以后可以调用(“调用”)它们
- 他们指定一个返回值
- 他们可以争论
- 他们有一个身体,用大括号包裹
函数体是我们每次调用函数时都会执行的指令集。
如果该函数没有返回值,则可以使用关键字void
函数名称之前。否则,您可以指定函数返回值类型(int
对于一个整数,float
对于浮点值,const char *
用于字符串等)。
您不能从一个函数返回多个值。
一个函数可以有参数。它们是可选的。如果没有它们,我们在括号内插入void
, 像这样:
void doSomething(void) {
/* ... */
}
在这种情况下,当我们调用该函数时,我们将在括号内不加任何名称的情况下调用该函数:
doSomething();
如果有一个参数,则指定参数的类型和名称,如下所示:
void doSomething(int value) {
/* ... */
}
调用函数时,将在括号中传递该参数,如下所示:
doSomething(3);
我们可以有多个参数,如果这样,我们可以在声明和调用中使用逗号将它们分开:
void doSomething(int value1, int value2) {
/* ... */
}
doSomething(3, 4);
参数传递复制。这意味着如果您修改value1
,则会在本地修改其值,并且在调用中传递该值的函数外部的值不会更改。
如果您通过指针作为参数,您可以修改该变量值,因为现在可以使用其内存地址直接访问它。
您无法为参数定义默认值。 C ++可以做到这一点(Arduino语言程序也可以做到),但是C不能。
确保在调用函数之前定义了该函数,否则编译器将引发警告和错误:
➜ ~ gcc hello.c -o hello; ./hello
hello.c:13:3: warning: implicit declaration of
function 'doSomething' is invalid in C99
[-Wimplicit-function-declaration]
doSomething(3, 4);
^
hello.c:17:6: error: conflicting types for
'doSomething'
void doSomething(int value1, char value2) {
^
hello.c:13:3: note: previous implicit declaration
is here
doSomething(3, 4);
^
1 warning and 1 error generated.The warning you get regards the ordering, which I already mentioned.
The error is about another thing, related. Since C does not “see” the function declaration before the invocation, it must make assumptions. And it assumes the function to return int
. The function however returns void
, hence the error.
If you change the function definition to:
int doSomething(int value1, int value2) {
printf("%d %d\n", value1, value2);
return 1;
}
you’d just get the warning, and not the error:
➜ ~ gcc hello.c -o hello; ./hello
hello.c:14:3: warning: implicit declaration of
function 'doSomething' is invalid in C99
[-Wimplicit-function-declaration]
doSomething(3, 4);
^
1 warning generated.In any case, make sure you declare the function before using it. Either move the function up, or add the function prototype in a header file.
Inside a function, you can declare variables.
void doSomething(int value) {
int doubleValue = value * 2;
}
A variable is created at the point of invocation of the function, and is destroyed when the function ends, and it’s not visible from the outside.
Inside a function, you can call the function itself. This is called recursion and it’s something that offers peculiar opportunities.
Download my free C Handbook
More clang tutorials:
- Introduction to the C Programming Language
- C Variables and types
- C Constants
- C Operators
- C Conditionals
- How to work with loops in C
- Introduction to C Arrays
- How to determine the length of an array in C
- Introduction to C Strings
- How to find the length of a string in C
- Introduction to C Pointers
- Looping through an array with C
- Booleans in C
- Introduction to C Functions
- How to use NULL in C
- Basic I/O concepts in C
- Double quotes vs single quotes in C
- How to return a string from a C function
- How to solve the implicitly declaring library function warning in C
- How to check a character value in C
- How to print the percentage character using `printf()` in C
- C conversion specifiers and modifiers
- How to access the command line parameters in C
- Scope of variables in C
- Can you nest functions in C?
- Static variables in C
- C Global Variables
- The typedef keyword in C
- C Enumerated Types
- C Structures
- C Header Files
- The C Preprocessor