Skip to content

Instantly share code, notes, and snippets.

@nightpool
Last active August 29, 2015 13:57
Show Gist options
  • Save nightpool/9592363 to your computer and use it in GitHub Desktop.
Save nightpool/9592363 to your computer and use it in GitHub Desktop.
int main() {
// So this is my main function
// it gets called automatically, but otherwise its no different then any other function
int number_of_tates = 0; // I take inspiration from my enviroment ;)
number_of_tates = work_on_song(number_of_tates); // Now I'm going to call my function, which I define down below.
// It takes an integer, the old amount, and returns an integer (the new amount).
// After calling the function, I assign it to the variable "number_of_tates",
// overwriting the old value.
cout << number_of_tates << end; // prints '3'
number_of_tates = work_on_song(number_of_tates); // Now I can call the same function again
number_of_tates = work_on_song(number_of_tates); // and again
number_of_tates = work_on_song(number_of_tates); // without rewriting the same code.
cout << number_of_tates << end; // prints '12'
}
int work_on_song(int n) { // How to parse this:
// returnType function_name(argumentType argumentVariableName [And then possibly more arguments after that])
// How to say it:
// Function work_on_song takes an integer (named n) and returns an integer.
return n + 3; // This could potentially be a much more complicated piece of code (that you'd want to test by itself for example,
// before integrating into a much bigger system).
// This way, you can keep your code clean, seperate and modular.
}
// To be exceedingly pedantic, this code won't compile as is. Because of historical reasons,
// C++ functions need to be declared above where you want to use them.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment