Okay I really hate making multiple threads, but I was wondering how to create functions. Like my own functions that are like
fry()
beat()
distort()
You get the idea. Just examples of verbs that I don't think are real standard functions.
Okay I really hate making multiple threads, but I was wondering how to create functions. Like my own functions that are like
fry()
beat()
distort()
You get the idea. Just examples of verbs that I don't think are real standard functions.
Function:
Code:#include <iostream> #include <string> void fry(std::string &food) { std::cout<<"You fried "<<food; } int main() { std::string chicken = "Chicken"; fry(chicken); return 0; }
Woop?
What does this mean? (std::string &food)
And that void fry() at the beginning... were you declaring the function perhaps?
I guess I just don't understand the whole "std::string" thing. Sorry I'm a noob (as if you couldn't tell already .
std::string is the string class provided by C++. I was defining the function if I wanted to prototype it I would have wrote just void fry(std::string &food) and defined it after main(). As for what std::string &food does it is passing the string object itself not a copy so it is "faster" if you wanted you could just write it as void fry(std::string food) and for this function it would produce the same results.
Woop?
also you can replace the return type (void for the example above) at the begining to something else
Code:int add(int int1, int int2) //the int can be replayed with any type { return int1 + int2; } int main() { cout << add(2,2) << endl; //this will display 4 int integer = add(1, 3); //integer is now 4 return 0; }
Also you can include <iostream>.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Here is a common way to use functions..
Code:#include <iostream> #include <string> int mult(int x, int y) { return x * y; }
I'm a beginner C++ programmer, but I have studied HTML and Java. So if you need to help me I should catch on fast =)
The void means it has no return type.Code:void fry () { //put code here } void beat () { //put code here } void distort () { //put code here } // Here is where your programs "heart" is... When you run the // .exe it starts with the first line from main as you probably know... // so we can call your functions right here int main () { fry (); beat (); distort (); return 0; }
A return type is when a function can return a value like lets say your function does 5 + 5 and returns 10 because its the sum. Then you can have a variable like...
"int sum = function(); "
function returns an integer that is assigned to sum.
This will give you a good explanation:
http://www.mvhs.fuhsd.org/bob_vanhoy/pdfs/lesson06.pdf
http://www.mvhs.fuhsd.org/bob_vanhoy/pdfs/lesson07.pdf
Last edited by JoshR; 07-22-2005 at 02:37 PM.
You can call your functions multiple times, too.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.