C++
Here is an article from a frien of mine wrote on functions in
C++
It gives you the basic jist of it, thought i'd write it since i myself was
confused. Here it is:
xde's small Guide to Understanding Functions
--> x11011110x <--
Index:>
A)Introduction
B) What you should know to start
X) Why use functions?
D) Understanding Functions
E) Conclusion
--------------------------------------
8***************************8
8******(Introdcution)*******8
8***************************8
Alright, well some people may be wondering why exactly i wrote this. The
answer
to that would be this: When i was first learning C++ (which i still am) i
had
gotten confused with functions, specifically because part of it wasn't
explained
well. So after reading it over several times i finally figured it out. I
also think
that you learn and understand things more when you try to explain it to
other people
so here i am, writing this article. Chances are it will be rather short, but
hay,
you can't write ALL that much on functions can you? Besides, most people
don't
like reading lots =P
--------------------------------------
8********************************************8
8******(What you should know to start)*******8
8********************************************8
Well, since i am not trying to teach you everything else such as declaring
variables,
using if- else, loops, and all that stuff. I am hoping you already know it,
because
it will make the understanding of it much easier. I will still make small
notes
so you can understand it though. Anyhow, on to learning!!
--------------------------------------
8********************************************8
8******(Why use Functions?)******************8
8********************************************8
Some of you may be wondering, i like what i know so far, why should i use
functions!?
Well, i will tell you! Well firstly, when you are working with a team of
programmers
it is much easier to have 1 programmer do a function, instead of you just
working
on them all. That way when you are done you can all just put your functions
together
and BAM! Your program is done. Secondly it makes it much easier for
debugging purposes
since in C++ you can debug single functions at a time. Also, if you have a
specific
action that you are going to do multiple times, like round something,
instead of
constantly type it- you can make a function for it and simply call that
function!
Functions are great! Yay!
--------------------------------------
8********************************************8
8******(Understanding Functions)*************8
8********************************************8
Ok, to start- we need to know three things about functions. There is what i
like to
call the Alpha, main, and beta. Ok, so you know the names now, whats it
mean?
Alpha is the first, for every function- you have to have it pre-stated (In
other words
you have to have it typed once before any function). Example:
#include <iostream.h>
void square(int x);
main()
{
}
Ok, so you have the line: void square(int x);
void -> is the data type
square -> is the function name
(inq x) -> is the variable & its data type
; -> and we have the semicolon.
Ok, so the above was our alpha- it was before the main program. Ok, so lets
continue
with the rest of the program:
#include <iostream.h> file://the library
void square(int x); file://the alpha (void means the function won't return
anything
file://the line above is needed for declaration
main()
{
int num; file://set number as an variable with data type integer
cout<<"Enter a number: "; file://Print "Enter a number:
"
cin>>num; file://take input from the user and stor it in num
square(num); file://something that may confuse you.. ill explain
return (0);
}
Now for the above, we have square(num); which is in our main part of the
function.
So the have the Alpha -> void square(int x); and our main -> the above.
Ok, so we get the code, but whats the square(num); business!!?
This is what confused me at first. I will explain it in a minute.
Ok, so here is the full code.
#include <iostream.h> file://the library
void square(int x); file://the alpha (void means the function won't return
anything
file://the line above is needed for declaration
main()
{
int num; file://set number as an variable with data type integer
cout<<"Enter a number: "; file://Print "Enter a number:
"
cin>>num; file://take input from the user and stor it in num
square(num); file://something that may confuse you.. ill explain
return (0);
}
void square(int x)
{
x = x * x; file://num times itself (how you square something)
cout<<"Your number squared is: ";
cout<<x <<endl;
}
Ok, the: void square(int x)
is our last function-> our beta. Think of it as this: Alpha - start, main-
middle
beta - end [yes i am aware that beta is a name for something else but this
is how
i learned].
Anyhow, so our beta is:
void square(int x)
{
x = x * x;
cout<<"Your number squared is: ";
cout<<x <<endl;
}
What the program does is asks us to enter a number, it then takes that
number and
squares it. Ok, so as i was talking about the thing that confused me before,
it was
the line: square(num);
So what happens is the num is declared as a variable with the data type of
an integer
::
int num;
::
then with the line: square(num)
it is basically taking the input from the user that was stored in variable
num,
and putting it in place of the x in our square function. Still confused? Let
me
attempt to draw a picture for you with text =P
You have num declared as a varaible with the integer data type
::
int num;
::
and the user types a number for the input- well say the user typed 5.
it goes like this:
------ -----
|num | -user input--> | 5 | num now = 5
------ -----
[now the square(num) part]
The function square
we have square(int x) --> notice the resemblence. square(int x) , and
square(num)
What happens here is the: x (the variable with integer data type) is
replaced with
the num.
so square(int x) -> is now square(num)
But the variable name is not changed.
So when the program does: x = x * x
it is really doing num = num * num
num = 5 so it comes out 25
Get it? If not i will try one last quick time. If you do, you can stop
reading this
part and go on to the next section.
When the user enters something for num (5 for example) it uses the line
square(num)
and replaces the (int x) part of square(int x) with the value of num.
So when the user puts in 5, it takes 5, puts it in num, it takes num, puts
that value
into x, then it does x = x * x
Which is technically num = num * num
----------------------------------
8***************************8
8*******(Conclusion)**********8
8***************************8
In conclusion, i hope you learned how to use functions. Perhaps later on i
will become
less lazy and add more to this... and maybe write something more. But until
then, this
is what you'll have to put up with.
And there was much rejoicing...Yay!