C++ Using functions, function parameters
Most languages allow you to create functions of some sort. Functions are used to break up large programs into named sections.
You have already been using a function which is the main function. Functions are often used when the same piece of code has to run multiple times. In this case you can put this piece of code in a function and give that function a name.
When the piece of code is required you just have to call the function by its name. (So you only have to type the piece of code once).
Let’s take a look at a simple example:
#include<iostream>
using namespace std;
void MyPrint()
{
cout << "Printing from a function.\n";
}
int main()
{
MyPrint();
return 0;
}
In the example above we declared a function with the name MyPrint. The only thing that this function does is to print the sentence “Printing from a function”. If we want to use the function we just have to call MyPrint() and the cout statement (in the function) will be executed. (Don’t forget to put the round brackets behind the function name when you call it or declare it).
Parameters and return
Functions can accept parameters and can return a result. Where the functions are declared in your program does not matter, as long as a functions name is known to the compiler before it is called. In other words: when there are two functions, i.e. functions A and B, and B must call A, than A has to be declared in front of B.
Let’s take a look at an example where a result is returned:
#include<iostream>
using namespace std;
int Add(int output1, int output2)
{
return output1 + output2;
}
int main()
{
int answer, input1, input2;
cout << "Give a integer number:";
cin >> input1;
cout << "Give another integer number:";
cin >> input2;
answer = Add(input1,input2);
cout << input1 << " + " << input2 << " = " << answer;
return 0;
}
The main() function starts with the declaration of three integers. Then the user can input two whole (integer) numbers. These numbers are used as input of function Add(). Input1 is stored in output1 and input2 is stored in output2. The function Add() will return the result of output1 + output2. The return value is stored in the integer answer. The number stored in answer is then printed onto the screen.
Void
If you don’t want to return a result from a function, you can use void return type instead of the int. So let’s take a look at an example of a function that will not return an integer:
void our_site()
{
cout << "www";
cout << ".NextDawn";
cout << ".nl";
}
Note: As you can see there is not an int before our_site() and there is not a return 0; statement in the function.
Global and local variables
A local variable is a variable that is declared inside a function. A global variable is a variable that is declared outside ALL functions. A local variable can only be used in the function where it is declared. A global variable can be used in all functions. See the following example:
#include<iostream>
using namespace std;
int A, B;
int Add()
{
return A + B;
}
int main()
{
int answer;
A = 2;
B = 2;
answer = Add();
cout << A << " + " << B << " = " << answer;
return 0;
}
As you can see two global variables are declared, A and B. These variables can be used in main() and Add().
The local variable answer can only be used in main().
That’s all for this tutorial. In the next tutorial we will talk some more about functions.
thank u guyzzzzzzzzzzzz
thnx so much ^^
how to write a program to find ∑ n^3/n !
plz help
good1!!!! 🙂