The Partridge Family were neither partridges nor a family. Discuss.
-
cletus8u
- Posts: 3
- Joined: June 1st, 2012, 1:21 am
Post
by cletus8u » June 1st, 2012, 1:36 am
So I've mostly set c++ to the side to focus on c# because that's the language my local community college focuses on. While reading Head First C# I learned that functions can take functions as parameters, I wondered if this worked with c++ so this is what I came up with:
Code: Select all
#include <iostream>
using namespace std;
class testies
{
public:
int balls;
int addThis(int number)
{
int result;
cout<<"Class testies is adding the number and running the function"<<endl;
result=number+20;
cout<<result<<" This is the result of testies class adding 20 to parameters."<<endl;
return result;
}
};
void multiply (int num1)
{
int product;
product = num1*20;
cout<<"This is the product of the initial number added to testies function then multiplied "<<endl;
cout<<product<<endl;
}
int main()
{
testies juevos;
multiply(juevos.addThis(10));
return 0;
}
While coming up with code on my own I realize I keep ending up creating classes with either testicles or boobs, I told my gf I need to force myself to write some with zombies or something. Anyways I was really excited that this worked because I figured that if a function could take a function as a parameter then it would only use the returned value and not actually run the code therein. This is the only place I could think of where I could share what I learned and I thought people might understand what it is I learned and give a crap. Hopefully somebody else can use this as well in their continued understanding of programming.
-
chili
- Site Admin
- Posts: 3948
- Joined: December 31st, 2011, 4:53 pm
- Location: Japan
-
Contact:
Post
by chili » June 2nd, 2012, 11:56 pm
To be technically correct, you're not passing one function to another as a parameter. You're calling one function (addThis) and then passing it's result directly to another funciton (multiply).
You can pass one function to another in the form of a pointer-to-function, but that is a completely different matter.
Chili
-
cletus8u
- Posts: 3
- Joined: June 1st, 2012, 1:21 am
Post
by cletus8u » June 3rd, 2012, 10:43 pm
Alright, thanks for the information.