page 1 page 2
page 3 page 4 page 5 page 6 page 7 page 8
i'm gonna give you some code, you can stare at it as long as you like before you go on.
see if you can figure out the basic concepts before i tell you everything anyway.
code:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int function()
{
cout << 5;
return 0;
}
int main()
{
cout << "let's see what function() does..." << endl;
function();
cout << "that was fun.";
return 0;
}
consider what you've just seen, it's a couple new things thrown at you.
done? first, there's a new std member, endl. this is end line, you use it to
make a new line in console output. not too hard, just remember to stick the arrows
in front of it.
int function() { }
this is the first function we've made together ;-;. i called it function, so
you have no excuse to be confused. this is a simple function, it just says "5" and
exits. calling it is even easier than making it (this is especially so when your
functions stop being so short), just say function(); and it does as you please.
why the parenthases? i'm glad i asked.
let's look at a slightly more advanced function, and how it's applied.
code:
float average(float x, float y)
{
return x/y;
}
int main()
{
float x = 10.0;
float y = 3.0;
cout << average(x,y);
return 0;
}
woooooo. that's pretty deep. don't worry, you'll be fine. let's start at the beginning.
float average(float x, float y)
we're declaring a function, i'm sure you'll recognize the type, function name, and parenthases.
but what are the other two floats? i thought we had enough! these are arguments, variables (in
this case, two numbers) that are passed to the function to be dealt with.
float x = 10.0; float y = 3.0;
not only did i save space right there, i showed you another feature of c++. if you put a semicolon
somewhere, you can start the next statement without making a new line. anyway, let's keep
moving. notice i said 10.0 and 3.0 instead of 10 and 3. these are floats, they're
special enough to get decimals. they'll need them after all, dividing 10 by 3 won't give you an
integer.
cout << average(x,y);
you don't necessarily need cout here, but knowing you can put function results into the same
stream as your text is handy. you're calling the average function, and passing the x
and y floats you just made as arguments. average takes these and divides x
by y and tells you what came of it.
page 1 page 2
page 3 page 4
page 5 page 6 page 7 page 8