What happens if we return a string from void function ? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 7

What happens if we return a string from void function ?

I tried a problem, I unfortunately returned a string from the void function but it didn't shown any error while compiling it. What can be the reason behind it?

6th Jan 2020, 5:29 PM
Rahul AS
Rahul AS - avatar
19 Réponses
+ 4
This works on code playground. https://code.sololearn.com/cL0EJOOb0b6J/?ref=app but Sololearn probably doesn't show compilation warnings.
8th Jan 2020, 4:35 PM
Sonic
Sonic - avatar
+ 3
~ swim ~ Pointer and reference what is difference in this context? Both deal with addresses content monipulations..... Not simply content or data..
7th Jan 2020, 8:36 AM
Jayakrishna 🇮🇳
+ 2
Where your proof :) code(link)?
6th Jan 2020, 5:49 PM
Abdol Hashimi
Abdol Hashimi - avatar
+ 2
~ swim ~ Yes. I am not telling that am correct.. I want to know the correct answer.. I have very little knowledge compare to you. So correct me if am wrong anywhere... I learned pass by value, reference. And I thought pass by reference = pass by pointer. I just also Google it now. I got varies answer but coming to array, " in C, Arrays are always pass by reference ". So just want the correct answer.. And I edited my answer according to your answer..
7th Jan 2020, 9:03 AM
Jayakrishna 🇮🇳
+ 1
In void, it doesn't return anything. May be you write code by call reference.. Can you show the code...?
6th Jan 2020, 5:49 PM
Jayakrishna 🇮🇳
+ 1
The program you wrote does show a warning as "return with a value, in a fuction returning void"
7th Jan 2020, 3:23 AM
Infinity
Infinity - avatar
+ 1
In your program, actually it does not returning anything. When a function return something then are able to store returned value like Newstr=hidepassword(str) ; Because of hidepassword is void, this statement gives error message in your program. In general, in C, Arrays are passby reference so that's why your program getting modified value of str from hidepassword function. Edit: read as pass by pointer instead of pass by reference.
7th Jan 2020, 8:24 AM
Jayakrishna 🇮🇳
+ 1
~ swim ~ OK. I understood. But in C, void fun(&a, &b); Is this also pass by pointers? Edited
7th Jan 2020, 11:02 AM
Jayakrishna 🇮🇳
+ 1
~ swim ~ oh mistakenly specified int in calling... I mean only fun(&a, &b) ; and it valid also... So also in function definition for this Apart from pointers, void fun(int a, int b) { a++, b++; }. Is this belong to what kind of?
7th Jan 2020, 11:25 AM
Jayakrishna 🇮🇳
+ 1
~ swim ~ I mean this, main() { int a=1,b=2; fun(&a, &b); //print } fun(int a, int b) { a++, b++; } I just in search of the differences, and a example..
7th Jan 2020, 11:40 AM
Jayakrishna 🇮🇳
+ 1
~ swim ~ OK. I am getting warnings in c, with no changes in value(1,2), but in c++, getting the expected values 2,3.. Ok.. . Thanks for all replies, and for the correction...
7th Jan 2020, 12:14 PM
Jayakrishna 🇮🇳
+ 1
It might be a WARNING from compiler but fine And not fulfill you purpose too.
8th Jan 2020, 8:17 AM
M Razaz 🇵🇰
M Razaz 🇵🇰 - avatar
+ 1
This is not point of concern and the reason is the void is means no value but it is only one which change it type when the type is different on its side but if you try to return string when the return should be int then it will return integer by doing any this....
8th Jan 2020, 9:33 AM
Abhishek nirwan
Abhishek nirwan - avatar
+ 1
Error
8th Jan 2020, 3:18 PM
Rushabh Futane
0
This is the code I have written where I get only warning not an error
6th Jan 2020, 6:24 PM
Rahul AS
Rahul AS - avatar
0
In void function we can't return any value
8th Jan 2020, 1:31 AM
surya
0
I am new
8th Jan 2020, 8:03 AM
Jawad Jisan
8th Jan 2020, 8:38 AM
Rushabh Futane