Thread: Void functions

  1. #1
    cap
    Guest

    Question Void functions

    I am working on functions and as I was reading it was going on about the different types of arguments and what kinds of variables the funtions will take and so, anyways it said that you can have something like:

    void functionName(void functionArg)

    Now I know that both wouldn't be void but what is the point of having a function that doesn't return a value?

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571

    Re: Void functions

    Originally posted by cap
    I am working on functions and as I was reading it was going on about the different types of arguments and what kinds of variables the funtions will take and so, anyways it said that you can have something like:

    void functionName(void functionArg)

    Now I know that both wouldn't be void but what is the point of having a function that doesn't return a value?
    What is the point? Good question. I am a strong advicator of always returning a value on a function. Some cases though there simply isn't a need for it. An example would be some simple math calculations. I always try to return values if it is necessary but sometimes you can skip it. As for if a function doesn't take any parameters... ( takes nothing but returns an integer ) It does just that, doesn't take anything so you would call it like MyFunction();

    /* Example of function not taking anything but returning an int */
    int MyFunction( void );

    Does that clear it up? Or still some questions?
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    They can still have a side effect, it's not as though they have no effect on the state of the program. Did you learn programming with a functional langauge? Or is C your first langauge?
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    void functionName(void functionArg)
    No, you WON"T see that! Ok. "void" means nada, nothing. So you will never have a "nothing" functionArg, if you know what i mean.
    "void*" is quite different. That is an 'undefined/undetermined/typeless' pointer. More on that in a moment. Look at these:

    void functionName(void);

    This is typically used for menus, global initializations, clarity(ha!), and so forth. Generally undesirable, avoid whenever possible.

    int functionName(void);

    This is generally used to grab global values, so forth. Avoid unless neccesary.

    void functionName(int *x);

    Generally used to change the value of some variable for some reason, etc. Sometimes useful.

    void functionName(int *x, int y);

    Generally used to change the value of x by y. Useful, common.

    int functionName(int x, int y);

    Used to return calculations of x with y, etc. Useful, common.

    The void* is like a blob that is molded (indirectly) at some later time. Take malloc() for instance. It returns a void* pointer, which can be cast into any type of pointer.
    In earlier times, the void* was the only way to effectively overload a function. This pointer type is used more often in C than in C++, simply because you CAN overload a function in C++. Needless to say, void* pointers can be tricky to work with. Anyway, I hope I haven't confused things for you
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    what is the point of having a function that doesn't return a value?
    Because, simply put, this is NOT a function. it is PROCEDURE. Procedures don't return values, FUNCTIONS do.

    Once you're done more coding you'll understand why.
    It is not the spoon that bends, it is you who bends around the spoon.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Once you're done more coding you'll understand why.
    You mean once you've done some Pascal. In C, there is no such thing as a proceedure. Pascal differentiates between the two, and has two implicitly different names for each. In C, they are both called functions. No? Find me a C programming book that calls them proceedures and differentiates between the two. Does K&R refer to them as proceedures? I doubt it.

    The technical term may be 'proceedure', but in C, everything that is a function is called a function. C does not have the term 'proceedure' in, thatI have ever seen, its definition. I have yet to see a [C] book that specificly states that they are different.

    but what is the point of having a function that doesn't return a value?
    Well with the rate people use globals here, I don't really ever see any point in actually returning anything...

    Quzah.
    Last edited by quzah; 08-23-2002 at 02:13 PM.
    Hope is the first step on the road to disappointment.

  7. #7
    CAP
    Guest
    That somewhat clears things up, maybe I will get the idea a little more when I really get into programming. C is my first language(I have done HTML and Javascript just to play around and get a feel for programming), I haven't ever done anything in Pascal. I was only asking because I couldn't see the point of having a function that doesn't return anything. I simply couldn't see why anyone would do that really, unless the function did something and then something else in the program used that data or information...but wouldn't that be returning somethig?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, you don't have to return anything to manipulate exterior data. This is the fun of pointers.

    When you pass an argument to a function, you pass one of two things:
    a) A copy of the data.
    b) The location of the actual data.

    The first is called passing by value.
    The second is called passing by reference.

    The first, you pass a copy of the data, like so:

    Code:
    #include <stdio.h>
    void fun1( int x )
    {
        printf("fun1 - x is %d", x );
        x++;
        printf("fun1 - x is %d", x );
    }
    int main( void )
    {
        int var;
    
        var = 10;
        printf("main - var is %d", var );
        fun1( var );
        printf("fun1 - var is %d", var );
    
        return 0;
    }
    Here, what happens is that a copy of the value of 'var' is put into the variable x in 'fun1'. As such, when the function is done, the original value in 'var' isn't ever changed.

    Now look what happens when we use a pointer to point at the real data rather than a copy of it:

    Code:
    #include <stdio.h>
    void fun1( int *x )
    {
        printf("fun1 - x is %d\n", *x );
        (*x)++;
        printf("fun1 - x is %d\n", *x );
    }
    int main( void )
    {
        int var;
    
        var = 10;
        printf("main - var is %d\n", var );
        fun1( &var );
        printf("fun1 - var is %d\n", var );
    
        return 0;
    }
    Here we pass the address of 'var' to the function so that whatever happens in 'fun1' actually effects the real variable 'var'.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Cap
    Guest
    Alright that is kinda cool(I don't know pointers yet )so it doesn't make much sense with that whole something * something else, that confuses me, although I do get most of the functions, I will probably come back here after I read a bit more on that chapter, thanks a lot

  10. #10
    CAP
    Guest
    One more thing I would like to add(My registry is so screwed up it isn't even funny so I can't log in at all anymore , that was just to explain the double post)anyways I know that now you should always do:
    int main(void, or whatever)

    but why do you have:
    void fun1( int *x )?
    Shouldn't you put a variable type in front of user created functions...or is that different somehow?

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Functions take the following:

    <return type> <function name> ( <parameters> )


    Thus:

    int myfunction( int x )


    This function returns an integer.
    This function is called 'myfunction'.
    This function takes one integer as an argument.

    Thus:

    void myfunction( void )

    This function doesn't return anything.
    This function is called 'myfunction'.
    This function doesn't take any arguments.

    You use the keyword 'void' when you don't want to have parameters for your function, or when you don't want it to return anything.

    And finally:

    void fun1( int *x )


    This function doesn't return anything.
    This function is called 'fun1'.
    This function takes one argument, which is a pointer to an integer.

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    WhoCaresAnymore
    Guest
    Ok thats cool, I will have to play around with some other functions but thanks.

  13. #13
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    You mean once you've done some Pascal. In C, there is no such thing as a proceedure.
    Quzah, you're missing the point (again). Stop talking on the darn phone! I mean, really!

    The concept (that's the keyword here) of procedures and functions did not start with Pascal-- they were derived from mathematics and applied to _ALL_ programming languages. By definition procedures perform work without return a result. Functions on the other hand return a result.

    Instead of talking on the phone, you need to spend more time in the book store, the library, somewhere that will help you expand your mind.
    It is not the spoon that bends, it is you who bends around the spoon.

  14. #14
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    if you want to get technical, you can state that all functions return a value, regardless of whether they're declared void or int.

    returned integers are returned through the register eax on i386+ machines. register eax always has a value in it, therefore it always returns something. the difference between an int function and a void function is merely the setting of the eax register to a value before that final 'ret' command.

    if the c compiler didn't enforce the inability to assign integers to void functions, you would likely get an undefined result from it.

  15. #15
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by ygfperson
    if you want to get technical, you can state that all functions return a value, regardless of whether they're declared void or int.

    returned integers are returned through the register eax on i386+ machines. register eax always has a value in it, therefore it always returns something. the difference between an int function and a void function is merely the setting of the eax register to a value before that final 'ret' command.
    system specific.
    hello, internet!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. saying hello and 1st question
    By darksys in forum C Programming
    Replies: 12
    Last Post: 10-31-2008, 02:58 PM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM