• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

functions in C++

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

andyl33t

Member
Joined
Nov 25, 2004
Location
U.K
hi, i dnt understand functions at all. like what they are? why you have them and what they do. could someone explain them 2 me?
Thanks
 
Hi andy

I am just starting to learn c++ too, have a look here there is a ton of info.

Hope this is of some help.
 
I understand the need for functions now, jst not sure how to inpliment them.

Like:

int mult ( int x, int y );

does the 'mult' mean multiply the 2 integers x and y?

What are the other commands such as 'mult'?

And also if you have a void function, it does not return an answer, so if it doesnt give you an answer, whats the point in having it?

Thanks
 
The name doesnt mean anything...its what you do inside the function that matters. It could be int asdf(int x, int y) or int ajsdfwaiuoejrou(int f, int aowerp) and it would not still do the same thing.
Basically, a function will return a value...it will do some processing and then return something. With 'void,' it doesn't return anything, so it will only process data. Here's a sample function:
Code:
int asdf(int a, int b)
{
//remember it doesnt have to be 'a' or 'b', it can be anything just as long as you remember to be consistent
int value1;
value1=a*b //if i had the variables as 'q' and 'zps' or something then it would be value1=q*zps see?
return value1; //all this does is make the function return what we got for value1.
//so if i said in the main procedure or something 'thisvariable=asdf(10,20)'
//then it would return what value1 got when we put in 10 and 20 into the function.
//since the function multiplies them together and returns what it got from doing that, 
//'thisvariable' will contain the return value, which would be 10*20 or 200.
}
 
ohh right ok. but when
return value1;
where does it write the value for value1? do you have to right in the main something like
cout >> value1;
?

Also with the void thing, what do you mean by it wont return anything?

Thanks

edit: dont worry, i understand it now, thanks for the help
 
Last edited:
if I wanted to add two numbers, say to dice that have already been rolled, you could do something like this:

public int addDice(n1,n2)
{
int die1 = n1, die2 = n2;
return (die1+die2);
}


Sorry thats in java but it should still be understandable.

And to use it:
int total;
die1 = 5, die2=4;
total = addDie(die1,die2);

Of course once you get into Objects... I'm sure you will be back :p
 
Here are some examples of simple functions:

Code:
#include <iostream>
#include <stdlib.h>

using namespace std;

int multiply(int a, int b);

int main()
{
  cout << multiply(2, 4);
  system("pause");
  exit(0);
}

int multiply(int a, int b)
{
  int c = a * b;
  return (c);
}

Code:
#include <iostream>
#include <stdlib.h>

using namespace std;

void printmessage();

int main()
{
  printmessage();
  system("pause");
  exit(0);
}

void printmessage()
{
  cout << "This is my message";
}
 
Last edited:
so the 'void' tells the program not to pass the flow back to the main after it has done a function?
 
No, it still passes flow back, but doesn't return a value. This is used when a function has side affects and no result. Look at this code:

Code:
void hello();

int main()
{
  hello();
  cout << "Goodbye world." << endl;
}

void hello()
{
  cout << "Hello world." << endl;
}

All that hello() does is print something. It doesn't actually need to return anything, so it has a void return type. When its done the flow goes back to main() which prints its message.
 
shouldnt i look like this:


#include <iostream>
#include <stdlib.h>

using namespace std;


void hello()
{
cout << "Hello world." <<endl;
}
int main()
{
hello();
cout << "Goodbye world." << endl;

system("pause");
exit(0);
}
 
Last edited:
Yeah, I left out some of the front matter.

Code:
#include <iostream>  // This is required for cout and endl
#include <stdlib.h>  // This is required for system and exit

using namespace std; // This is required for cout and endl

void hello()
{
  cout << "Hello world." <<endl;
}
int main()
{
  hello();
  cout << "Goodbye world." << endl;

  system("pause"); // This isn't portable so it is bad to use in examples.
  exit(0);         // This isn't required.
}

I can explain what any of those pieces do if you are confused.
 
Back