Académique Documents
Professionnel Documents
Culture Documents
10/10/2012
What is a function?
Example
Example
#include <iostream>
using namespace std;
int Addition (int x, int y)
{
int z;
z=x+y;
return(z);
}
int main ()
{
int a=2, b=1, Sum;
Sum=Addition(2, 10);
cout<< Sum<< endl;
cout<<Addition(a,b);
return 0;
}
10/10/2012
Example
#include <iostream>
using namespace std;
Message ();
main ()
{
Message ();
return 0;
}
int Message ()
{
cout<< This function<< endl;
cout<<returns nothing;
return 0;
}
Example
The format:
1) void identifiername (parameters)
{block of statements;}
2) void identifiername ()
{block of statements;}
3) void identifiername (void)
{block of statements;}
#include <iostream>
using namespace std;
void main ()
{
cout<< This function<< endl;
cout<<returns nothing;
}
10/10/2012
Example
#include <iostream>
using namespace std;
void Message ()
{
cout<< This function<< endl;
cout<<returns nothing;
}
int main ()
{
Message ();
return 0;
}
LOCAL
Defined outside a
function.
Defined inside a
function.
Recognized by the
entire program.
Recognized by the
particular function
only.
LOCAL variable
10/10/2012
AUTOMATIC
Example: Error
STATIC variable
AUTOMATIC variable
10/10/2012
Passing An Argument
One or more arguments can be passed from
one function to another by value or by
address.
A function that passes an argument is known
as the calling function.
Another function that receives the argument
is known as the receiving function.
The local variables must be placed in
parentheses () in both the calling and
receiving functions.
Example
Note#:
Always
declare the
variable
data type
in the
receiving
function.
10/10/2012
Example
Example
10/10/2012
Passing By Values
A copy of the variables value is sent to the
receiving function parameter.
The receiving function can modify the copied
value but not the calling functions variable.
By default, all arguments or parameters are
passed by value.
By default, all arrays are passed by address.
Example
10/10/2012
Passing By Address
A variables address is copied and sent to the
receiving function parameter.
All variables are stored in RAM memory
address assigned by C++ each time a variable
is defined.
When the variable is called, C++ refers to that
address and uses whatever it needs.
If changes was made to that variable in the
receiving function, changes will also affect
the variable in the calling function.
10/10/2012
Example
#include <iostream>
#include <string>
using namespace std;
string getName();
void displayName(string & name);
string getName()
{
string nameF1;
cout << "From function<<
" \"string getName()\"\nWhat is your name?: ";
getline(cin, nameF1);
return (nameF1);
}
int main()
{
string name;
name = getName(); cout << "\n";
displayName(name); cout << "\n\n";
return 0;
}
Example
10/10/2012
Output
______________________________________
From function "string getName()"
What is your name?: Justin
Hello Justin, this line is being displayed from
function: "void displayName(string & name)".
Press any key to continue . . .
10