Mar 7, 2015 at 1:56pm UTC
Can you help me to use void? What is the advantages and disadvantages of void? How can I maximize this function? Thanks
Mar 7, 2015 at 2:22pm UTC
Functions are ways to organize code into particular tasks. A void function is no different, but doesn't return a value to where it's called from. It might do other useful things like set internal states of a class, or display information to the user.
Mar 7, 2015 at 2:48pm UTC
Can I use void inside the int main()?
Mar 7, 2015 at 3:16pm UTC
Depends on what you mean by use, you can call any function you want in main. Show an example of what you're trying to do.
Mar 7, 2015 at 3:30pm UTC
Your indentation suggests that you're missing a closing curly brace on line 4..
If, instead, you having Operations be declared
inside main, no that is not allowed.
You can't define functions inside other functions.
As in, this is illegal:
1 2 3 4 5 6 7 8
int main()
{
int another_func() // not allowed
{
return 5;
}
return another_func();
}
Last edited on Mar 7, 2015 at 3:35pm UTC
Mar 7, 2015 at 3:35pm UTC
Can you elaborate it more sir @ganado?
Mar 7, 2015 at 3:42pm UTC
Your previous example is ambiguous with its lack of braces/indentation.
Whether it's void or not doesn't matter, either way you cannot define functions within other functions.
Edit:
To use your example:
1 2 3 4 5 6 7 8
int main()
{
int x,y,z;
void Operations(void )
{
z = x + y;
}
}
is not allowed, you'd have to do something like
1 2 3 4 5 6 7 8 9 10 11
int Operations(int x, int y)
{
return x + y;
}
int main()
{
int x, y, z;
x = 1;
y = 2;
z = Operations(x, y);
}
Last edited on Mar 7, 2015 at 4:06pm UTC
Mar 7, 2015 at 10:31pm UTC
Thanks sir! I think I need to test another functions :)