Why do you have to return a value when writing a function?

Updated on technology 2024-03-30
8 answers
  1. Anonymous users2024-02-07

    If the main function does not end with a return statement, C specifies that the compiler will automatically add a return to the generated object file to indicate that the program exits normally. However, I would suggest that you add a return statement to the end of the main function, about void main In C and C++, the prototype of the function that does not receive any arguments and does not return any information is "void foo(void); Probably because of this, many people mistakenly think that if the program doesn't need to return any information, they can define the main function as void main(void). However, this is wrong!

    The return value of the main function should be defined as an int type, as is specified in both the C and C++ standards. While in some compilers, void main can be compiled (e.g. vc6), not all compilers support void main because void main is never defined in the standard. In g++, if the return value of the main function is not of type int, it will not compile at all.

    and a warning will be issued. So, if you want your program to be very portable, be sure to use int main. 4.

    What the return value does The return value of the main function is used to describe the exit state of the program. If it returns 0, it means that the program exits normally, otherwise it means that the program exits abnormally. Let's do a little experiment in the WinXP environment.

    First compile the following program: int main( void ) Then open the "Command Prompt" in the attachment, run the executable file you just compiled in the command line, and then enter "echo %errorlevel%", press Enter, and you can see that the return value of the program is 0. Assuming that the file you just compiled is , if you enter "a &&dir", the folders and files in the current directory will be listed.

    However, if you change it to "return -1", or some other non-0 value, and enter "a &&dir" after recompilation, dir will not be executed. Because the meaning of && is: if the program before && exits normally, then continue to execute the program after &&, otherwise it will not be executed.

    In other words, we can use the return value of the program to control whether or not to execute the next program. That's the benefit of int main. If you're interested, you can also change the return value type of the main function to a non-int type (e.g. float), recompile and execute "a &&dir", see what happens, and think about why it happens.

    By the way, if you enter a ||dir, it means that if A exits abnormally, dir will be executed.

  2. Anonymous users2024-02-06

    Not all of them have a return value.

  3. Anonymous users2024-02-05

    The function return value means that the result of the function implementation is returned to the caller.

    As a standard rule, the main function of c needs to have a return value. In the standard, the return type of the main function is int, so a return value is required. But due to historical reasons, there are many places where you can see main without a return value.

    For example, direct main() or void main() But this general compiler will not think that it is wrong, but it is recommended to write the standard c main function, preferably with a return value, 0 means normal exit.

  4. Anonymous users2024-02-04

    The return value is the result calculated by the program, and it is also the final result returned by the function implementation.

    Functions (methods) are generally divided into those with a return value and those without a return value, with no return value and no return value for the function that ultimately does not return the result, and if there is a return value, the calculation result of the function is finally returned.

  5. Anonymous users2024-02-03

    All functions don't necessarily have input parameters and return worth. First of all, not all functions must have parameters to be calculated, and secondly, not all functions are used for calculation, and function formulas can also be used for finding, extracting, sorting, changing formats and other functions. Function formulas all have parameters, but they are not necessarily necessary parameters, which can be omitted, or may be regarded as default parameters.

    Return worth working principle

    The main tunal function will be returned after the moderated function is run, but the memory space allocated by the system for the local variables in the moderated function will be released after the moderated function is run. In other words, the value returned by return is released as soon as the modulated function runs at the end of the run, so how is it returned to the main tonal function?

    In fact, when executing the return statement, the system automatically creates a temporary variable internally, and then assigns the value to the temporary variable to which the return is to be returned.

    Therefore, when the modulated function is finished, the return value behind it is really released, and finally the value is returned to the tonal function through this temporary variable. And the type of return value specified when defining the function is actually the type of the temporary variable specified.

    This is why when the type of the expression in the return statement is different from the return value type of the function, the return type is converted to the function return value type. The return statement actually assigns the following value to the temporary variable, so it should be based on the type of the temporary variable, that is, the type of the return value of the function.

  6. Anonymous users2024-02-02

    A function return is a return value, which is the result of a function. The reason for the return is that if you need to get the processing result of the function in the program for further processing, the function must have a return value. The return value of a function is defined by return.

    The format is: return value.

    1. return is a sign of the end of a function, there can be multiple returns in the function, but as long as it is executed once, the whole function will end running.

    2. If the return value of return is not typed, it can be any data type.

    3. There is no limit to the number of return values of return, that is, multiple values of any type can be separated by commas.

  7. Anonymous users2024-02-01

    Hello dear. 1. The form of function invocation can be divided into three ways according to the form of function invocation: 1

    For example, printf star(); 2.function expression c=2*max(a,b); 3.

    function parameter printf("%d",max(a,b)); m=max(a,max(b,c));where max(b,c) is a function call, and its value is used as an argument to max for another call. After the assignment, the value of m is the largest of a, b, and c. 2. Data transfer when the function is calledWhen the parametric function is called, there is a data transfer relationship between the main tone function and the modulated function.

    When defining a function, the variable name in parentheses after the function name is either a formal parameter (or a virtual parameter). When a function is called in a major function function, the arguments in parentheses after the function name are referred to as "actual parameters" (abbreviated)."Actual parameters")。Actual parameters can be constants, variables, or expressions.

    In the process of invoking a function, the data transfer between the real and form parameters that occurs is often called "virtual and real combination". 3. The return value of the function The return value of the function is obtained by the return statement in the function. For functions without return values, the function should be defined as a "void type" (or "null type"), and the return statement must not appear in the function body.

  8. Anonymous users2024-01-31

    The function name of a function is both a representation of the function and a variable. Since the function name variable is usually used to bring the processing result data of the function back to the calling function, that is, the recursive call, the function name variable is generally called the return value.

    Functions can have a return value or no return value. For functions without a return value, the function only completes an operation, the return value type should be defined as void, and there can be no return statement in the function body, and one can be placed there when it needs to exit at the specified position of the program.

Related questions
10 answers2024-03-30

The difference between the north and the south should be the main factor, after all, there are too many differences in living habits and customs. In addition, I met you on the Internet, and you are older than others. Breaking up is not the only solution, it depends on what role your man and woman play in it, whether he is on the same side of the same line, and whether he is on both sides of the conflict when you have a conflict with his family. >>>More

14 answers2024-03-30

The image of a primary function is a skewed straight line. >>>More

6 answers2024-03-30

If your phone's keys are out of order, we recommend you:

1.If you cannot use the back button to go back to the previous interface or exit when running a program, it is recommended to click the home button to return to the standby interface and try to re-enter the program. >>>More

8 answers2024-03-30

In order to keep the mouth clean and prevent oral diseases. >>>More

24 answers2024-03-30

Not necessarily! The kind of phenomenon you mentioned, I feel that it should be adapted to the previous years. Now it's just the opposite, some highly educated college students are now having trouble finding jobs, because they have some theoretical knowledge but no practical work experience. >>>More