I learned that algorithms written in programming languages always return some value, not necessarily an input value.
But, if the return type of the main function is void, then does the algorithm return any value?
I learned that algorithms written in programming languages always return some value, not necessarily an input value.
But, if the return type of the main function is void, then does the algorithm return any value?
Actually, you have asked three different questions: 1) [Does algorithm always have a return value?[sic]"
The algorithm by default is not a function/method (or to say program), but it could be. Sticking to the stringent definition of the algorithm. An algorithm is a set of instruction for accomplishing a task. Every piece of code could be called an algorithm e.g. 1) defining a variable or 2) writing a logic for a loop is also an algorithm. So, addressing to your original question "Does algorithm always have a return value?" answer is 'NO'. In similar situations like 1), and 2) an algorithm does not return a value.
2) "I learned that the algorithm written in programming langauge always have a return value, not necessarily an input value.[sic]"
If you wish to convert/write your pseudo code to a specific program in some programming language e.g. C/C++. Then every program should consist of at least one function. Programming languages like C/C++/JAVA/C# etc., provides default function call Main() in which you can call user defined function(s) (or main itself).
So, reaching the end of a function other than main is equivalent to return;. reaching the end of the main function is equivalent to return 0;. reaching the end of any other value-returning function is undefined behavior, but only if the result of the function is used in an expression.
In certain languages like C/C++ there are features which allows function to defined as 'no-return' function.
#include <stdlib.h> #include <stdio.h> #include <stdnoreturn.h>
noreturn void stop_now(int i) { if (i > 0) exit(i); } int main(void) { puts("Preparing to stop..."); stop_now(2); puts("This code is never executed."); }
The 'noreturn' keyword appears in a function declaration and specifies that the function does not return by executing the return statement or by reaching the end of the function body. This type of adventure could be costly and can cause undefined behaviour to your C/C++ program.
P.S. When function/method have return type as 'void' they do not return any value, or anything. Control simply returns. It is also important to learn that void doesn't mean nothing. void is a type to represent nothing. That is a subtle difference : the representation is still required, even though it represents nothing.
3) "But it we use the main function with void return value, does that algorithm have a return value?[sic]"
P.S. If you define return type of main function as 'void' then it does not have any return value. You can actually omite 'return' keyword or simply type 'return' with no expression (or even 0).
An algorithm is not the same as it's implementation in some language.
Most languages have some sort of null
-type. Java and C++, for instance, have a void
type, and Python has a none
type. Returning void
or none
is returning something, because both void
and none
are a part of the language, so yes, even a program which returns null
has returned something.
To answer your other question, an algorithm always outputs something because by definition it must solve some class of problems in finite space and time.
void
/null
functions return "a nothing", if you will: a datum that has type void
or null
. This is incorrect: they return nothing at all. No datum has type void
or null
. These are just keywords meaning "there is no return value": look at the assembler output and you'll see that nothing is returned. languages such as Pascal achieve the same thing by having a keyword function
for functions that return a value and procedure
for functions that don't.
– David Richerby
Dec 27 '17 at 18:49
eax
is the return value. In that sense, a value is returned even when the callee doesn't set 'eax' before returning. The type void
means the value is meaningless.
– WhatsUp
Oct 22 '19 at 10:59
void
function returns nothing. Some languages say it returns a certain value (e.g. ()
in Haskell). Either way the practical effect is the same.
– user253751
Oct 23 '19 at 11:49
An algorithm always outputs something because by definition it must solve some class of problems in finite space and time.