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.
An algorithm always outputs something because by definition it must solve some class of problems in finite space and time.