>>1
Functions :
I guess you know what functions are, I'll show you how to use them with some examples
#include <iostream>
using std::cout;
using std::endl;
int no_u()//int is the return type
{
return 3;
#if 0 //the following code will not be executed but it also works
int uhNo = 3;
return uhNo;
#endif
}
void foo()//void = no return value
{
cout<<"NO U"<<endl;
}
void lulz(int helloLol,long shitty_parameter)/*helloLol and shitty_parameter are parameters you decide of their value when you call the function(see main) you can have as many parameters as you want*/
{
cout<<helloLol<<" "<<shitty_parameter<<endl;
}
int fourchan(int HAHAHAOHWOW)
{
return HAHAHAOHWOW+1;
}
int main(int argc, char *argv[])
{
cout << no_u() << endl;
foo();
lulz(2,4);//helloLol = 2, shitty_parameter = 4
fourchan(7);
return 0;
}
Bools :
A bool can have 2 values : true(any value but 0) and false(0)
int fourchan = 2<3//fourchan = true
int fourchan = false//fourchan = false
int fourchan = 3==3//fourchan = true
if(fourchan)//if fourchan is true do_something will be executed
{
do_something();
}
if(!fourchan)//if fourchan is false do_something will be executed
{
do_something();
}
if(fourchan==6)//if fourchan = 6(fourchan==6 returns a boolean)
While loops :
int main(int argc,char *argv[])
{
fourchan = 3;
while(fourchan<10)//AS LONG AS fourchan<10
{
cout<<fourchan<<endl;
fourchan ++;
}
}
Arrays are like a "list" of datas
int myArray[3];//a 3 ints array is declared
there are 3 values(like if you had 3 different variables) in this array
myArray[0]
myArray[1]
myArray[2]
myArray[0] = 3;
myArray[1] = 4;
etc.
If you understand that code you will probably be ready for your exam :
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
#define ARRAY_SIZE 5
void drawValuesInArray(int *array)
{
int pos = 0;//variables in main and in functions are not the same
while(pos<ARRAY_SIZE)
{
cout<<array[pos]<<endl;
pos++;
}
}
int main(int argc,char *argv[])
{
int pos = 0;
int lolarray[ARRAY_SIZE];
while(pos<ARRAY_SIZE)
{
cin>>lolarray[pos];
pos++;
}
drawValuesInArray(lolarray);
cin.get();
return 0;
}