"Void function does not return any value "- What does this actually mean? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 56

"Void function does not return any value "- What does this actually mean?

7th May 2018, 12:00 PM
The Princess Diary 2001
The Princess Diary 2001 - avatar
60 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
13th May 2018, 1:20 PM
Rajeeb
+ 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.
7th May 2018, 12:03 PM
Rusty.Metal
+ 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! 😀
10th May 2018, 5:06 PM
🐙evil octopus
🐙evil octopus - avatar
+ 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 ()
10th May 2018, 7:00 PM
AZTECCO
AZTECCO - avatar
+ 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.
10th May 2018, 4:16 PM
The Princess Diary 2001
The Princess Diary 2001 - avatar
+ 14
thanks to all for answer & upvote my question 😊
10th May 2018, 3:52 PM
The Princess Diary 2001
The Princess Diary 2001 - avatar
+ 11
pmsl
10th May 2018, 6:52 PM
AZTECCO
AZTECCO - avatar
+ 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
10th May 2018, 3:35 PM
Yash✳️
Yash✳️ - avatar
+ 9
"Void (NonValue-Returning) Functions" http://www.cs.fsu.edu/~cop3014p/lectures/ch7/index.html
7th May 2018, 12:04 PM
Rahul George
Rahul George - avatar
+ 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.
10th May 2018, 3:22 PM
Nommer101
Nommer101 - avatar
+ 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 😃😃😃😃😃😃😃😃😃
11th May 2018, 3:28 AM
Deepak Kumar
Deepak Kumar - avatar
+ 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 ...
11th May 2018, 12:06 PM
{ sood(Hemant , Prakash); }
{ sood(Hemant , Prakash); } - avatar
+ 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 .
12th May 2018, 6:38 AM
Ananya Srivastava
Ananya Srivastava - avatar
+ 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;
10th May 2018, 4:01 PM
Yash Nirmal
Yash Nirmal - avatar
+ 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-----------; }
10th May 2018, 4:02 PM
D-Key
D-Key - avatar
+ 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; }
10th May 2018, 8:22 PM
Nikhil Gaurav
Nikhil Gaurav - avatar
+ 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); }
10th May 2018, 4:12 PM
D-Key
D-Key - avatar
+ 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
11th May 2018, 1:07 AM
Sugiyanto Goutama
Sugiyanto Goutama - avatar
+ 5
void means it has nothing in it if you have nothing how can you return
11th May 2018, 3:04 AM
Mani Tiwari
Mani Tiwari - avatar
+ 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.
11th May 2018, 5:23 AM
Sanju Jose
Sanju Jose - avatar