C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 307,003 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,979 people online right now. Registration is fast and FREE... Join Now!




Tutorial on Functions

 
Reply to this topicStart new topic

> Tutorial on Functions, Introduction to writing functions

Rating  3
Pontus
Group Icon



post 22 Apr, 2007 - 10:02 AM
Post #1


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:
CODE
"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.

Right, lets start with the a simple function:
CODE
#include <iostream>

using namespace std;

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

int main(){
testfunction(); // call your function
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.
CODE
#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(){
calculate(10,21);// call your function
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
CODE
#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:
CODE
cout<<calculate(10,20)

Now, the second way is using referances:
CODE
#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.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

eredeath
*



post 30 Sep, 2007 - 03:58 PM
Post #2
Good tutorial :-)
Go to the top of the page
+Quote Post

Elcric
Group Icon



post 2 May, 2009 - 10:27 AM
Post #3
Excellent! Can a C++ function return more than one piece of information? biggrin.gif
Go to the top of the page
+Quote Post

Pontus
Group Icon



post 3 May, 2009 - 08:47 AM
Post #4
Nope, It can return only one. If you want it to modify more than one piece of information, you can use parameters.
Go to the top of the page
+Quote Post

Elcric
Group Icon



post 3 May, 2009 - 08:55 AM
Post #5
Thanks! biggrin.gif
Go to the top of the page
+Quote Post

ezgass
*



post 15 Sep, 2009 - 11:21 AM
Post #6
QUOTE(Elcric @ 2 May, 2009 - 10:27 AM) *

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


Just use pointers and you can get as many values as you need! Pointers have power! smile.gif
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 06:34AM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month