Page 1 of 1

Tutorial on Functions Introduction to writing functions Rate Topic: 2 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=27004&amp;s=afd9c72b871ecd40fedeca0f7dad8db7&md5check=' + ipb.vars['secure_hash'], cur_rating: 3, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 Pontus

• Cattlebruiser

Reputation: 18
• Posts: 612
• Joined: 28-December 06

Posted 22 April 2007 - 11:02 AM

Functions,

This is a tutorial on how to create functions and what you can do with them.
This is the syntax used to declare functions:
```"return data type" "name of function" ("all the parameters, seperated by a comma");

Examples:
void noParameters(); //Takes no paramerers, returns nothing.
void test (int a, int b); //Takes two integer parameters, returns nothing.
int sum(int a, int b); //Takes two integer parameters, returns an integer.
```

As you can see, you dont need to include parameters.

```#include <iostream>

using namespace std;

void testfunction(); //you need to declare each function just like a variable

int main(){
return 0;
}

void testfunction(){ //here goes the code in your function
cout<<"This is a function"<<endl;
}
```

As you can see, it is quite simple. Declare a function, write that function and call it.
Ok, so if you want to let your function calculate something you need to give your function parameters.
```#include <iostream>

using namespace std;

void calculate(int a,int b); //you need to give the parameters after the name of your function

int main(){
return 0;
}

void calculate(int a,int b){//here goes the [code] in your function
int c=a+b;
cout<<"the solution is "<<c<<endl;
}
```

Here you go, a function that takes two parameters and calculates the sum.
Now if you dont want to display the result, instead you can calculate two numbers and give the sum as value for a third integer. You can do this in two ways:
The first 1, using return
```#include <iostream>

using namespace std;
int calculate(int a;int b); //yes, an integer as a function

int main(){
int sum = calculate(10,20);
cout << sum; //the integers' value is now 30
return 0;
}

int calculate(int a,int b)
{
c = a + b;
return c; //return the value of the int c
}
```

This might look strange but the function will be handled as an ordinary integer, you can use it to assign it to an integer or you can display it directly:
```cout<<calculate(10,20)
```

Now, the second way is using referances:
```#include <iostream>

using namespace std;

void calculate(int a,int b,int& c); //you need to give the parameters after the name of your function

int main(){
int sum;
calculate(10,20,sum);
cout<<sum;//the integers' value is now 30
return 0;
}

void calculate(int a,int b,int& c) //the last parameter is a reference to an int, this will hold the sum of the 2 first integers
{
c=a+b;
}
```

Let me explain this: if you give a parameter to a function it will make a copy of it, so it wont change the values, by giving it a reference, it will change the value of the refered integer.
Sometimes this will be useful and sometimes it wont.

Is This A Good Question/Topic? 2

Replies To: Tutorial on Functions

#2 eredeath

Reputation: 0
• Posts: 5
• Joined: 14-September 07

Posted 30 September 2007 - 04:58 PM

Good tutorial :-)

#3 Elcric

• D.I.C Regular

Reputation: 102
• Posts: 453
• Joined: 02-May 09

Posted 02 May 2009 - 11:27 AM

Excellent! Can a C++ function return more than one piece of information?

#4 Pontus

• Cattlebruiser

Reputation: 18
• Posts: 612
• Joined: 28-December 06

Posted 03 May 2009 - 09:47 AM

Nope, It can return only one. If you want it to modify more than one piece of information, you can use parameters.

#5 Elcric

• D.I.C Regular

Reputation: 102
• Posts: 453
• Joined: 02-May 09

Posted 03 May 2009 - 09:55 AM

Thanks!

#6 ezgass

Reputation: 0
• Posts: 2
• Joined: 14-September 09

Posted 15 September 2009 - 12:21 PM

Elcric, on 2 May, 2009 - 10:27 AM, said:

Excellent! Can a C++ function return more than one piece of information?

Just use pointers and you can get as many values as you need! Pointers have power!

#7 pluto1

Reputation: 0
• Posts: 5
• Joined: 18-December 09

Posted 19 December 2009 - 04:49 PM

brilliant! Thank you!

#8 PlasticineGuy

• mov dword[esp+eax],0

Reputation: 281
• Posts: 1,436
• Joined: 03-January 10

Posted 10 March 2010 - 12:57 AM

I would add a section on default arguments:
```int divide(int x, int y = 1) {
if(y == 0) throw "Division by 0.";
return x / y;
}
int main() {
return divide(5);
}
```
Also note with prototypes:
```int divide(int x, int y = 1);
int main() {
return divide(5);
}
int divide(int x, int y) { //note no = 1 here
if(y == 0) throw "Division by 0.";
return x / y;
}
```

This post has been edited by PlasticineGuy: 10 March 2010 - 12:58 AM

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }