Last active
May 21, 2016 12:32
-
-
Save mhmd-azeez/6a652511b4093be212dae72031dd3810 to your computer and use it in GitHub Desktop.
Functions, what are they, how to use them and how to make them?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <string> | |
| using namespace std; | |
| string GetName() | |
| { | |
| cout << "What's your name? "; | |
| string name; | |
| cin >> name; | |
| return name; | |
| } | |
| void SayHello(string name) | |
| { | |
| cout << "Hello " << name << endl; | |
| } | |
| int GetAge() | |
| { | |
| int age; | |
| do | |
| { | |
| cout << "How old are you? "; | |
| cin >> age; | |
| } while (age <= 0); // make sure the users gives a positive number | |
| return age; | |
| } | |
| int Add(int num1, int num2) | |
| { | |
| return num1 + num2; | |
| } | |
| void main() | |
| { | |
| SayHello(GetName()); | |
| int age = GetAge(); | |
| cout << "So, you are " << age << " years old.\n"; | |
| cout << "In 3 years, you'll be " << Add(age, 3) << " old.\n"; | |
| system("pause"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <string> | |
| using namespace std; | |
| // Prototypes | |
| int Add(int, int); | |
| int GetAge(); | |
| string GetName(); | |
| void SayHello(string); | |
| void main() | |
| { | |
| SayHello(GetName()); | |
| int age = GetAge(); | |
| cout << "So, you are " << age << " years old.\n"; | |
| cout << "In 3 years, you'll be " << Add(age, 3) << " old.\n"; | |
| system("pause"); | |
| } | |
| /* | |
| Adds two numbers together | |
| @num1 the first number | |
| @num2 the second number | |
| */ | |
| int Add(int num1, int num2) | |
| { | |
| return num1 + num2; | |
| } | |
| // Asks the user for his/her age | |
| int GetAge() | |
| { | |
| int age; | |
| do | |
| { | |
| cout << "How old are you? "; | |
| cin >> age; | |
| } while (age <= 0); // make sure the users gives a positive number | |
| return age; | |
| } | |
| // Gets the name of the user | |
| string GetName() | |
| { | |
| cout << "What's your name? "; | |
| string name; | |
| cin >> name; | |
| return name; | |
| } | |
| // Greets the user | |
| void SayHello(string name) | |
| { | |
| cout << "Hello " << name << endl; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment