# Do algorithms always have a return 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?

• In C and C++, the return type of the main function is not void but int. – gnasher729 Oct 22 at 13:52

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.

• I fully agree with your first and third paragraphs but I'm afraid your second paragraph is just wrong. You're arguing that 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
• @DavidRicherby It's hard to say. If you look at compiled C codes, e.g. on 8086 processors, there's simply no return value. It's the calling convention that specifies e.g. 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 at 10:59
• Many languages specify that a 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 at 11:49

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).

• Rather than putting "[sic]" in quoted text in your answer, you could just edit the question to remove the grammatical mistakes. – David Richerby Dec 27 '17 at 18:51
• @DavidRicherby I have tried to edit it, but my peer review is pending. I did it after you suggested. – Ubi hatt Dec 30 '17 at 2:51

An algorithm always outputs something because by definition it must solve some class of problems in finite space and time.