Help with functions (C++ programming)

Started by
10 comments, last by Toadhead 20 years ago
Hi! I need help with C++ programming, what exaclty do functions?? specialy were can you use them for, why do they return to a value, and to WHAT value do they return? Were do you use them for etc. Help would be apreciated [edited by - Toadhead on April 12, 2004 1:56:26 PM]
Advertisement
Functions let you group code together so that piece of code can be reused over and over. Functions can also accept data, as well returning data. Functions break down like this:

return-type function([Parameters]);

If you are making your own function, it can have as many parameters as you want. Say, for example, you have a function to add to numbers and then it outputs to the screen:

#include <iostream>using namespace std;void Add(int X, int Y);void Add(int X, int Y) {  cout << "Adding those numbers you get: " << X + Y << endl;}int main() {  Add(5, 3);  Add(1, 2);  return 0;}


Notice at the top after the namespace, I rewrote the function. This is called a prototype, a function decleration. Try compiling this code below.

#include <iostream>using namespace std;int main() {  Add(5, 3);  Add(1, 2);  return 0;}void Add(int X, int Y) {  cout << "Adding those numbers you get: " << X + Y << endl;}


error C2065: ''Add'' : undeclared identifier

Or something similar will appear. This is because main cannot see the function Add.

Now what about the return keyword? Well, say, instead of having our Add function display the result, we want it to return it to us. Well, since the function is going to return an integer value, we''ll need an int return type.

#include <iostream>using namespace std;int Add(int X, int Y); //Notice I changed to intint Add(int X, int Y) {  return (X + Y);}int main() {  cout << "Two numbers added: " << Add(3, 5) << endl;  cout << "Two other numbers added: " << Add(Add(4, 3), 2) << endl;  int X = Add(34, 12);  cout << "X = " << X << endl;  return 0;}


This code that I have given you is not at all useful. Seeing as how you can just use + to add numbers. But it should give you an idea of how functions work.

Where I use functions depends on what I am programming. I like to use functions to split my code up into sections, or for specific tasks. Such as in a game, I may have:

- OnDraw() //Does all the drawing of game images
- OnEvent() //Handles all the input events
- OnLoop() //Main loop
- OnMessage() //Handles messages from controls (Buttons, etc...)
- etc...

This makes it much easier to isolate a problem, as well as maintaining code. A specific task example I may have is my Blit function. (I use SDL if anyone is wondering)

Note: Blit, basically means to Draw. So when I say I am blitting an image, you could say I am drawing the image to the screen.

void Blit(SDL_Surface *Dest, int DestX, int DestY, SDL_Surface *Src) {	SDL_Rect DestR;	DestR.x = DestX; 	DestR.y = DestY;	SDL_BlitSurface(Src, NULL, Dest, &DestR);}


Now I don''t want to get too technical, but don''t you think that it would be rather (lack of a better word) stupid to rewrite that code everytime I want to Blit an image? This function takes care of it for me.
The return value is just the answer (shut up 1337157s, I'm being simple). For instance:

#include <iostream>using namespace std;int Add(int x, int y);int main(){ int a; int b; int c; a = 2; b = 7; c = Add(a,b);  cout << c; return 0;}Add(int x, int y){ return (x+y);}


See? The return value got stored in the variable c.

[edited by - Drakkcon on April 12, 2004 3:19:48 PM]
Drakkcon, you have a few errors:

1. Forgot a semi-colon after the std
2. You didn''t prototype the Add function, so main cannot see it. (Try compiling it)
Holy crap that was stupid of me! There, I fixed it.

[edited by - Drakkcon on April 12, 2004 3:20:12 PM]
WOW!

Thanks alot MetaCipher and Drakkcon !! I fixed Drakkcons erros in the script myselve before he changed it

Now I finaly understand it.. I think I'm gonna look for an new tutorial for C++ becuase the one I use currently explain things very difficult :/



Ow.. btw, how do you create such white box were you can put your code in?

[edited by - Toadhead on April 12, 2004 3:53:18 PM]
you use the word "source" surrounded by brackets (I can't write it of course). so [] would surround source.
To stop it put brackets around /source.


[edited by - Drakkcon on April 12, 2004 4:09:22 PM]
thanks..I know how it works with closing etc. most forums use "Code" instead of "source"...
[code][/code] makes text have use a fixed width font, and doesn't allow word-wrap (be careful, long unwrapped lines can get very annoying).
//Like this://  (good for just a few lines of code)int AFunction(){  return 0;}   
[source][/source] makes the white box.

And if you really want to write out forum tags, then you can use &#91; and &#93; for '[' and ']', or you can just break the tag up with an empty <B></B> tag (or any HTML tag, really), so that "code" becomes "co<B></B>de". Or at least, I'm trying it for the first time right now, and hoping it'll work. And if you need to use '<'; or '>', then that would be &lt; and &gt;

[edited by - Agony on April 12, 2004 4:28:48 PM]
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
[source] < iostream> , yay!

[edited by - Drakkcon on April 12, 2004 4:29:51 PM]

This topic is closed to new replies.

Advertisement