+ 56
"Void function does not return any value "- What does this actually mean?
61 Answers
+ 6
Hello,
Normally when we create a function we try to get a value in return with ( return 0) to see if the function is terminating correctly.
some functions are predefined in the compiler
or programming language .( ex: return, void, int , char)
suppose we write a simple program in 'C' programming language with declaration. such as
void main() {
}
// here no value cannot be expected as return. because you have declared the function as NULL.
//as we know, this does not mean that the program will not run or terminate correctly, but we cannot write a return value at the end of the function ( because function already declared as NULL/ VOID)
suppose we write a program
with no value expected in return at the end of completion of a function.
it normally returns as void
So Void is predefined as NULL inside compiler and also void function does not take any arguments .
kindly refer to wikipedia article:-
https://en.m.wikipedia.org/wiki/Void_type
+ 43
When you call a function, that called function (which has a return type of 'void') doesn't return any value to the called function.
Ex:
int add (int a, int b) {
return a+b;
}
void add(int a, int b) {
cout << a+b;
}
in this example the above function adds the values and returns them as an integer to the calling function, whereas the later just adds and prints them.
+ 24
Snehal Jagdish Teke I think you need to understand what return types are before diving into that. in short, every function returns something. even 1 + 2 is a function. in most languages it is equivalent to 1.add(2) or add(1, 2), which returns the value of 1 + 2. in this case, the add() function returns an integer, 3.
when you add strings, e.g. "Hi" + " there", the + represents a function which returns a string that consists of the first string concatenated with the second, in this case "Hi there".
when you declare a function, you always need to declare the return type (int, string, int*, etc.) and then the function itself. the following function will cause an error:
string Func() {
return 3;
}
this is because the return type of the function has to be a string, but you returned an int.
when you have a void function, it does not return anything. for example:
int add(int one, int two) {
return one + two;
}
int main() {
cout << add(1, 2);
return 0;
}
the function above outputs 3.
void fun(int a) {
string h = "hi";
}
int main() {
cout << fun(3);
return 0;
}
the function above, however, does not return a value, and will therefore output "null" or 0 (depending on the compiler).
I know this explanation is very messy but I hope it helps you understand a bit more about return types. good luck and happy coding! 😀
+ 16
a function has to return something..
void tells the compiler this function returns nothing.. void.
If you accidentally assign a void to a variable the compiler warns you.. thank you compiler and sorry, I'll take more care next time..
int func(){return 2;}
.....
void function(){....}
....
...
int var =0;
var =function();// error! maybe you meant var = func ()
+ 15
sharuk khan
calling function means...
A function is a group of statements that together perform a task. A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function. The C standard library provides numerous built-in functions that your program can call.
+ 14
thanks to all for answer & upvote my question 😊
+ 11
pmsl
+ 10
Example of usage of a function which returns a value in C++:
int sum(int a, int b){
return a + b;
}
cout<<"The sum of 5 and 7 is "<<sum(5, 7);
If the function was
void sum(int a, int b){
cout << a + b;
}
You can't do
cout<<"The sum of 5 and 7 is "<<sum(5, 7);
Because the function `sum` doesn't actually return a value but prints something to the console
+ 9
"Void (NonValue-Returning) Functions"
http://www.cs.fsu.edu/~cop3014p/lectures/ch7/index.html
+ 7
In strongly typed languages the compiler needs to know how to determine whether the result of a function is correct or not as well as the code receiving the result.
Therfore you have to declare a type for the function. However if you want to create a function that doesn't really return anything then you can use void. This basically tells the compiler not to expect anything to come from the function call.
+ 7
I think if we use void as return type in a function then we can not use the result of the function in the calling function.
for example:-
https://code.sololearn.com/cXVJIuCdkX5j/?ref=app
👆 program will show error because a void return type can not return a value
👇here is correct version of this program.
The only problem was of void which is replaced by int as return type of sum function
https://code.sololearn.com/c0dHKt1C9VUi/?ref=app
this program will show result 19
😃😃😃😃😃😃😃😃😃
+ 7
like in c++: int return type function is of 2 byte, float has 4 btye and so on..
But wait what the return type of void. it is zero or null return type ...
+ 7
You should first understand what are return types ,see when you call any function it works and generate a value if you want that final value to use some where else then you need to get that value and for that you need to return the value from the function using the appropriate return type. Whenever we don't need any value generated during function call we use void return type .
+ 6
void in a non value returning func...void can also called as a data type,,,it decides which type of function a coder declaring
and. void=return;
+ 6
Syntax:-
Type Function_Name (parameter1,parameter2,...,parameters_list)
{
All your codes here;
-----------------------------
-----------------------------
-----------------------------
}
//driver program
main()
{
line 1---------;. // call function in any line
line 2---------; // as per requirement.
.
Function_Name (return values if any);
.
.line n-----------;
}
+ 6
Whenever we write a method with void return type then the function doesn't return any value instead it only return the control from the called method back to the calling method.
If a function is a written with void as a return type then it's not mandatory to write the return statement, compiler will automatically write it implicitly or if we write it then compiler won't write it.
Public static void check(int num)
{
statements. .
return; // not needed here compiler write implicitly
}
Whereas if a function is declared with another primitive as a type then w should return the same type as we declared in the declaration.
Public static int check(int num)
{
statement. . .
return num;
}
+ 5
Example:- Without using void
#include <stdio.h>
//Function type integer of Sum
int Sum (int a, int b)
{
int Add;
Add= a+b;
printf("\n Sum=%d",Add);
}
int main()
{
int p,c;
scanf("%d %d",&p,&c);
Sum(p,c);
}
+ 5
void function doesn't returning a value, becuase void just only run the method inside directly...
void usually use for print some data in a class,, and it doesn't consume a lot of memory
+ 5
void means it has nothing in it
if you have nothing how can you return
+ 5
void is a type of data which doesnt return a value . it is because it doesnt have a specific data ,as in int you can store integer data and as in char you can store character data and you have to return integer value or return char valur respectively. But in void you dont have to return any value. you can write return ; or simply you dont have to write it.