functions question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sonoman

    functions question

    Hi all:
    I have to functions. Function A that returns a parameter and function B that
    takes a parameter, both are the same type. Can I put a call to function A as
    a parameter for function B? or is it better to put the returned value from A
    into a variable and the put that variable into B as a parameter? Which is a
    better programming practice? Thanks in advance


  • Buster

    #2
    Re: functions question

    "Sonoman" <fcarpio@cse.fa u.edu> wrote[color=blue]
    > Hi all:
    > I have to functions. Function A that returns a parameter and function B that
    > takes a parameter, both are the same type. Can I put a call to function A as
    > a parameter for function B? or is it better to put the returned value from A
    > into a variable and the put that variable into B as a parameter? Which is a
    > better programming practice? Thanks in advance[/color]

    Generally speaking it's fine to pass the return value of one function as an
    argument to a second function. There are sometimes issues with exception
    safety if the value is a pointer to a dynamically allocated object, especially
    if
    the second function takes more than one such argument.

    Can you be more specific about the problem you are trying to solve?

    Regards,
    Buster.


    Comment

    • jeffc

      #3
      Re: functions question


      "Buster" <noone@nowhere. com> wrote in message
      news:blhnfr$f1$ 1@news7.svr.pol .co.uk...[color=blue]
      > "Sonoman" <fcarpio@cse.fa u.edu> wrote[color=green]
      > > Hi all:
      > > I have to functions. Function A that returns a parameter and function B[/color][/color]
      that[color=blue][color=green]
      > > takes a parameter, both are the same type. Can I put a call to function[/color][/color]
      A as[color=blue][color=green]
      > > a parameter for function B? or is it better to put the returned value[/color][/color]
      from A[color=blue][color=green]
      > > into a variable and the put that variable into B as a parameter? Which[/color][/color]
      is a[color=blue][color=green]
      > > better programming practice? Thanks in advance[/color]
      >
      > Generally speaking it's fine to pass the return value of one function as[/color]
      an[color=blue]
      > argument to a second function.[/color]

      I think he meant the function as expression.
      void f(int);
      int g();

      // call
      f(g());

      If that's what he meant, then I think the call I wrote is fine, as long as
      it's maintainable. If it gets longer and difficult to understand, it should
      be changed. If it is too hard to debug (you want to see the value of g()
      before f gets called), it should be changed.


      Comment

      • Buster

        #4
        Re: functions question


        "jeffc" <nobody@nowhere .com> wrote in message[color=blue]
        > "Buster" <noone@nowhere. com> wrote in message[color=green]
        > > "Sonoman" <fcarpio@cse.fa u.edu> wrote[/color][/color]
        [color=blue][color=green]
        > > Generally speaking it's fine to pass the return value of one function
        > > as an argument to a second function.[/color][/color]
        [color=blue]
        > I think he meant the function as expression.
        > void f(int);
        > int g();
        >
        > // call
        > f(g());[/color]

        I think that's what I meant too. What do you think I meant? :)
        [color=blue]
        > If that's what he meant, then I think the call I wrote is fine, as long as
        > it's maintainable. If it gets longer and difficult to understand, it should
        > be changed. If it is too hard to debug (you want to see the value of g()
        > before f gets called), it should be changed.[/color]

        I think you're right. OTOH it might just about be possible to come
        up with an example where it's unsafe. The person who writes is
        very good at coming up with those examples.

        Later,
        Buster.



        Comment

        • Buster

          #5
          Re: functions question


          "Buster" <noone@nowhere. com> wrote
          [color=blue]
          > I think you're right. OTOH it might just about be possible to come
          > up with an example where it's unsafe. The person who writes[/color]

          Dammit. Does anyone ever use overwrite mode on purpose?
          The person who writes GOTW ...
          [color=blue]
          > is very good at coming up with those examples.
          >
          > Later,
          > Buster.
          >
          >
          >[/color]


          Comment

          • jeffc

            #6
            Re: functions question


            "Buster" <noone@nowhere. com> wrote in message
            news:blhr5j$jmd $1@news6.svr.po l.co.uk...[color=blue]
            >
            > "jeffc" <nobody@nowhere .com> wrote in message[color=green]
            > > "Buster" <noone@nowhere. com> wrote in message[color=darkred]
            > > > "Sonoman" <fcarpio@cse.fa u.edu> wrote[/color][/color]
            >[color=green][color=darkred]
            > > > Generally speaking it's fine to pass the return value of one function
            > > > as an argument to a second function.[/color][/color]
            >[color=green]
            > > I think he meant the function as expression.
            > > void f(int);
            > > int g();
            > >
            > > // call
            > > f(g());[/color]
            >
            > I think that's what I meant too. What do you think I meant? :)[/color]

            When you wrote "it's fine to pass the return value of one function as an
            argument to a second function", I thought you meant
            int i = g();
            f(i);

            This sounds like the conversation between the 2 German guards in the No One
            Lives Forever computer game (you had to be there :-)


            Comment

            • Buster

              #7
              Re: functions question


              "jeffc" <nobody@nowhere .com> wrote[color=blue]
              > "Buster" <noone@nowhere. com> wrote[color=green]
              > > I think that's what I meant too. What do you think I meant? :)[/color]
              >
              > When you wrote "it's fine to pass the return value of one function as an
              > argument to a second function", I thought you meant
              > int i = g();
              > f(i);[/color]

              Ahh. Yes, I see that I didn't say what I meant. Thanks.
              [color=blue]
              > This sounds like the conversation between the 2 German guards in the No One
              > Lives Forever computer game (you had to be there :-)[/color]

              OK.

              Regards,
              Buster.


              Comment

              • Kevin Goodsell

                #8
                [OT] Overwrite (was Re: functions question)

                Buster wrote:
                [color=blue]
                > "Buster" <noone@nowhere. com> wrote
                >
                >[color=green]
                >>I think you're right. OTOH it might just about be possible to come
                >>up with an example where it's unsafe. The person who writes[/color]
                >
                >
                > Dammit. Does anyone ever use overwrite mode on purpose?[/color]

                I turn it on all the time, but never on purpose. I don't think I ever
                use that key intentionally, but it's right next to 3 keys I use
                constantly. I often think about just prying that sucker off my keyboard.
                A less permanent solution would be nice... I wonder if there's an
                application available for disabling particular keys...

                -Kevin
                --
                My email address is valid, but changes periodically.
                To contact me please use the address from a recent posting.

                Comment

                • Mike Wahler

                  #9
                  Re: [OT] Overwrite (was Re: functions question)


                  "Kevin Goodsell" <usenet1.spamfr ee.fusion@never box.com> wrote in message
                  news:Zo%eb.1258 6$NX3.5472@news read3.news.pas. earthlink.net.. .[color=blue]
                  > Buster wrote:
                  >[color=green]
                  > > "Buster" <noone@nowhere. com> wrote
                  > >
                  > >[color=darkred]
                  > >>I think you're right. OTOH it might just about be possible to come
                  > >>up with an example where it's unsafe. The person who writes[/color]
                  > >
                  > >
                  > > Dammit. Does anyone ever use overwrite mode on purpose?[/color]
                  >
                  > I turn it on all the time, but never on purpose. I don't think I ever
                  > use that key intentionally, but it's right next to 3 keys I use
                  > constantly. I often think about just prying that sucker off my keyboard.
                  > A less permanent solution would be nice... I wonder if there's an
                  > application available for disabling particular keys...[/color]

                  I'm not sure about 'disable', but there are applications
                  which allow the assignment of given functions to particular
                  keys. Perhaps assign the 'insert' key to a 'beep' operation,
                  or perhaps to play an audio file which says "fumble-fingers!"
                  :-)

                  -Mike


                  Comment

                  Working...