+ 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?
28 Antworten
+ 4
This works on code playground.
https://code.sololearn.com/cL0EJOOb0b6J/?ref=app
but Sololearn probably doesn't show compilation warnings.
+ 3
~ swim ~
Pointer and reference what is difference in this context?
Both deal with addresses content monipulations..... Not simply content or data..
+ 2
Where your proof :) code(link)?
+ 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..
+ 1
In void, it doesn't return anything.
May be you write code by call reference..
Can you show the code...?
+ 1
The program you wrote does show a warning as "return with a value, in a fuction returning void"
+ 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.
+ 1
~ swim ~ OK. I understood. But in C,
void fun(&a, &b);
Is this also pass by pointers?
Edited
+ 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?
+ 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..
+ 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...
+ 1
It might be a WARNING from compiler but fine
And not fulfill you purpose too.
+ 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....
+ 1
Error
0
This is the code I have written where I get only warning not an error
0
In void function we can't return any value
0
I am new