Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 232 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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:


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


Hard to understand? I'll show you a basic example of a function:


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


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.


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


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!


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


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:


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


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:


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


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![/hide]
Reply

#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
Reply

#3
Quote:(07-26-2013, 06:53 PM)Eternity Wrote:

[To see links please register here]

"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.
Reply

#4
Quote:(07-26-2013, 07:00 PM)Xiledcore Wrote:

[To see links please register here]

Quote: (07-26-2013, 06:53 PM)Eternity Wrote:

[To see links please register here]

"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
Reply

#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:


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


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:


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


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.
Reply

#6
Quote:(07-27-2013, 02:30 AM)0xDEADPIXEL Wrote:

[To see links please register here]

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:


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


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:


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


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!
Reply

#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

#8
Quote:(07-27-2013, 10:56 AM)Platinum Wrote:

[To see links please register here]

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!
Reply

#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.
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through