Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Stormrage

Usefulness of Functions and code help!

This topic is 5578 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well, I'm learning all about functions now and I've made a little program that gives you the SQUARE of the numbers you type in. You get the answer after you type in a negative number. For example, if i typed 2 and 2 and -2 I should get an answer of 4. If I type in a negative number twice in a row, the "game" should quit. For some reason however, it isn't doing this. What have I done wrong? Also, I'm confused with these Functions. What exactly are they useful for? In other words, what do they do/how do they work? Thanks for your help.
#include <stdio.h>
#include <iostream.h>

double square(double doubleVar)
{
return doubleVar * doubleVar;
}
int sumSequence (void)
{
int accumulator=0;
for(;;)
{
double dValue=0;
cout<<"Enter next number: ";
cin>>"dValue";
if (dValue<0)
{
break;
}
int value=(int)square(dValue);
accumulator=accumulator + value;
}
return accumulator;
}
int main(int arg, char* pszArgs[])
{
cout<<"You know what to do!\n";
int accumulatedValue;
do
{
cout<<"\nEnter Next Sequence\n";
accumulatedValue=sumSequence();
cout<<"\nYour total is ";
cout<<accumulatedValue;
} while(accumulatedValue !=0);
int i;
cout<<"Press any key + ENTER to exit\n";
[edited by - Stormrage on June 17, 2003 2:31:42 PM]

Share this post


Link to post
Share on other sites
Advertisement
Functions are snippets of code put together and given a name, so that you can execute that code just by calling the appropriate function. Take a look at this, for example:
int DrawSprite(int x1, int y1, int x2, int y2, CTile *lpTile)
{
// Render a quad with the specified size and texture

unsigned uTex = lpTile->GetTexture();
if(m_uCurrentTexture != uTex)
{
glBindTexture(GL_TEXTURE_2D, uTex);
m_uCurrentTexture = uTex;
}

glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2i(x1, y1);
glTexCoord2f(0.0f, 1.0f); glVertex2i(x1, y2);
glTexCoord2f(1.0f, 1.0f); glVertex2i(x2, y2);
glTexCoord2f(1.0f, 0.0f); glVertex2i(x2, y1);
glEnd();
return 0;
}


This piece of code draws an image onto the screen. Now, if I wanted to draw an image, I could copy+paste the code, but that would take up more memory and make the code harder to read. That''s where functions come into play. Instead of typing all of the above code again and again, I put it together under the name "DrawSprite".
Functions are stored in only one place (as opposed to copy+pasteing code), and when you call a function, the control flow is transferred to that place. This gives a very tiny performance hit (negligible, actually), but makes up for it by saving memory and making the code easier to understand.


For crying out loud, she has fishes coming out of her head on either side. How can you find this hot?!

Share this post


Link to post
Share on other sites
Ah i see! So instead of painstakingly writing the code again and again you could just use a function to call upon when you need it :D My code is still not working though...

Share this post


Link to post
Share on other sites
Hi Stormage,
I dont mean to be rude here but its really hard to read your code, indenting might make it a little easier for you to read and see any mistakes.
hope you get it sorted

Share this post


Link to post
Share on other sites
try this


#include <stdio.h>
#include <iostream.h>

double square(double doubleVar)
{
return doubleVar * doubleVar;
}

int sumSequence (void)
{
int accumulator=0;

double dValue=0;

while(true)
{
cout<<"Enter next number: ";
cin>>dValue;

if (dValue<0)
{
break;
}

int value=(int)square(dValue);

accumulator=accumulator + value;
}

return accumulator;
}

int main(int arg, char* pszArgs[])
{
cout<<"You know what to do!\n";

int accumulatedValue;

cout<<"\nEnter Next Sequence\n";

accumulatedValue=sumSequence();

cout<<"\nYour total is ";
cout<<accumulatedValue;

cout<<"Press any key + ENTER to exit\n";
}


this will make your program ask you numbers until you enter a negative number and will output the sum of all the square of the numbers you enter

Yann L POWNS Carmack

Share this post


Link to post
Share on other sites
The program works now! Great! Also, thanks for the indent tip for my code (i might as well do it before i get into bad habits) :D.

I have another question about Functions, however. What did my:
double square(double doubleVar) function do?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
double square(double doubleVar)
{
return doubleVar * doubleVar;
}

This will take in a copy of the variable you pass into it and then return the square of that value.

Might I suggest the following:

double square(const double& doubleVar)
{
return doubleVar * doubleVar;
}

It doesn''t look like much of a change but it ensures that you don''t make a copy of the value(which is good for lessening memory consumption) plus it tells the compiler not to change the value of the variable you are passing in(which would be a bad thing in this case).

Both functions are used the same way so do which ever you feel is best.

Something tells me that I might be mistaken about this, in which case someone will correct me and belittle me any second now.

Share this post


Link to post
Share on other sites
There's something about distrupting program flow with breaks that bugs me. I would change the program like this:


int sumSequence()
{
int accumulator=0;
double dValue=0;

do
{
accumulator+=(int)square(dValue);
cout<<"Enter next number: ";
cin>>dValue;
}while(!dValue<0);

return accumulator;
}


Edit: Oh yeah and regarding the usefulness of functions, it isn't just about saving us the trouble of typing the same codes over and over; it's also to help us organize our program and break it down into smaller parts/tasks. To really see the usefulness of functions you have to imagine programming in a world where functions didn't exist. Can you imagine programming like that? It would be like trying to do everything in one place. You can do without functions in simple programs but once you start writing more complex programs I can guarantee you that your program will get VERY messy if you don't try to break your program down into smaller task.

[edited by - Greatwolf on June 17, 2003 9:07:13 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!