Wednesday, 6 March 2013

Week 5 day 1

This day, I learned about how a function is called in C++. when defining a function you use a call function eg;
int myFunction
{
      cout << "This is a function to display this message";
      return 0;
}
That is a basic function, when using C++ you always use the "main" function which is used to call any other functions for example look at the following script;
#include <iostream>
using namespace std;
int myFunction ()
{
cout << "called from myFunction!\n";
return 0;
}
int main ()
{
cout << "Called from main()!";
myFunction();
cout << "Back to main!";
return 0;
}
this prints:
Called from main()!
called from myFunction!
Back to main!

this is the basics of using more than one function with C++. It is also easier for the system to handle if you break up the work into many functions instead of making one do all the work.

No comments:

Post a Comment