Can you inherit from one function to another?

Ok I am writing a program and I am trying to figure out can you inhert from one function to another?

Please elaborate.

A function can call an another function.
A lambda function can use the variables of surrounding (function) scope.
A function can return value and pass parameters by reference.
I have a program I am supposed to write for school where you use a structure to set the variables and etc. then u create a function to get the data then you create a function to output the data. I cant for the life of me figure out how to use the DATA from the function i collect data into the function I want to use to output the collected data. It is eluding me badly
closed account (EwCjE3v7)
I dont think u know much about functions

U may wanna skim over them again

But what ur saying is that u get the data into the function, but u dont know how to give that data to the output function?

Well functions have a return type and parameters, let me show u something

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
string DATA(string &s)
{
   // do some stuff with s
   return s; // return the string
}
void output(const string &s) // void returns nothing
{
   cout << s << endl;
 }

int main ()
{
  string myDATA = "Hi";
  output(DATA(myDATA)); // calls the DATA function and then uses the string returned by that and calls output
  return 0;
}

Please excuse any errors above, as I wrote this from my ipad
If u dont know srrings then replace every string to a int
Last edited on
use a structure

Are we talking about member functions of a class/struct?

http://www.cplusplus.com/doc/tutorial/classes/

Or perhaps function reference parameters?

http://www.cplusplus.com/doc/tutorial/functions/
@adikted
it's because you stored your data inside the function that collected it.

move the data declaration (where you save collected data) ouside of both functions so they can both see it.

1
2
3
4
5
6
7
8
9
10
11
int myData;

void collect()
{
  myData = 5;
}

void output()
{
   cout << myData;
}
closed account (EwCjE3v7)
Do not do what @Jaybob66 said, it is bad practice. You should avoid global variables.
You can google search and 90% of the people will tell u that they are bad, the other 10% are beginners like me and u and dont know yet.

So yea dont use them, instead us parameters and return types.
@Xp3rtHammor

I wasn''t suggesting that the data should be global, i suggested moving it so that it was in scope for both functions. What scope that is depends on the structure of the OP's code.
@Jaybob66: The way you wrote it, it seems like suggesting a use of global. Lets assume that Murphy's Law has full effect.

In essence the Xp3rtHamm0r's example has the data outside, but seen via parameters. However, that function DATA() has an extra complication:
It does return a (copy of) value that it got as reference parameter.
1
2
3
4
5
6
7
8
// This is same
string s, t;
t = DATA( s );

// as this
string s, t;
DATA( s );
t = s;

While the more the merrier, the KISS principle has clear merit.


Encapsulation is a useful keyword. It can make Jaybob66's code safe and sound:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Foo
{
  int myData;
public:
  void collect()
  {
    this->myData = 5;
  }

  void output()
  {
     std::cout << this->myData;
  }
};

int main ()
{
  Foo bar;
  bar.collect();
  bar.output();

  return 0;
}
closed account (EwCjE3v7)
@keskiverto

Here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
string &DATA(string &s)
{
   // do some stuff with s
   return s; // return the string
}
void output(const string &s) // void returns nothing
{
   cout << s << endl;
 }

int main ()
{
  string myDATA = "Hi";
  output(DATA(myDATA)); // calls the DATA function and then uses the string returned by that and calls output
  return 0;
}
Return a reference? More fun, less simple.

The simple options are:
1
2
3
4
5
6
7
8
9
10
11
12
13
// a function that returns a new value
string foo()
{
  string s;
  // do some stuff with s
  return s;
}

// a function that changes an existing value
void bar( string & s )
{
  // do some stuff with s
}
closed account (EwCjE3v7)
Oh please ignore my previous post. I was thinking about something else :D
@keskiverto

your "Encapsulation" is what was in my head but wasn't sure of the OPs context so took the lazy option, blaming Murphy sounds good to me ;)


Topic archived. No new replies allowed.