简体   繁体   English

C - void 函数(void)、参数和返回值

[英]C - void function(void), parameters and return values

I was given a large C-file containing several functions declared as follows:我得到了一个包含几个函数的大型 C 文件,声明如下:

void function_a(void);

The functions are written above the main program:函数写在主程序之上:

static void function_a(void) { .... }

Within the main program these functions are called as:在主程序中,这些函数被称为:

function_a();

Now as far as I know a function declared as written above does neither use parameters, nor have return values.现在据我所知,上面所声明的函数既不使用参数,也不具有返回值。 However within these functions variables and arrays are used, which are not defined within these functions, but only in the main program.然而,在这些函数中使用了变量和数组,它们不是在这些函数中定义的,而是仅在主程序中定义的。

Is this really correct C-Syntax?这真的是正确的 C 语法吗? How can the functions access data from the main program, if it is not handed over as a parameter?如果不作为参数传递,函数如何从主程序访问数据?

In addition the main program uses variables, which are calculated within the functions it calls.此外,主程序使用变量,这些变量是在它调用的函数中计算的。

Can you provide more context, please?你能提供更多的背景吗? In C you cannot access variables from another function but you can access global ones.在 C 中,您不能从另一个函数访问变量,但可以访问全局变量。

The following program is valid and will output 3, since i is a global variable and is visible everywhere.下面的程序是有效的,将输出 3,因为i是一个全局变量并且在任何地方都可见。

#include <stdio.h>

int i = 2;

void plusone() {
        i = i + 1;
}

int main() {
        plusone();
        printf("i = %d\n", i);
}

On the other side, the program below won't compile because i is local to main() function and is invisible elsewhere.另一方面,下面的程序将无法编译,因为imain()函数的本地函数,并且在其他地方不可见。

#include <stdio.h>

void plusone() {
        i = i + 1;
}

int main() {
        int i = 2;
        plusone();
        printf("i = %d\n", i);
}

Said that, usage of global variables is a bad practice and should be avoided.也就是说,使用全局变量是一种不好的做法,应该避免。

So this:所以这:

//file_a.c
void function_a(void)
{
    //...
}

Makes function_a a function that does not take any arguments and does not return any values.使function_a成为一个不接受任何参数且不返回任何值的函数。 This function may be used by all other functions in the same file function_a was declared or in any other .c file that you tell the compiler to put together into the final program.此功能可以由所有其他功能一起使用同一个文件function_a被宣布或以任何其他.c文件,你告诉编译器放在一起进入最后的程序。

In other words, this function is accessible to all translation units of your program.换句话说,您程序的所有翻译单元都可以访问此功能。 If you have this function in a file called file_a.c , and you also have another file called file_z.c you can do this:如果您在名为file_a.c的文件中有此函数,并且您还有另一个名为file_z.c文件,则可以执行以下操作:

//file_z.c
void function_z(void)
{
    function_a();
}

And all is fine.一切都很好。

On the other hand, this另一方面,这

//file_b.c
static void function_b(void)
{
    //...
}

(and I renamed the function for clarity) (为了清楚起见,我重命名了该函数)

Makes function_b a function that does not take any arguments and does not return any values, just like function_a .使function_b成为一个不接受任何参数且不返回任何值的function_a ,就像function_a一样。 However, it also says that function_b has static-file scope in the translation unit that begins or includes file_b.c .但是,它还表示function_b在开始或包含file_b.c的翻译单元中具有静态文件范围。 That means it cannot be accessed by other translation units.这意味着它不能被其他翻译单元访问。

So if you now tried to do this in file_z.c :因此,如果您现在尝试在file_z.c执行此file_z.c

void function_z(void)
{
    function_b();
}

You would get compilation errors:你会得到编译错误:

file_z.c:(.text+0xa): undefined reference to `function_b'
collect2: error: ld returned 1 exit status

Because each file_{a,b,z}.c is (or probably should be) the starting point of their own separate translation units, and function_b was declared with static-file scope in file_b.c , this function simply "does not exist" for other translation units.因为每个file_{a,b,z}.c是(或者可能应该是)它们自己独立翻译单元的起点,并且function_bfile_b.c声明为静态文件范围, file_b.c这个函数根本“不存在" 用于其他翻译单元。

Now, as to the variables declared just before function_b , I take they look somewhat like this:现在,对于在function_b之前声明的变量,我认为它们看起来有点像这样:

//file_a.c
int array[10];
void function_a(void)
{
    //...
}

That simply declares a global variable.这只是声明了一个全局变量。 It can be accessed by all functions in file_a.c , provided that they appear after the declaration.它可以被file_a.c的所有函数访问,前提是它们出现声明之后。 It can also be used by other translation units (like file_b.c or file_z.c ) if you declare it like this:如果您像这样声明它,它也可以被其他翻译单元(如file_b.cfile_z.c )使用:

//file_b.c
extern int array[10];

When you compile everything togheter, that declaration in the translation unit that starts with file_b.c will tell the compiler that array is not a file-scope variable within that file_b.c ;当您将所有内容编译在一起时,翻译单元中以file_b.c开头的file_b.c将告诉编译器array不是该file_b.c的文件范围变量; rather the compiler will have to look for that variable in another translation unit, and then link them together so that all functions do the same thing to that block of 10 integers.相反,编译器必须在另一个翻译单元中查找该变量,然后将它们链接在一起,以便所有函数对 10 个整数块执行相同的操作。

In C it is not at all possible to access local variables of another function or even scope.在 C 中,根本不可能访问另一个函数甚至作用域的局部变量。 Is the code compiling correctly?代码编译正确吗? If yes these variables must be global defined either in the beginning or some header file.如果是,这些变量必须在开头或某个头文件中全局定义。 If not make them global to access from non-parametric functions.如果不是让它们全局访问非参数函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM