Nikolai C++ Master
k Posts : 24 Points : 43 Reputation : 5 Join date : 2012-04-26
| Subject: C++ Functions Thu May 03, 2012 3:50 am | |
| Okay today I will be going over functions and what uses they have. What is a Function? A function is a block of statements that is executed once called from anywhere in the application. What is the Function format? - Code:
-
Datatype NameOfTheFunction (Parameter, CouldBeManyParameters, SeperatedByAComma) { // Begin the block //group of statements } //End the function
The name of the function could be anything. But keep the names convenient and organized so people know what they are looking at when they read your source. The parameters are arguments you want to pass to the function to be able to use. I will give an example on this. The Datatype of the function, is the data we want to return to other functions to use. Again I will give an example on this.
So let's create a function that will take an integer input, and return it to the main function to be used later on. - Code:
-
using std::cin; int TakeInput() { int x; cin << x; return x; }
Done. Simple right? TakeInput is called, int x is the input and is returned to be used. So how is this useful? Let's implement it. - Code:
-
#include <iostream> using std::cin; using std::cout;
int main() { int nAmount = TakeInput(); //The return from TakeInput will be stored in int nAmount cout << nAmout; //You should see what you entered inside the TakeInput; }
int TakeInput() { cout << "Enter the amount!->"; int x; cin << x; return x; } So go ahead and try to compile that.
You'll realize it gave an error and could not compile. The reason for it is if you remember our control flow tutorial. The compiler will begin to compile from the main function. Now we called TakeInput from the main function, but at the time we called it the compiler had not seen the function. Since the function appeared under main(). There is an easy fix for that. It's called a function prototype or forward delceration. What it does is it tells the compiler ahead of time that we are going to be dealing with this function. So even if the function hasn't appeared yet when main is called, don't panic and compile anyways. A prototype is also great programming practice because if you were to pass on large sources, the person reading the source could tell from the prototype what functions your incorperates. And prototypes appear at the top of the source under the inclues. - Code:
-
#include <iostream> using std::cin; using std::cout;
int TakeInput(); //This is the prototype for TakeInput() int main() { int nAmount = TakeInput(); //Again nAmount will store what ever TakeInput return passes to the main function. cout << nAmout; }
int TakeInput() { cout << "Enter the amount!->"; int x; cin << x; return x; } Now your source should compile just fine. And your program should output the number taken in from the Input Function. So what can we do now? Well we can take in multiple inputs just by calling that input function. - Code:
-
#include <iostream> using std::cin; using std::cout;
int TakeInput(); //This is the prototype for TakeInput() int main() { int z = TakeInput(); int y = TakeInput(); int x = TakeInput(); int j = TakeInput(); cout << z << y << x << j; }
int TakeInput() { cout << "Enter integer!->"; int x; cin << x; return x; } All right now let's get into a function with arguments. All right what's so great about a function with arguments? Well if we were to call a function such as add two integers without an argument. - Code:
-
int Add() { int x; std::cin << x; int y; std::cin << y; int result = x+y; return result; }
What we had to do we retake those inputs of x and y then add and return them. That seems redundant since we all ready took the inputs in our previous example. - Code:
-
int Add(int x, int y) { int result = x+y; return result; }
Now if you're wondering what is x and y, how do we make it useful? Let's go ahead and do that right now. - Code:
-
#include <iostream> using std::cin; using std::cout;
int Add(); //Add prototype int TakeInput(); //TakeInput prototype
int main() { int FirstInput = TakeInput(); //Take the first integer, and return is passed to int FirstInput int SecondInput = TakeInput(); //Take the second integer and return is passed to int SecondInput
int myresult = Add(FirstInput, SecondInput); //myresult will store the result passed from the Add function cout << myresult; //Your application should show the result of the two integers added. system("pause") //This is a stupid idea but you can do it for now to see the result.
return 0; }
int TakeInput() { cout << "Enter integer that will be added->"; int x; cin << x; return x; }
int Add(int x, int y) { int result = x+y; return result; }
And there you go. A function tutorial. One big reason why functions are useful, is they keep your code organized. If you kept everything in the main function, and something went wrong with adding two integers, you'd have to search through main to find the portion that adds two integers, and then correct it. If you use functions you can just look at the function prototype, know the function Add exists, search for the function using the find option, and correct the function. Well enjoy the tutorail, take the time to read it again. If I missed explaining something please let me know. Also you should be able to make a calculator now based off this tutorial.
Last edited by Nikolai on Thu May 03, 2012 6:04 am; edited 1 time in total | |
|
woefie Owner
Posts : 52 Points : 97 Reputation : 0 Join date : 2012-04-24
| Subject: Re: C++ Functions Thu May 03, 2012 4:35 am | |
| Man, i don´t have to pay you for this do i? this is realy HQ thanks for ur tuts, NOW COME IN CHATTTT I NEVER CHATTED WITH YU | |
|
Nikolai C++ Master
k Posts : 24 Points : 43 Reputation : 5 Join date : 2012-04-26
| Subject: Re: C++ Functions Thu May 03, 2012 6:11 am | |
| No you don't have to pay me. | |
|
woefie Owner
Posts : 52 Points : 97 Reputation : 0 Join date : 2012-04-24
| Subject: Re: C++ Functions Fri May 04, 2012 12:00 am | |
| xD good is it important to create a new....class? if i say that right each thing u wanna make o-o? | |
|
Rzdude Beginner
Posts : 78 Points : 110 Reputation : 0 Join date : 2014-06-27
| Subject: Re: C++ Functions Fri Jun 27, 2014 5:42 am | |
| I guess I might try c++ but it looks way to complicated | |
|
Sponsored content
| Subject: Re: C++ Functions | |
| |
|