Thread: Function-doubt

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    7

    Function-doubt

    Hello, this wierd question just came to my mind.

    Why isnt it possible to call a function which takes no arguments with a function call as argument which does not return any value (which imho is equivalent to calling a function which takes no arguments with no arguments).

    f.e.
    Code:
    void foo(void) {...}
    void bar(void) {...}
    
    foo(bar())
    Dont get me wrong, i know void is not a value and that it can not be treated like one.

    With my logic it would make sense and it should be possible to do that. I mean, why not? Any argument why that should not be possible?

    Let me hear your opinions about this.

  2. #2
    Registered User
    Join Date
    Jul 2008
    Posts
    38
    Specifying void in a functions argument list does not make it argument for that function, it only says that the function takes no arguments. Thats all.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    7
    florian thankz for your reply,
    but can you please elaborate,
    I mean bar() return nothing then doesn,t it mean that foor function expect nothing on invoking
    through function call foo(bar())

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You'll have compile-time error for foo(bar()) since foo() does not take any parameter and bar() returns nothing. lol!

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    7
    but compiler can check through function prototype that bar() will return nothing means
    foo(bar()) shld be equivalent to foo()..????????

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Have you tried to compile it?
    gcc foobar.c
    foobar.c::error: too many arguments to function ‘foo’

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    7
    i think i am bit confuse about void and null
    well i have checked thats why i am asking here that why compiler can,t check or how it interpret
    that if boo function return nothing , then why it can,t use for a function who accept no argument
    BTW thankz

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    Quote Originally Posted by fluteofliar View Post
    i think i am bit confuse about void and null
    NULL == 0. Think of it this way:

    #define NULL 0

    After running this code:

    int var;
    var = NULL;

    "var" will contain "0".

    void == no variable exists in the first place.

    It makes more sense to declare a function as func() rather than func(void).

    Quote Originally Posted by fluteofliar View Post
    that if boo function return nothing , then why it can,t use for a function who accept no argument
    1) Because it doesn't make any sense to allow it.

    2) The compiler looks for things between the ()s before checking their type.
    Last edited by MTK; 06-07-2010 at 08:07 AM.

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    int var;
    var = NULL;
    It's very poor practice. Use NULL only for pointer.
    NULL may be defined as (void*)0.

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    Quote Originally Posted by Bayint Naung View Post
    Code:
    int var;
    var = NULL;
    It's very poor practice. Use NULL only for pointer.
    NULL may be defined as (void*)0.
    I know, and I would never use NULL for anything but pointers.

    I was just trying to explain it to the original poster that the concepts of "NULL" and "void" are completely unrelated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive function
    By WatchTower in forum C Programming
    Replies: 11
    Last Post: 07-15-2009, 07:42 AM
  2. Replies: 6
    Last Post: 07-21-2008, 06:04 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM

Tags for this Thread