Advertisement

Functions,Functions functions

Started by January 20, 2002 10:19 PM
4 comments, last by gamesmaster2 22 years, 7 months ago
Alright I just got started here so bear with me if I''m asking a dumb question.Alright I checked out the for beginners section and decided that I would go the long way around and learn c++.I got a for dummies book for c++ and I breezed through the first parts of the book.Then I got to functions.I can''t say I''m confused I understand the wording all right but until this point I was able to close the book and come up with some sample program that wasn''t in the book so I know that I got the concept down you know.But for some reason I can''t get my mind around functions.If you know of any other resources that worked for you when you first got started.Whether it''s a website that broke things down for you or maybe a book that you found real helpful in that area.Or if you have some little words of wisdom that you think will help me out please tell me.
The road to hell is paved in good intentions
I don''t know any particular resources for learning that, but if you ever have any particular question about them - I''m sure someone here would help you out.

Since it sounds like your problem is figuring out what functions are for, just thing about it like this.

Your source code could just be one giant main function, with lots of repitition and what not, or when you see yourself typing something more than once, you could put that in a function - for a simple example (not necessarily practical though)...

//a) without additional functions
void main()
{
double temperature1;
double temperature2;
double temperature3;

temperature1 = (57. - 32.)/1.8;
temperature2 = (63. - 32.)/1.8;
temperature3 = (21. - 32.)/1.8;
}

//b) with additional functions
void main()
{
double temperature1;
double temperature2;
double temperature3;

temperature1 = FahrToCelc(57.);
temperature2 = FahrToCelc(63.);
temperature3 = FahrToCelc(21.);
}

double FahrToCelc(double tempFahr)
{
return (tempFahr-32.)/1.8;
}

So in essance, the point in this example is that not only do functions cut down on what you have to type, but also the chances that you''ll make errors (just imagine this were alot more complicated - and you used the first way) - the odds that you''d make a typo somewhere and say accidently put a 3.2 when you meant 32. are pretty good.

So just remember, if you see yourself typing something (substantial) more than once, you could probably stick it in a function.
Advertisement
Just get a book on comuter science it will get u started. A basic way of thinking of it is instead of having to right the same code over and over use functions. For example lets say u wrote some code that is 1000 lines long and uses 100 variables to get the job done. Now lets say u need to use this code multiple times. Well we could just copy and paste the code right, well what problems arise?

Lets start with source code problems:
1.Readability/Understandablity.
2.Big Source Files.
3.Longer to compile.
4.will still have to rename the input varibles for each instance of the code.
Problems with Object/executable code:
1.Bigger Code.
2.With really big code segments and how performance nowadays is so dependent on cache, smaller code can be faster.

The bigger executable code is because the compiler actually makes as many copies of the code as u use in the source. So when u use functions the file is smaller because your code is only present once. The code just gets called upon every time u actually use it in your source. I hope that helps, and i hope i didn''t confuse u anymore.



-potential energy is easily made kinetic-

-potential energy is easily made kinetic-

Thanks alot for your quick reply.Yeah I gotta admit finding a reason to use a function was alluding me.Maybe once I get started on making larger programs I''ll find more use for them.I hope so I''m getting tired of reading chapter 6.
The road to hell is paved in good intentions
No infinisearch you didn''t confuse me too bad.Basically Functions are around so you don''t have to keep typing the same code over and over.You can just call up the code at the approiate (did I spell that right) time.I guess the big thing for me right now is that I don''t know if I learned it or not because I can''t really think of a way to use it right now.So far all the programs I''ve been working on have been pretty simple things.I guess I''ll go on to arrays and if I''m still having problems I''ll come back to it.
The road to hell is paved in good intentions
Hey, I just did a quick search on google on "why use functions" (google is your friend - just like gamedev).

Some more reasons why to use functions...

http://www.cs.utah.edu/~hamlet/release/lessons/fortran08/fortran08/node4.shtml

http://www.cs.utah.edu/~zachary/computing/lessons/uces-10/uces-10/node11.html

And some examples of uses and why to use functions

http://www.infocom.cqu.edu.au/Staff/Mike_Turnbull/Home_Page/Lecturetts/Sect1.htm

Best of luck to you as you go about learning C++.
-lao

This topic is closed to new replies.

Advertisement