Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


C++ Tutorial - Functions filter_list
Author
Message
C++ Tutorial - Functions #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:
(07-27-2013, 02:30 AM)0xDEADPIXEL Wrote:
Another useful thing to point out when talking about functions in C++ is that not all functions have to return any value.

Normally when declaring a function without a type we use "void" as the type specifier for the function. The "void" type specifier is a special type specifier that indicates the absence of return type.

Code example:

Code:
#include <iostream>

void SayHello()
{
    std::cout << "Hello CodeCommunity, I'm a void function :)";
}
int main()
{
    SayHello();
    return 0;
}

The "void" keyword can also be used in the function's parameter list to specify that we don't want the function to take any actual parameters when it's called. The function SayHello() could have also been declared as:

Code:
void SayHello(void)
{
    std::cout << "Hello CodeCommunity, I'm a void function :)";
}

Although it is optional to use "void" in the parameter list of the function, leaving the parameter list blank will also result in a function that does not take any parameters.


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]

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

Reply

RE: C++ Tutorial - Functions #2
"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."

I'm so pro that i understand it even though i don't code in C++ lol Tongue
Good work father Smile
[Image: tumblr_m4vms28lYu1qj3ir1.gif]

Reply

RE: C++ Tutorial - Functions #3
(07-26-2013, 06:53 PM)Eternity Wrote: "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."

I'm so pro that i understand it even though i don't code in C++ lol Tongue
Good work father Smile

That's good, my Son. I'm glad all that hard work has paid off. Now, back to the basement.
[Image: 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

Reply

RE: C++ Tutorial - Functions #4
(07-26-2013, 07:00 PM)Xiledcore Wrote:
(07-26-2013, 06:53 PM)Eternity Wrote: "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."

I'm so pro that i understand it even though i don't code in C++ lol Tongue
Good work father Smile

That's good, my Son. I'm glad all that hard work has paid off. Now, back to the basement.

But I'm not sleepy Sad
[Image: tumblr_m4vms28lYu1qj3ir1.gif]

Reply

RE: C++ Tutorial - Functions #5
Another useful thing to point out when talking about functions in C++ is that not all functions have to return any value.

Normally when declaring a function without a type we use "void" as the type specifier for the function. The "void" type specifier is a special type specifier that indicates the absence of return type.

Code example:

Code:
#include <iostream>

void SayHello()
{
    std::cout << "Hello CodeCommunity, I'm a void function :)";
}
int main()
{
    SayHello();
    return 0;
}

The "void" keyword can also be used in the function's parameter list to specify that we don't want the function to take any actual parameters when it's called. The function SayHello() could have also been declared as:

Code:
void SayHello(void)
{
    std::cout << "Hello CodeCommunity, I'm a void function :)";
}

Although it is optional to use "void" in the parameter list of the function, leaving the parameter list blank will also result in a function that does not take any parameters.
(This post was last modified: 07-27-2013, 02:31 AM by Dieo.)
[Image: dCNByK5.png]

Reply

RE: C++ Tutorial - Functions #6
(07-27-2013, 02:30 AM)0xDEADPIXEL Wrote:
Another useful thing to point out when talking about functions in C++ is that not all functions have to return any value.

Normally when declaring a function without a type we use "void" as the type specifier for the function. The "void" type specifier is a special type specifier that indicates the absence of return type.

Code example:

Code:
#include <iostream>

void SayHello()
{
    std::cout << "Hello CodeCommunity, I'm a void function :)";
}
int main()
{
    SayHello();
    return 0;
}

The "void" keyword can also be used in the function's parameter list to specify that we don't want the function to take any actual parameters when it's called. The function SayHello() could have also been declared as:

Code:
void SayHello(void)
{
    std::cout << "Hello CodeCommunity, I'm a void function :)";
}

Although it is optional to use "void" in the parameter list of the function, leaving the parameter list blank will also result in a function that does not take any parameters.

Another great post, I'll make sure I add it in the thread (completely forgot to explain the void type).

Thank you!
[Image: 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

Reply

RE: C++ Tutorial - Functions #7
Void is the thing I love about C++, reminds me of the fact that we're just living in a void and we are just mere projects of aliens O_o... Good tutorial!

Reply

RE: C++ Tutorial - Functions #8
(07-27-2013, 10:56 AM)Platinum Wrote: Void is the thing I love about C++, reminds me of the fact that we're just living in a void and we are just mere projects of aliens O_o... Good tutorial!

Nah, we're actually living in a virtual reality created by beings far more advanced than us.

Oh, and thanks!
[Image: 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

Reply

RE: C++ Tutorial - Functions #9
You can declare the integer within the for loop statement as well, to lessen the scope of that variable, even though it's not really significant here because the function ends after the system call.

Why are you using system("PAUSE") though? There's no reason to be using a system() call here for that, and it's horrible practice to do so.

To add to what 0xDEADPIXEL had mentioned, the int main() function also doesn't require you to use the return keyword. Same thing with C99. ANSI C requires it by specification.
-- cxS

[ Haskell/.NET/C/C++ - Software Engineer ]

Reply







Users browsing this thread: 1 Guest(s)