C++ Tutorial - Functions 07-26-2013, 06:26 PM
#1
Introduction to functions
Hello there, and welcome to my tutorial on functions. I'll be covering how to declare a function, how to use one and examples of functions.
First of all, what is a function?
Good question, Sir! A function is basically a block of code that performs a task. Functions are great because it helps you divide your code into smaller chunks that are easier to develop. We've already declared one function before. Yes, the main function.
How do we declare a function, sensei?
Oh, that is really easy. The form goes as follows:
Code:
return_type name_Of_Func( parameters )
{
The body of the function
}
Hard to understand? I'll show you a basic example of a function:
Code:
int countChr(std::string str)
{
int cLength;
cLength = str.length();
return cLength;
}
What this does is that it counts the characters in the string and stores the number of characters in an integer variable. And then it returns the value. This concept might be a little hard to grasp at first, but You'll get the hang of it eventually. Just practise.
Now we need to call this function.
Sensei, tell us how to call the function please!
Yes, here's a complete example of the function I made.
Code:
#include<iostream>
#include<string>
int countChr(std::string str)
{
int cLength;
cLength = str.length();
return cLength;
}
int main()
{
std::string name("Xiledcore");
std::cout << "There are: " << countChr(name) << " letters in: " << name << std::endl;
return 0;
}
This might be a little advanced for some, so I'll show you another example of a function that adds two numbers. It's really easy!
Code:
#include<iostream>
#include<string>
int addition(int a, int b) //Declares a function with return type: integer
{
int total; //Declares int named total which will store the value of the two numbers added together
total = a + b; //Sets the value of total to whatever a and b added together is.
return total; //Returns the value of the two numbers added together
}
int main()
{
int n1 = 5; //Declares number one
int n2 = 15; //Declares number 2
std::cout << "The total value of: " << n1 << " + " << n2 << " is: " << addition(n1, n2) << "!" << std::endl; //Adds the two numbers together and prints the total value
return 0;
}
So that should be the basics of functions! I'm sorry if this was too hard to understand. If you've got any questions, please ask me. I promise, Sensei never bites!
TIP: You won't learn this completely if you don't experiment with it. Try and make some functions yourself and see how it goes! If you're having problems, post a help thread in this forum section and I (and other forum members if they want) will try and help you as much as we can.
Useful additions by other members
0xDEADPIXEL:
Spoiler:
If there's something I've done wrong that I haven't realized, please tell correct me. And by all means, if you've got something you'd like to add to the tutorial, reply on here and I'll add it to the thread.
Thank you!
![[Image: WV5eQ42.jpg]](http://i.imgur.com/WV5eQ42.jpg)
If you've got any questions regarding c++ or java, feel free to hit me up with a private message at any time.
http://adf.ly/UyTEk