Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Calculated values from different functions to a single function.

Calculated values from different functions to a single function.

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
22 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C CodingLover

    I hope and may I give a wrong explanation. I'll try to give an example. Here it is. Say I have following functions. void functionOne(int x, int y) { // Do some work and find some values, // say int a, int b // Need to send those values to mainCal() } void functionTwo() { // Do some work and find some values, // say double dd, // Need to send those values to mainCal() } Now what I want to do is, use those calculated values inside another function for another process. void mainCal(int i, int j, double k) { // Using i, j, k // Do another process } I think now my question is clear to you :)

    I appreciate your help all the time... Eranga :)

    R Offline
    R Offline
    Rajasekharan Vengalil
    wrote on last edited by
    #6

    Hmm. Since this is a C++ forum I am going to assume you are using C++. Here's one way of doing it:

    void functionOne( int x, int y, int& a, int& b )
    {
    // do some work and initialize "a" and "b"
    }

    void functionTwo( double& dd )
    {
    // do some work and initialize "dd"
    }

    void mainCal( int i, int j, double k )
    {
    functionOne( 1, 2, i, j );
    functionTwo( k );
    }

    The key thing to note here is that a, b and dd are passed by reference to functionOne and functionTwo. -- modified at 4:34 Monday 12th November, 2007 Fixed typo where only 2 params where supplied to functionOne when it expects 4.

    -- gleat http://blogorama.nerdworks.in[^] --

    C 1 Reply Last reply
    0
    • C CodingLover

      I hope and may I give a wrong explanation. I'll try to give an example. Here it is. Say I have following functions. void functionOne(int x, int y) { // Do some work and find some values, // say int a, int b // Need to send those values to mainCal() } void functionTwo() { // Do some work and find some values, // say double dd, // Need to send those values to mainCal() } Now what I want to do is, use those calculated values inside another function for another process. void mainCal(int i, int j, double k) { // Using i, j, k // Do another process } I think now my question is clear to you :)

      I appreciate your help all the time... Eranga :)

      R Offline
      R Offline
      Rajesh R Subramanian
      wrote on last edited by
      #7

      It doesn't look like you are using x, y and dd in minCal(). But is that just what you want? Why can't you simply use global variables? ...if only I understood your question right...


      Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->ßRÅhmmÃ<-·´¯`·.

      C 1 Reply Last reply
      0
      • R Rajasekharan Vengalil

        Hmm. Since this is a C++ forum I am going to assume you are using C++. Here's one way of doing it:

        void functionOne( int x, int y, int& a, int& b )
        {
        // do some work and initialize "a" and "b"
        }

        void functionTwo( double& dd )
        {
        // do some work and initialize "dd"
        }

        void mainCal( int i, int j, double k )
        {
        functionOne( 1, 2, i, j );
        functionTwo( k );
        }

        The key thing to note here is that a, b and dd are passed by reference to functionOne and functionTwo. -- modified at 4:34 Monday 12th November, 2007 Fixed typo where only 2 params where supplied to functionOne when it expects 4.

        -- gleat http://blogorama.nerdworks.in[^] --

        C Offline
        C Offline
        CodingLover
        wrote on last edited by
        #8

        That's make sense to me, thanks. But I have one question. I want to use x, y and dd inside the mainCal(). here,

        gleat wrote:

        void functionOne( int x, int y, int& a, int& b ) { // do some work and initialize "a" and "b" }

        you have use two reference, and that's clear to me. Then I want to call mainCal() there and send x and y. So what you have done here,

        gleat wrote:

        void mainCal( int i, int j, double k ) { functionOne( i, j ); functionTwo( k ); }

        is not clear for me....

        I appreciate your help all the time... Eranga :)

        R 1 Reply Last reply
        0
        • C CodingLover

          Hi all, I don't know how far my question is obvious to you. Say I have few functions, and they return different types of values. I want to use them inside an other single function. How should I do that. Any clue.

          I appreciate your help all the time... Eranga :)

          O Offline
          O Offline
          only coder
          wrote on last edited by
          #9

          typedef struct tagRET_VAL { int code; void* pValue; }RET_VAL, *PRETVAL; enum CODE{ CODE_INT, CODE_STRING }; void fun1() { int a = 10; RET_VAL rv; rv.code = CODE_INT; rv.pValue = &a; return &rv; } void fun2() { char s[] = "hello"; RET_VAL rv; rv.code = CODE_STRING; rv.pValue = s; return &rv; } void GetValue( PRET_VAL pRV ) { switch( pRV->code ) { case CODE_INT: // do something with (int *)pRV->pValue .... break; case CODE_STRING: // do something with (char *)pRV->pValue ... break; } }

          1 Reply Last reply
          0
          • R Rajesh R Subramanian

            It doesn't look like you are using x, y and dd in minCal(). But is that just what you want? Why can't you simply use global variables? ...if only I understood your question right...


            Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->ßRÅhmmÃ<-·´¯`·.

            C Offline
            C Offline
            CodingLover
            wrote on last edited by
            #10

            brahmma wrote:

            It doesn't look like you are using x, y and dd in minCal(). But is that just what you want?

            Exactly that is what I want to do is..

            brahmma wrote:

            Why can't you simply use global variables?

            Actually this is a iterative process. I mean first sends some data to functionOne() and functionTwo(), and do calculations and those values need to use inside the mainCal().

            I appreciate your help all the time... Eranga :)

            N R C 3 Replies Last reply
            0
            • C CodingLover

              That's make sense to me, thanks. But I have one question. I want to use x, y and dd inside the mainCal(). here,

              gleat wrote:

              void functionOne( int x, int y, int& a, int& b ) { // do some work and initialize "a" and "b" }

              you have use two reference, and that's clear to me. Then I want to call mainCal() there and send x and y. So what you have done here,

              gleat wrote:

              void mainCal( int i, int j, double k ) { functionOne( i, j ); functionTwo( k ); }

              is not clear for me....

              I appreciate your help all the time... Eranga :)

              R Offline
              R Offline
              Rajasekharan Vengalil
              wrote on last edited by
              #11

              Sorry. My bad! I assumed that x and y were inputs to functionOne and then forgot to supply those parameters when calling it! If you do not have any other input arguments you can drop those, i.e., you could something like this:

              void functionOne( int& a, int& b )
              {
              // do some work and initialize "a" and "b"
              }

              void functionTwo( double& dd )
              {
              // do some work and initialize "dd"
              }

              void mainCal( int i, int j, double k )
              {
              functionOne( i, j );
              functionTwo( k );
              }

              -- gleat http://blogorama.nerdworks.in[^] --

              C 1 Reply Last reply
              0
              • C CodingLover

                Hi all, I don't know how far my question is obvious to you. Say I have few functions, and they return different types of values. I want to use them inside an other single function. How should I do that. Any clue.

                I appreciate your help all the time... Eranga :)

                X Offline
                X Offline
                Xing Chen
                wrote on last edited by
                #12

                I seems you want to use other process's function,as your's. I suggest you do it in Inject dll,and return result to your application. hope this article will be help you: http://www.codeproject.com/library/InjLib.asp[^]

                1 Reply Last reply
                0
                • C CodingLover

                  brahmma wrote:

                  It doesn't look like you are using x, y and dd in minCal(). But is that just what you want?

                  Exactly that is what I want to do is..

                  brahmma wrote:

                  Why can't you simply use global variables?

                  Actually this is a iterative process. I mean first sends some data to functionOne() and functionTwo(), and do calculations and those values need to use inside the mainCal().

                  I appreciate your help all the time... Eranga :)

                  N Offline
                  N Offline
                  Nelek
                  wrote on last edited by
                  #13

                  And what is the problem with global/class_member variables? You can use them along your class, or between classes, without a problem.

                  Greetings. -------- M.D.V. If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you “The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson ;)

                  C 1 Reply Last reply
                  0
                  • R Rajasekharan Vengalil

                    Sorry. My bad! I assumed that x and y were inputs to functionOne and then forgot to supply those parameters when calling it! If you do not have any other input arguments you can drop those, i.e., you could something like this:

                    void functionOne( int& a, int& b )
                    {
                    // do some work and initialize "a" and "b"
                    }

                    void functionTwo( double& dd )
                    {
                    // do some work and initialize "dd"
                    }

                    void mainCal( int i, int j, double k )
                    {
                    functionOne( i, j );
                    functionTwo( k );
                    }

                    -- gleat http://blogorama.nerdworks.in[^] --

                    C Offline
                    C Offline
                    CodingLover
                    wrote on last edited by
                    #14

                    But pal, I've confusing on this also :(

                    gleat wrote:

                    functionOne( i, j );

                    Can't I call the mainCal() inside the functionOne() as, mainCal(a, b)

                    I appreciate your help all the time... Eranga :)

                    R 1 Reply Last reply
                    0
                    • C CodingLover

                      brahmma wrote:

                      It doesn't look like you are using x, y and dd in minCal(). But is that just what you want?

                      Exactly that is what I want to do is..

                      brahmma wrote:

                      Why can't you simply use global variables?

                      Actually this is a iterative process. I mean first sends some data to functionOne() and functionTwo(), and do calculations and those values need to use inside the mainCal().

                      I appreciate your help all the time... Eranga :)

                      R Offline
                      R Offline
                      Rajesh R Subramanian
                      wrote on last edited by
                      #15

                      I don't understand why can't you use global variables.


                      Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->ßRÅhmmÃ<-·´¯`·.

                      1 Reply Last reply
                      0
                      • C CodingLover

                        But pal, I've confusing on this also :(

                        gleat wrote:

                        functionOne( i, j );

                        Can't I call the mainCal() inside the functionOne() as, mainCal(a, b)

                        I appreciate your help all the time... Eranga :)

                        R Offline
                        R Offline
                        Rajasekharan Vengalil
                        wrote on last edited by
                        #16

                        You can call any function from any other function. The point is to understand the use of passing parameters by reference. If you have a function declared like so for instance:

                        void foo( int& a )
                        {
                        a = 10;
                        }

                        And you called it like so from somewhere:

                        int i;
                        foo( i );
                        cout<It'd print 10. See here[^] for more information.

                        --
                        gleat
                        http://blogorama.nerdworks.in[^]

                        C 1 Reply Last reply
                        0
                        • N Nelek

                          And what is the problem with global/class_member variables? You can use them along your class, or between classes, without a problem.

                          Greetings. -------- M.D.V. If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you “The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson ;)

                          C Offline
                          C Offline
                          CodingLover
                          wrote on last edited by
                          #17

                          It's true pal. But if I change my code now it can be a real mess for me. Because I have to change lots of coding parts in my code.

                          I appreciate your help all the time... Eranga :)

                          R N 2 Replies Last reply
                          0
                          • C CodingLover

                            It's true pal. But if I change my code now it can be a real mess for me. Because I have to change lots of coding parts in my code.

                            I appreciate your help all the time... Eranga :)

                            R Offline
                            R Offline
                            Rajesh R Subramanian
                            wrote on last edited by
                            #18

                            I don't think it's so difficult. Just declaring the variables which you want to access from multiple functions globally will do all the magic for you. That's the reason why global variables exist. Do remember that global and static variables can be bad if you use them carelessly.


                            Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->ßRÅhmmÃ<-·´¯`·.

                            1 Reply Last reply
                            0
                            • C CodingLover

                              brahmma wrote:

                              It doesn't look like you are using x, y and dd in minCal(). But is that just what you want?

                              Exactly that is what I want to do is..

                              brahmma wrote:

                              Why can't you simply use global variables?

                              Actually this is a iterative process. I mean first sends some data to functionOne() and functionTwo(), and do calculations and those values need to use inside the mainCal().

                              I appreciate your help all the time... Eranga :)

                              C Offline
                              C Offline
                              CodingLover
                              wrote on last edited by
                              #19

                              Ok, here is a real scenario may that why I have use this. Mainly I've read a file and find different data from it, on different functions. On the same function I store them in a database. That mean I've store data on the database in different places(ie: on different functions). What I want to do is, store all the data on a single function. I think it is clear to me, when I look at the code. May be I can use those codes later.

                              I appreciate your help all the time... Eranga :)

                              1 Reply Last reply
                              0
                              • R Rajasekharan Vengalil

                                You can call any function from any other function. The point is to understand the use of passing parameters by reference. If you have a function declared like so for instance:

                                void foo( int& a )
                                {
                                a = 10;
                                }

                                And you called it like so from somewhere:

                                int i;
                                foo( i );
                                cout<It'd print 10. See here[^] for more information.

                                --
                                gleat
                                http://blogorama.nerdworks.in[^]

                                C Offline
                                C Offline
                                CodingLover
                                wrote on last edited by
                                #20

                                Ok, I got it. Thanks for the link as well.

                                I appreciate your help all the time... Eranga :)

                                1 Reply Last reply
                                0
                                • C CodingLover

                                  It's true pal. But if I change my code now it can be a real mess for me. Because I have to change lots of coding parts in my code.

                                  I appreciate your help all the time... Eranga :)

                                  N Offline
                                  N Offline
                                  Nelek
                                  wrote on last edited by
                                  #21

                                  Declare the variables at the header, initialize them in the constructor and use "search and replace" to change the names is a lot of work? What do u prefer? Do that "lot" of work now? Or having scope/access problems in every thing you code afterwards?

                                  Greetings. -------- M.D.V. If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you “The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson ;)

                                  C 1 Reply Last reply
                                  0
                                  • N Nelek

                                    Declare the variables at the header, initialize them in the constructor and use "search and replace" to change the names is a lot of work? What do u prefer? Do that "lot" of work now? Or having scope/access problems in every thing you code afterwards?

                                    Greetings. -------- M.D.V. If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you “The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson ;)

                                    C Offline
                                    C Offline
                                    CodingLover
                                    wrote on last edited by
                                    #22

                                    Now actually I have no idea what to do... :| Here is a one function I've used. void CSRFDBDlg::GetMess(int length, char * buffer, ADODB::_RecordsetPtr pRecSet) { UpdateData(TRUE); _variant_t vtData; // VARIANT data type long nRetLength = 0; // Dummy value _variant_t vAppendChunck = BinaryToVariant((BYTE*)buffer, length, nRetLength); pRecSet->Fields->GetItem("Bio_Data")->AppendChunk(vAppendChunck); vtData.vt = VT_I2; vtData.iVal = (int)nRetLength; pRecSet->Fields->GetItem("Bio_Size")->PutValue(vtData); } Here I have two values to store in the database.

                                    I appreciate your help all the time... Eranga :)

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups