简体   繁体   中英

What is a program that doesn't return anything in C programming?

What is a program that doesn't return anything in C programming? Can somebody please explain this to me? I am a beginner and learning basics. and what does void function do in briefly please. I would appreciate the reply and I am really confused now.

Every C function specifies its return type. Specifying the return type as void is C's way of specifying that the function doesn't return a value (and presumably is meant to be called only for it side effects).

Some other languages (Pascal, for example) make this distinction by calling value-returning subroutines "functions" and non-value-returning subroutines "procedures".

Under a hosted implementation (basically a C implementation not targeting an embedded system), the main function is the program's entry point. The standard requires the return type of main to be int but allows implementations to specify other types. Any program that uses some other type is non-portable.

Normally, with main defined to return int , the result returned by main is given to the calling environment to communicate some information, such as whether the program succeeded or failed. As a special case that applies only to the main function, failing to explicitly return a value is equivalent to returning 0 , which denotes successful termination.

Some implementations will allow you to define main with a return type of void , but doing so makes your program non-portable, and there is no good reason to do so. Personally, I use void main() as a filter for C books and tutorials. Any author who suggests void main() without discussion implementation-defined behavior or freestanding implementations does not know C well enough to be teaching it.

A void function doesn't return any value. Likewise, you cannot use void function calls to initialize variables.

void SayHello(void) {
    puts("Hello");
}

So if you try this,

int x = SayHello();

It will throw an error. Because the function call doesn't evaluate to anything. Void functions are used for performing tasks, that do not output a value.

On the other hand, take a function with a return value:

int Sum(int x, int y) {
    return x+y;
}

A call to that function is an expression that evaluates to the value that was returned from the function. For example, Sum(10, 1) is an expression that evaluates to 11, the value returned from the function.

A void function is a function that doesn't return anything. But you can still print stuff or modify

for example:

int sum(x, y)
{
    return x+y;
}

You can then assign the value returned to a variable like:

int answer = sum(3,2);

In a void function you can for example print stuff

void printnumber(int x) 
{
    printf("%d", x);
}

This will print out the x but won't return a value You can't do something like:

int answer = printnumber(7);

It just doesn't make sense!

Some functions return values, some functions produce outputs, and still some functions access memory and make changes.

The C programming language allows us to decide what value (if any) a function returns. For instance:

The C library function size_t strlen(const char *string) (found in <string.h> ) returns the number of characters in a given string.

The C library function void* malloc(size_t size) (found in <stdlib.h> ) returns a void* that points to a valid memory location at least size bytes large.

The C library function int fclose(FILE *filePointer) (found in <stdio.h> ) returns 0 if the file pointed to by filePointer was successfully closed, and returns another value if an error occurs.

The C library function void free(void *pointer) (found in <stdlib.h> ) frees the memory pointed to by pointer and returns nothing at all

When you write a function you should decide if it makes most sense for your function to return something useful, which might be a useful piece of data or a notice that everything went smoothly, or maybe it should return nothing at all. This is up to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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