calling function in print

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Time
    New Member
    • Jan 2010
    • 77

    calling function in print

    Hi,
    In C,C++ and Java, while using printf()/cout/System.out.prin t()... if we call a function which returns some value, no error is generated; but if we call void type function; it gives an error.
    So, when it calls a function which returns some value; how does it accept it?
    I mean there is no variable; meaning no memory location for the returning value to get stored..and its not just related to calling a function as void type is not allowed..
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    I wont be able to tell you the detail but this is something like that.

    if a function return type is void, you simply cant assign to variable;
    as example
    void display()
    {
    ...
    }


    what ever display code content you cant do this
    int x=display();
    char c = display();


    simply compiler wont let you do this.

    as well as

    if you try this
    printf("whateve r it is", display());
    compiler wont let you do this. Cause compiler will look for the return type of display, when it will found that display return nothing it will generate an error. I dont know the adject detail of how C call a function

    I have had little knowledge. it first push all the variable from left to right to the stack. Then call the function. in case of function calling in other function as parameter, as example
    printf("whateve r it is", display());

    it will first call display then push it to the return value to stack. But if it does not have any defined type compiler wont even try to do that. Just let you know that you are wrong.

    I cant recall where I got this information. May be from MSDN. If you want to know more about function calling standard. go to msdn.microsfot. com (or something like that) and look for function calling standard PASCAL and __stdcall, __cdecl.

    Follow the link http://unixwiz.net/techtips/win32-callconv.html to get a basic knowledge about calling convention(I just have googled it)

    Let me know if there is any misleading information

    Regards,
    Johny

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      This has nothing to do with printf -- it would happen for any function. If you have a functoin call that looks something like this:
      Code:
      foo(a b, c, func(), e, f);
      It means,
      • function foo takes 6 arguments. Presumably there is a prototype for foo that speciifes the types of all those arguments.
      • The return value of func is passed as the 4th argument to foo. Again, presumably there is a prototype for func that specifies the type of the return value.

      If the prototype for func specifies that there is no return value then the compiler realizes it is being asked to do the impossible -- pass a nonexistent return value to foo. This isn't possible, so the compiler issues an error.

      If there were no prototype for func, then the compiler would assume the function returns an int and would generate executable code accordingly. The program wouldn't work properly, but there would be no compiler error because the compiler would be unaware of the problem.

      Comment

      Working...