Void Function

Can you help me to use void? What is the advantages and disadvantages of void? How can I maximize this function? Thanks
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.
Can I use void inside the int main()?
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.
1
2
3
4
5
6
7
8
int main()
{
    int x,y,z;

void Operations(void)
{
    z = x + y;
}
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
Can you elaborate it more sir @ganado?
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
Thanks sir! I think I need to test another functions :)
Topic archived. No new replies allowed.