0
$\begingroup$

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?

$\endgroup$
1
$\begingroup$

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.

Further reading.

$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – David Richerby Dec 27 '17 at 18:49
0
$\begingroup$

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.