stuck

Discussion in 'C Programming' started by Bill Cunningham, Jan 17, 2011.

  1. I have these two functions one calls the other one. I have tried
    everything to debug them except a damn debugger. I have placed puts messages
    at all the return and I still get two numbers on my screen. What's wrong
    with this code? I will post the header, object file source and main() code.

    #include <stdio.h>

    double oper(char, double, double);
    double vop(double);

    Ok that's my declarations

    #include "core.h"

    double oper(char c, double x, double y)
    {
    if (c == 'a')
    return vop(x + y);
    else if (c == 's') {
    return vop(x - y);
    } else if (c == 'm') {
    return vop(x * y);
    } else if (c == 'd') {
    return vop(x / y);
    } else {
    fputs("usage error\n", stderr);
    return -1;
    }
    return 0;
    }

    double vop(double oper)
    {
    return printf("%.2f\n",oper);
    }

    Above here is the main code I compile in .o file. And this is the main
    function:

    #include "core.h"

    int main()
    {
    double a;
    a = oper('s', 5, 2);
    return vop(a);
    }

    Everything works like I want it to except I am getting two numbers. The
    second number is always the same whatever I pass to oper().

    Bill
    Bill Cunningham, Jan 17, 2011
    #1
    1. Advertising

  2. Bill Cunningham

    Ian Collins Guest

    On 01/18/11 08:12 AM, Bill Cunningham wrote:
    > I have these two functions one calls the other one. I have tried
    > everything to debug them except a damn debugger. I have placed puts messages
    > at all the return and I still get two numbers on my screen. What's wrong
    > with this code?


    What do you expect if you call printf twice?

    3/10.

    --
    Ian Collins
    Ian Collins, Jan 17, 2011
    #2
    1. Advertising

  3. Bill Cunningham

    steve Guest

    On Jan 17, 11:12 am, "Bill Cunningham" <> wrote:
    >     I have these two functions one calls the other one. I have tried
    > everything to debug them except a damn debugger. I have placed puts messages
    > at all the return and I still get two numbers on my screen. What's wrong
    > with this code? I will post the header, object file source and main() code.
    >
    > #include <stdio.h>
    >
    > double oper(char, double, double);
    > double vop(double);
    >
    > Ok that's my declarations
    >
    > #include "core.h"
    >
    > double oper(char c, double x, double y)
    > {
    >     if (c == 'a')
    >         return vop(x + y);
    >     else if (c == 's') {
    >         return vop(x - y);
    >     } else if (c == 'm') {
    >         return vop(x * y);
    >     } else if (c == 'd') {
    >         return vop(x / y);
    >     } else {
    >         fputs("usage error\n", stderr);
    >         return -1;
    >     }
    >     return 0;
    >
    > }
    >
    > double vop(double oper)
    > {
    >     return printf("%.2f\n",oper);
    >
    > }
    >
    > Above here is the main code I compile in .o file. And this is the main
    > function:
    >
    > #include "core.h"
    >
    > int main()
    > {
    >     double a;
    >     a = oper('s', 5, 2);
    >     return vop(a);
    >
    > }
    >
    > Everything works like I want it to except I am getting two numbers. The
    > second number is always the same whatever I pass to oper().


    Of course, you're going to get 2 numbers. You call vop() twice.
    Have you read the documentation for printf? Try changing your
    main to

    int main(void)
    {
    double a, b;
    a = oper('s', 5., 2.);
    b = oper('s', 50., 2.);
    printf("%f %f\n", a, b);
    return (0);
    }


    --
    steve
    steve, Jan 17, 2011
    #3
  4. Hi,

    On 2011-01-17, Bill Cunningham <> wrote:
    > I have these two functions one calls the other one. I have tried
    > everything to debug them except a damn debugger. I have placed puts messages
    > at all the return and I still get two numbers on my screen. What's wrong
    > with this code? I will post the header, object file source and main() code.
    >
    > #include <stdio.h>
    >
    > double oper(char, double, double);
    > double vop(double);
    >
    > Ok that's my declarations
    >
    > #include "core.h"
    >
    > double oper(char c, double x, double y)
    > {
    > if (c == 'a')
    > return vop(x + y);
    > else if (c == 's') {
    > return vop(x - y);
    > } else if (c == 'm') {
    > return vop(x * y);
    > } else if (c == 'd') {
    > return vop(x / y);
    > } else {
    > fputs("usage error\n", stderr);
    > return -1;
    > }
    > return 0;
    > }


    This function will return the vop's return value (in case the supported
    operation is passed in the 'c' variable).

    >
    > double vop(double oper)
    > {
    > return printf("%.2f\n",oper);
    > }


    This function will return the printf's return value (number of successfully
    written characters), not the oper's value.

    >
    > Above here is the main code I compile in .o file. And this is the main
    > function:
    >
    > #include "core.h"
    >
    > int main()
    > {
    > double a;
    > a = oper('s', 5, 2);
    > return vop(a);
    > }


    The 'a' variable will contain the oper's return value (number of characters
    written by printf). Try call like oper('s', 9, 2); and you will see that the
    number has changed.

    -P

    >
    > Everything works like I want it to except I am getting two numbers. The
    > second number is always the same whatever I pass to oper().
    >
    > Bill
    >
    >
    Pavel Borzenkov, Jan 17, 2011
    #4
  5. steve wrote:

    [snip]

    > Of course, you're going to get 2 numbers. You call vop() twice.


    How am I doing that? Do you mean in the oper() and in main() I am
    calling vop?

    > Have you read the documentation for printf? Try changing your
    > main to
    >
    > int main(void)
    > {
    > double a, b;
    > a = oper('s', 5., 2.);
    > b = oper('s', 50., 2.);
    > printf("%f %f\n", a, b);
    > return (0);
    > }


    Yes I see what you're doing above but it is defeating the purpose I
    started out with. I want vop() or oper() to call the other to printf. I
    don't just want to stick printf into main(). I hope I'm clear.

    Bill
    Bill Cunningham, Jan 17, 2011
    #5
  6. "Bill Cunningham" <> writes:

    > I have these two functions one calls the other one. I have tried
    > everything to debug them except a damn debugger.


    Might be worth a try then.

    > I have placed puts messages
    > at all the return and I still get two numbers on my screen. What's wrong
    > with this code? I will post the header, object file source and main() code.
    >
    > #include <stdio.h>
    >
    > double oper(char, double, double);
    > double vop(double);
    >
    > Ok that's my declarations
    >
    > #include "core.h"
    >
    > double oper(char c, double x, double y)
    > {
    > if (c == 'a')
    > return vop(x + y);
    > else if (c == 's') {
    > return vop(x - y);
    > } else if (c == 'm') {
    > return vop(x * y);
    > } else if (c == 'd') {
    > return vop(x / y);
    > } else {
    > fputs("usage error\n", stderr);
    > return -1;
    > }
    > return 0;


    This last line serves no purpose.

    > }
    >
    > double vop(double oper)
    > {
    > return printf("%.2f\n",oper);


    What value does printf return? Is that really the value you want vop
    (and hence) oper to return?

    > }
    >
    > Above here is the main code I compile in .o file. And this is the main
    > function:
    >
    > #include "core.h"
    >
    > int main()
    > {
    > double a;
    > a = oper('s', 5, 2);
    > return vop(a);


    Do you really want the result of vop to the return value from main?
    Looks unlikely to me.

    > }
    >
    > Everything works like I want it to except I am getting two numbers.


    You get two numbers printed because there are two calls to vop: one in
    main and one from the call to oper in main.

    > The
    > second number is always the same whatever I pass to oper().


    Not if you change the call to oper('s', 50, 2) or to oper('s', 2, 5) or,
    indeed, to lots of other things that give a different second number.

    --
    Ben.
    Ben Bacarisse, Jan 17, 2011
    #6
  7. Pavel Borzenkov wrote:
    > Hi,
    >
    > On 2011-01-17, Bill Cunningham <> wrote:
    >> I have these two functions one calls the other one. I have tried
    >> everything to debug them except a damn debugger. I have placed puts
    >> messages at all the return and I still get two numbers on my screen.
    >> What's wrong with this code? I will post the header, object file
    >> source and main() code.
    >>
    >> #include <stdio.h>
    >>
    >> double oper(char, double, double);
    >> double vop(double);
    >>
    >> Ok that's my declarations
    >>
    >> #include "core.h"
    >>
    >> double oper(char c, double x, double y)
    >> {
    >> if (c == 'a')
    >> return vop(x + y);
    >> else if (c == 's') {
    >> return vop(x - y);
    >> } else if (c == 'm') {
    >> return vop(x * y);
    >> } else if (c == 'd') {
    >> return vop(x / y);
    >> } else {
    >> fputs("usage error\n", stderr);
    >> return -1;
    >> }
    >> return 0;
    >> }

    >
    > This function will return the vop's return value (in case the
    > supported operation is passed in the 'c' variable).


    Ok maybe that's the mistake. I've never really worked in this manner
    before with returns. I thought I was sending to vop(). So what I probably
    need then is to use return oper() does that sound right. Boy I feel like a
    big fool now but this one really stuck me.
    >>
    >> double vop(double oper)
    >> {
    >> return printf("%.2f\n",oper);
    >> }

    >
    > This function will return the printf's return value (number of
    > successfully written characters), not the oper's value.


    I'm not sure I understand here.

    >> Above here is the main code I compile in .o file. And this is the
    >> main function:
    >>
    >> #include "core.h"
    >>
    >> int main()
    >> {
    >> double a;
    >> a = oper('s', 5, 2);
    >> return vop(a);
    >> }

    >
    > The 'a' variable will contain the oper's return value (number of
    > characters written by printf). Try call like oper('s', 9, 2); and you
    > will see that the number has changed.
    >
    > -P
    >
    >>
    >> Everything works like I want it to except I am getting two numbers.
    >> The second number is always the same whatever I pass to oper().
    >>
    >> Bill
    Bill Cunningham, Jan 17, 2011
    #7
  8. Bill Cunningham wrote:
    > I have these two functions one calls the other one. I have tried
    > everything to debug them except a damn debugger. I have placed puts messages
    > at all the return and I still get two numbers on my screen. What's wrong
    > with this code?


    Whether the code is wrong or right depends on what you want it to do,
    which you haven't said -- yet.

    > double oper(char c, double x, double y)
    > {
    > if (c == 'a')
    > return vop(x + y);
    > else if (c == 's') {
    > return vop(x - y);
    > } else if (c == 'm') {
    > return vop(x * y);
    > } else if (c == 'd') {
    > return vop(x / y);
    > } else {
    > fputs("usage error\n", stderr);
    > return -1;
    > }
    > return 0;


    That "return 0" is unreachable, so I would say it is "wrong".

    > double vop(double oper)
    > {
    > return printf("%.2f\n",oper);


    Since I have never seen a function that was expected to return the
    return value of a printf() call, I *suspect* that that is "wrong".
    Perhaps you meant:

    printf("%.2f\n",oper);
    return oper;

    > }
    >
    > Above here is the main code I compile in .o file. And this is the main
    > function:
    >
    > #include "core.h"
    >
    > int main()
    > {
    > double a;
    > a = oper('s', 5, 2);


    The first number is printed in vop() that is called from oper(). It
    prints the number 3 (5-2) in the form 3.00.

    With your implementation of vop(), the variable "a" will always have the
    return value of printf() which normally is the number of characters
    printed. This number depends on the magnitude of the parameter oper in
    vop(); here oper equals 3.0 so the printed string is "3.00\n" which is 5
    characters long (under Linux, where \n is a single character).
    Therefore, "a" is 5.0.

    > return vop(a);


    The second number is printed in this call of vop() and is 5.00.

    By the way, I would call this "return" statement "wrong" because it
    tries to return a double value from main(), instead of an int.

    > }
    >
    > Everything works like I want it to except I am getting two numbers. The
    > second number is always the same whatever I pass to oper().


    Try oper('s', 5000.0, 2.0). The second printed number should then be
    larger than 5 because the first printf() prints the longer string
    4998.00\n (which is 8 characters under Linux).

    --
    Niklas Holsti
    Tidorum Ltd
    niklas holsti tidorum fi
    . @ .
    Niklas Holsti, Jan 17, 2011
    #8
  9. Ben Bacarisse wrote:
    > "Bill Cunningham" <> writes:
    >
    >> I have these two functions one calls the other one. I have tried
    >> everything to debug them except a damn debugger.

    >
    > Might be worth a try then.
    >
    >> I have placed puts messages
    >> at all the return and I still get two numbers on my screen. What's
    >> wrong with this code? I will post the header, object file source and
    >> main() code.
    >>
    >> #include <stdio.h>
    >>
    >> double oper(char, double, double);
    >> double vop(double);
    >>
    >> Ok that's my declarations
    >>
    >> #include "core.h"
    >>
    >> double oper(char c, double x, double y)
    >> {
    >> if (c == 'a')
    >> return vop(x + y);
    >> else if (c == 's') {
    >> return vop(x - y);
    >> } else if (c == 'm') {
    >> return vop(x * y);
    >> } else if (c == 'd') {
    >> return vop(x / y);
    >> } else {
    >> fputs("usage error\n", stderr);
    >> return -1;
    >> }
    >> return 0;

    >
    > This last line serves no purpose.
    >
    >> }
    >>
    >> double vop(double oper)
    >> {
    >> return printf("%.2f\n",oper);

    >
    > What value does printf return? Is that really the value you want vop
    > (and hence) oper to return?


    Ok no no. I want printf to return vop() then. Ok thanks much for clearing me
    up. I will try this again maybe from scratch. I want the answer of oper() to
    be passed to vop() which is called from main and it prints what oper()
    returns.

    >> }
    >>
    >> Above here is the main code I compile in .o file. And this is the
    >> main function:
    >>
    >> #include "core.h"
    >>
    >> int main()
    >> {
    >> double a;
    >> a = oper('s', 5, 2);
    >> return vop(a);

    >
    > Do you really want the result of vop to the return value from main?
    > Looks unlikely to me.
    >
    >> }
    >>
    >> Everything works like I want it to except I am getting two numbers.

    >
    > You get two numbers printed because there are two calls to vop: one in
    > main and one from the call to oper in main.
    >
    >> The
    >> second number is always the same whatever I pass to oper().

    >
    > Not if you change the call to oper('s', 50, 2) or to oper('s', 2, 5)
    > or, indeed, to lots of other things that give a different second
    > number.
    Bill Cunningham, Jan 17, 2011
    #9
  10. Niklas Holsti wrote:
    > Bill Cunningham wrote:
    >> I have these two functions one calls the other one. I have tried
    >> everything to debug them except a damn debugger. I have placed puts
    >> messages at all the return and I still get two numbers on my screen.
    >> What's wrong with this code?

    >
    > Whether the code is wrong or right depends on what you want it to do,
    > which you haven't said -- yet.
    >
    >> double oper(char c, double x, double y)
    >> {
    >> if (c == 'a')
    >> return vop(x + y);
    >> else if (c == 's') {
    >> return vop(x - y);
    >> } else if (c == 'm') {
    >> return vop(x * y);
    >> } else if (c == 'd') {
    >> return vop(x / y);
    >> } else {
    >> fputs("usage error\n", stderr);
    >> return -1;
    >> }
    >> return 0;

    >
    > That "return 0" is unreachable, so I would say it is "wrong".


    I wondered if that return was needed or not. I seemed like it should be
    there.

    >> double vop(double oper)
    >> {
    >> return printf("%.2f\n",oper);

    >
    > Since I have never seen a function that was expected to return the
    > return value of a printf() call, I *suspect* that that is "wrong".


    I have seen it. I don't know if it's good code or not though.

    > Perhaps you meant:
    >
    > printf("%.2f\n",oper);
    > return oper;
    >
    >> }
    >>
    >> Above here is the main code I compile in .o file. And this is the
    >> main function:
    >>
    >> #include "core.h"
    >>
    >> int main()
    >> {
    >> double a;
    >> a = oper('s', 5, 2);

    >
    > The first number is printed in vop() that is called from oper(). It
    > prints the number 3 (5-2) in the form 3.00.
    >
    > With your implementation of vop(), the variable "a" will always have
    > the return value of printf() which normally is the number of
    > characters printed. This number depends on the magnitude of the
    > parameter oper in vop(); here oper equals 3.0 so the printed string
    > is "3.00\n" which is 5 characters long (under Linux, where \n is a
    > single character). Therefore, "a" is 5.0.
    >
    >> return vop(a);

    >
    > The second number is printed in this call of vop() and is 5.00.
    >
    > By the way, I would call this "return" statement "wrong" because it
    > tries to return a double value from main(), instead of an int.
    >
    >> }
    >>
    >> Everything works like I want it to except I am getting two numbers.
    >> The second number is always the same whatever I pass to oper().

    >
    > Try oper('s', 5000.0, 2.0). The second printed number should then be
    > larger than 5 because the first printf() prints the longer string
    > 4998.00\n (which is 8 characters under Linux).


    Interesting.
    Bill Cunningham, Jan 17, 2011
    #10
  11. Bill Cunningham

    osmium Guest

    "Niklas Holsti" wrote:

    <snippage>

    > Bill Cunningham wrote:
    >> I have these two functions one calls the other one. I have tried
    >> everything to debug them except a damn debugger. I have placed puts
    >> messages at all the return and I still get two numbers on my screen.
    >> What's wrong with this code?

    >
    > Whether the code is wrong or right depends on what you want it to do,
    > which you haven't said -- yet.


    This is a persistent problem, Bill. Cn you complete the comments I have
    added? I can *almost* figure out what oper() is supposed to do, then it
    calls vop() for some reason unknown to me and I am baffled. I think you
    wanted to print something but wanted to do it indirectly. Might a macro be
    be usefull here? Is the object to save typing? Or what?

    // This function ....
    >> double oper(char c, double x, double y)
    >> {
    >> if (c == 'a')
    >> return vop(x + y);
    >> else if (c == 's') {
    >> return vop(x - y);
    >> } else if (c == 'm') {
    >> return vop(x * y);
    >> } else if (c == 'd') {
    >> return vop(x / y);
    >> } else {
    >> fputs("usage error\n", stderr);
    >> return -1;
    >> }
    >> return 0;

    >


    // This fucntion ...
    >> double vop(double oper)
    >> {
    >> return printf("%.2f\n",oper);
    >> }

    Why overuse identifiers? There are plenty of unique identifiers to go
    around, and uniqueness adds clarity.
    In other words:

    double vop(double abc)
    {
    retrun printf("%.2f\n", abc):
    }
    would be less confusing.

    >> Above here is the main code I compile in .o file. And this is the main
    >> function:
    >>
    >> #include "core.h"
    >>


    // this program tests ... by.... and then ....
    >> int main()
    >> {
    >> double a;
    >> a = oper('s', 5, 2);

    >
    > The first number is printed in vop() that is called from oper(). It prints
    > the number 3 (5-2) in the form 3.00.
    >
    > With your implementation of vop(), the variable "a" will always have the
    > return value of printf() which normally is the number of characters
    > printed. This number depends on the magnitude of the parameter oper in
    > vop(); here oper equals 3.0 so the printed string is "3.00\n" which is 5
    > characters long (under Linux, where \n is a single character). Therefore,
    > "a" is 5.0.
    >
    >> return vop(a);

    >
    > The second number is printed in this call of vop() and is 5.00.
    >> }
    >>
    >> Everything works like I want it to except I am getting two numbers. The
    >> second number is always the same whatever I pass to oper().
    osmium, Jan 17, 2011
    #11
  12. osmium wrote:
    > "Niklas Holsti" wrote:
    >
    > <snippage>
    >
    >> Bill Cunningham wrote:
    >>> I have these two functions one calls the other one. I have tried
    >>> everything to debug them except a damn debugger. I have placed puts
    >>> messages at all the return and I still get two numbers on my screen.
    >>> What's wrong with this code?

    >>
    >> Whether the code is wrong or right depends on what you want it to do,
    >> which you haven't said -- yet.


    Ok I thought it was clear I will start adding comments from now on. I
    want to call a function to do basic arithmetic operations called oper().
    vop() mean ViewOPer. What oper() returns is passed to vop() which acts as a
    sort of printf without calling printf directly in main. What I wanted was a
    simple experiment in a function passing value to another function which
    takes that data and does something with it. I this case prints data.

    > This is a persistent problem, Bill. Cn you complete the comments I
    > have added? I can *almost* figure out what oper() is supposed to do,
    > then it calls vop() for some reason unknown to me and I am baffled. I
    > think you wanted to print something but wanted to do it indirectly. Might
    > a macro be be usefull here? Is the object to save typing? Or
    > what?
    > // This function ....
    >>> double oper(char c, double x, double y)
    >>> {
    >>> if (c == 'a')
    >>> return vop(x + y);
    >>> else if (c == 's') {
    >>> return vop(x - y);
    >>> } else if (c == 'm') {
    >>> return vop(x * y);
    >>> } else if (c == 'd') {
    >>> return vop(x / y);
    >>> } else {
    >>> fputs("usage error\n", stderr);
    >>> return -1;
    >>> }
    >>> return 0;

    >>

    >
    > // This fucntion ...
    >>> double vop(double oper)
    >>> {
    >>> return printf("%.2f\n",oper);
    >>> }

    > Why overuse identifiers? There are plenty of unique identifiers to go
    > around, and uniqueness adds clarity.
    > In other words:
    >
    > double vop(double abc)
    > {
    > retrun printf("%.2f\n", abc):
    > }
    > would be less confusing.
    >
    >>> Above here is the main code I compile in .o file. And this is the
    >>> main function:
    >>>
    >>> #include "core.h"
    >>>

    >
    > // this program tests ... by.... and then ....
    >>> int main()
    >>> {
    >>> double a;
    >>> a = oper('s', 5, 2);

    >>
    >> The first number is printed in vop() that is called from oper(). It
    >> prints the number 3 (5-2) in the form 3.00.
    >>
    >> With your implementation of vop(), the variable "a" will always have
    >> the return value of printf() which normally is the number of
    >> characters printed. This number depends on the magnitude of the
    >> parameter oper in vop(); here oper equals 3.0 so the printed string
    >> is "3.00\n" which is 5 characters long (under Linux, where \n is a
    >> single character). Therefore, "a" is 5.0.
    >>
    >>> return vop(a);

    >>
    >> The second number is printed in this call of vop() and is 5.00.
    >>> }
    >>>
    >>> Everything works like I want it to except I am getting two numbers.
    >>> The second number is always the same whatever I pass to oper().
    Bill Cunningham, Jan 17, 2011
    #12
  13. Bill Cunningham

    Ian Collins Guest

    On 01/18/11 11:00 AM, Bill Cunningham wrote:
    > osmium wrote:
    >> "Niklas Holsti" wrote:
    >>
    >> <snippage>
    >>
    >>> Bill Cunningham wrote:
    >>>> I have these two functions one calls the other one. I have tried
    >>>> everything to debug them except a damn debugger. I have placed puts
    >>>> messages at all the return and I still get two numbers on my screen.
    >>>> What's wrong with this code?
    >>>
    >>> Whether the code is wrong or right depends on what you want it to do,
    >>> which you haven't said -- yet.

    >
    > Ok I thought it was clear I will start adding comments from now on. I
    > want to call a function to do basic arithmetic operations called oper().
    > vop() mean ViewOPer.


    Then why don't you call it ViewOper?

    --
    Ian Collins
    Ian Collins, Jan 17, 2011
    #13
  14. Bill Cunningham

    osmium Guest

    "Bill Cunningham" writes:

    > osmium wrote:
    >> "Niklas Holsti" wrote:
    >>
    >> <snippage>
    >>
    >>> Bill Cunningham wrote:
    >>>> I have these two functions one calls the other one. I have tried
    >>>> everything to debug them except a damn debugger. I have placed puts
    >>>> messages at all the return and I still get two numbers on my screen.
    >>>> What's wrong with this code?
    >>>
    >>> Whether the code is wrong or right depends on what you want it to do,
    >>> which you haven't said -- yet.

    >
    > Ok I thought it was clear I will start adding comments from now on. I
    > want to call a function to do basic arithmetic operations called oper().
    > vop() mean ViewOPer. What oper() returns is passed to vop() which acts as
    > a sort of printf without calling printf directly in main. What I wanted
    > was a simple experiment in a function passing value to another function
    > which takes that data and does something with it. I this case prints data.


    In that case, the call to vop directly from main makes no sense does it?
    Why do you think the operating system wants to know the results returned by
    a function that *you* wrote? The OS only cares about goodness or badness.

    In the future you might want to think "what will the caller do with this
    value I am planning to return?" If the answer is "nothing", then don't use a
    return, the function will return without the explicit statement being
    included. In the case at hand you wanted the printf for the side effects,
    not the value returned by printf. It may seem ridiculous, but printing (and
    other very useful things, are classified as side effects.

    I will look forward to an explanatory comment in front of each of your
    function definitions.
    osmium, Jan 17, 2011
    #14
  15. osmium wrote:

    > In that case, the call to vop directly from main makes no sense does
    > it? Why do you think the operating system wants to know the results
    > returned by a function that *you* wrote? The OS only cares about
    > goodness or badness.
    > In the future you might want to think "what will the caller do with
    > this value I am planning to return?" If the answer is "nothing", then
    > don't use a return, the function will return without the explicit
    > statement being included. In the case at hand you wanted the printf
    > for the side effects, not the value returned by printf. It may seem
    > ridiculous, but printing (and other very useful things, are
    > classified as side effects.
    > I will look forward to an explanatory comment in front of each of your
    > function definitions.


    What if oper() said this...

    if (c=='a')
    return x+y; ...

    Now if I am correct the added variables in the function would be returned
    but it wouldn't be printed. I want to pass this "return x+y;" to a function
    that would print the results. An experiment using two functions. I could use
    printf in main but I want a wrapper function that would basically printf in
    this function. Make sense?

    Bill
    Bill Cunningham, Jan 18, 2011
    #15
  16. Bill Cunningham

    osmium Guest

    "Bill Cunningham" wrote:

    > osmium wrote:
    >
    >> In that case, the call to vop directly from main makes no sense does
    >> it? Why do you think the operating system wants to know the results
    >> returned by a function that *you* wrote? The OS only cares about
    >> goodness or badness.
    >> In the future you might want to think "what will the caller do with
    >> this value I am planning to return?" If the answer is "nothing", then
    >> don't use a return, the function will return without the explicit
    >> statement being included. In the case at hand you wanted the printf
    >> for the side effects, not the value returned by printf. It may seem
    >> ridiculous, but printing (and other very useful things, are
    >> classified as side effects.
    >> I will look forward to an explanatory comment in front of each of your
    >> function definitions.

    >
    > What if oper() said this...
    >
    > if (c=='a')
    > return x+y; ...
    >
    > Now if I am correct the added variables in the function would be returned
    > but it wouldn't be printed. I want to pass this "return x+y;" to a
    > function that would print the results. An experiment using two functions.
    > I could use printf in main but I want a wrapper function that would
    > basically printf in this function. Make sense?


    You could compute the desired result for all four good cases and pass that
    value to a function that prints it.

    Something like:

    in oper():
    double result;
    ....
    if(c == 'a')
    result = x+y;
    // other three cases plus error detection case
    ....
    print(result);
    return result;

    void print(double arg)
    {
    printf( fromat_stuff, arg); // note lack of return statement
    }
    ---------------
    I don't know if that helps or not. If not code something up and post it.
    osmium, Jan 18, 2011
    #16
  17. On 2011-01-17 21:16, Bill Cunningham wrote:
    > Niklas Holsti wrote:
    >> Since I have never seen a function that was expected to return the
    >> return value of a printf() call, I *suspect* that that is "wrong".

    >
    > I have seen it. I don't know if it's good code or not though.


    I would recommend you to only write code that makes sense to you.

    http://en.wikipedia.org/wiki/Cargo_cult_programming

    August

    --
    The competent programmer is fully aware of the limited size of his own
    skull. He therefore approaches his task with full humility, and avoids
    clever tricks like the plague. --Edsger Dijkstra
    August Karlstrom, Jan 18, 2011
    #17
  18. On 2011-01-18 13:54, Richard wrote:
    > August Karlstrom<> writes:
    >> I would recommend you to only write code that makes sense to you.
    >>
    >> http://en.wikipedia.org/wiki/Cargo_cult_programming
    >>
    >> August

    >
    > And I would recommend you write code that others will understand : and
    > you will soon see why.


    OK, my bad. I meant to say "Don't write code that doesn't make sense to
    you".


    August

    --
    The competent programmer is fully aware of the limited size of his own
    skull. He therefore approaches his task with full humility, and avoids
    clever tricks like the plague. --Edsger Dijkstra
    August Karlstrom, Jan 18, 2011
    #18
  19. "Bill Cunningham" <> writes:
    > Ben Bacarisse wrote:

    [...]
    >> What value does printf return? Is that really the value you want vop
    >> (and hence) oper to return?

    >
    > Ok no no. I want printf to return vop() then.


    That makes no sense at all.

    I don't expect you to stop making wild guesses and stringing words
    together randomly, but *if* your actual goal is to learn to write C,
    then you should.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jan 18, 2011
    #19
  20. Bill Cunningham

    BruceS Guest

    On Jan 18, 12:52 pm, Keith Thompson <> wrote:
    > "Bill Cunningham" <> writes:
    > > Ben Bacarisse wrote:

    > [...]
    > >> What value does printf return?  Is that really the value you want vop
    > >> (and hence) oper to return?

    >
    > > Ok no no. I want printf to return vop() then.

    >
    > That makes no sense at all.
    >
    > I don't expect you to stop making wild guesses and stringing words
    > together randomly, but *if* your actual goal is to learn to write C,
    > then you should.


    It seems clear to me that that is not his goal. Bill seems to be
    achieving his goal quite nicely.
    BruceS, Jan 18, 2011
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nic

    One Hot FSM stuck !!

    Nic, Feb 26, 2004, in forum: VHDL
    Replies:
    5
    Views:
    1,136
    FGreen
    Mar 2, 2004
  2. TheRain
    Replies:
    4
    Views:
    7,337
  3. ccd

    Webmatrix stuck

    ccd, Aug 21, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    371
  4. HalaszJ

    Stuck

    HalaszJ, Oct 21, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    596
    Eduardo M?ndez
    Oct 22, 2003
  5. Brent Burkart

    emails get stuck in mailroot\Queue

    Brent Burkart, Oct 21, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    2,492
    Brent Burkart
    Oct 23, 2003
Loading...

Share This Page