Home > Software engineering >  Using variables in a function from another function
Using variables in a function from another function

Time:02-11

In function foo() there is a loop that iterates until it finds an optimum set of variables and determines that as the ideal set. The function only returns one variable, in order to pass a unit test.

In the next function bar(), I need to output all of the variables in function foo() as it iterates. First output the optimum set of variables, and then the rest of the possible variable sets seperately.

int foo(int a, int b) {
    int c, d;
    while ( etc. ) {
        c = arithmetic_for_c;
        d = arithmetic_for_d;
        e = c   d;
    }
    return e;
}

int bar(a, b) {
    cout << e;
    cout << c << d;
}

This example is very simple, but you get the idea.

I have a feeling references (int&, string& etc) would help somehow, but I'm not sure how they would be used here.

I tried to put together a global array but that seemed to get a bit too complex for the scope of this assignment.

The loop is a necessity, but also seems to ruin any hope for variables or arrays in the global scope.

Unfortunately there are a number of things we haven't learned yet, so there is likely a solution I can't use yet.

Thoughts?

Thank you!

CodePudding user response:

If I'm not mistaken, this is not really possible in C , as the variables are declared in the scope of the function foo, and cannot be accessed from a different scope. But you can always use something like this:

Pass by reference (out parameters):

#include <iostream>

// Declaring bar earlier as it has to be accessed by foo
int bar(int& c, int& d) {
    int e = 0; // Declaring and initializing variable e

    int count = 0;

    // Loop
    while (count < 10) {
        c  ;
        d  = 2;
        e = c   d;

        count  ;
    }
    return e; // Return variable e
}

int foo(int a, int b) {
    int c, d;
    c = a, d = b; // setting c = parameter a, d = parameter b
    int e = a   b;

    std::cout << c << d << std::endl; // Printing variables c and d
    std::cout << e << std::endl; // Printing variable e

    e = bar(c, d); // Calling bar function. Also this function increments c by 10 and d by 20.

    std::cout << c << d << std::endl; // Printing variables c and d
    std::cout << e << std::endl; // Printing variable e

    return e; // Return variable e
}

int main() {
    foo(10, 20); // Calling function 'foo'
}

This is just an example.

CodePudding user response:

The CPU is performing one thread at a specific time. So it can only perform the code in function foo or the function bar, but never both. The scope of the variables in the function are called local or auto variables. They are valid only in the context of the execution of the function. At language level you will say they go out of scope at the closing } bracket. Technically, the memory for the variable is allocated temporarily while entering the function and automatically released on exit. It's just a memory location on the stack. The live time of the variable ends at the end of scope.

So, you must always look at the scope of a variable. You can't access anything temporarily allocated on a different function's stack. The game changes if one function calls another. You can pass a reference from the calling function to the called function, but not the other way.

int foo(int a, int b, int& c, int& d) {    
    while ( etc. ) {
        c = arithmetic_for_c;
        d = arithmetic_for_d;
        e = c   d;
    }
    return e;
} // scope of the reference c and  ends here, 
  // but not the scope of the referenced variables.

int bar(a, b) {
    int c, d;

    // here you define the role of calling function and called function.
    int e = foo(a, b, c, d);  

    cout << e;
    cout << c << d;
} // scope of c and d ends here.

CodePudding user response:

Unless you want to go really fancy (probably not within your reach, yet), foo() has to help bar() a little.

Since you want to show the end result first, then the intermediate data later, you will have to find some way of storing the intermediate states. You could do so, using arrays or lists and push the intermediate values into them.

But another, probably shorter option is, to just store the intermediate output.

You know how to use std::cout by now, which prints output to the console. It is of type std::ostream. And next to output to a console (or file etc.), the c standard library also allows to output to a string. So, for your use case to work, you create such a string stream, then call bar and give it as output stream the string stream. At the end of your calculations, you call bar with the regular output stream to print the end result, then, you output the string of the string stream to the regular output stream.

It sounds more convoluted, than it actually is, if you see it in code:

#include <iostream>
#include <sstream>

void bar(int a, int b, int c, int d, int e, std::ostream& os) {
  os
    << "a: " << a
    << " b: " << b
    << " c: " << c
    << " d: " << d
    << " e: " << e
    << std::endl;
}

int foo(int a, int b, std::ostream& os ) {
  std::ostringstream ossteps;
  int c=  0;
  int d=  1;
  int e= 42;
  while (c < 10) {
    bar(a,b,c,d,e,ossteps);
    c  ;
    d  = c*c;
    e = (a * b) - d;
  }
  bar(a,b,c,d,e,os);
  os << ossteps.str();
  return e;
}

int main (int argc, const char* argv[]) {
  int efinal = foo(1,2, std::cout);
  return 0;
}

  •  Tags:  
  • c
  • Related