Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Function 2
User-Defined Functions
C++ programs usually have the following
form:
//
//
//
//
include statements
function prototypes
main() function
function definitions
Arguments/Parameters
one-to-one correspondence between the
arguments in a function call and the parameters
in the function definition.
int argument1;
double argument2;
// function call (in another function, such as main)
result = thefunctionname(argument1, argument2);
// function definition
int thefunctionname(int parameter1, double parameter2){
// Now the function can use the two parameters
// parameter1 = argument 1, parameter2 = argument2
Function Definition
The function definition can be placed
anywhere in the program after the function
prototypes.
If a function definition is placed in front of
main(), there is no need to include its
function prototype.
Pass by Value: Example
// Print the sum and average of two numbers
// Input: two numbers x & y
// Output: sum - the sum of x & y
//
average - the average of x & y
#include <iostream>
using namespace std;
void PrintSumAve ( double, double );
int main ( ) {
double x, y;
cout << "Enter two numbers: ";
cin >> x >> y;
PrintSumAve ( x , y );
return 0;
}
Pass by Value: Example
void PrintSumAve (double no1, double no2)
{
double sum, average;
sum = no1 + no2;
average = sum / 2;
cout << "The sum is "
<< sum << endl;
cout << "The average is "
<< average << endl;
}
Pass by Value: Example
Data areas after call to PrintSumAve() :
Programming
Scope of Identifiers
Scope
A sequence of statements within { … } is
considered a block of code.
The part of the program where you can use a
certain identifier is called the scope of that
identifier.
The scope of an identifier starts immediately
after its declaration and ends when the
“innermost” block of code within which it is
declared ends.
It is possible to declare the same identifier in
another block within the program.
Scope
The
scope of an identifier does not apply if the same
identifier is declared in an inner block.
A global declaration of an identifier is made outside the
bodies of all functions, including the main function. It is
normally grouped with the other global declarations and
placed at the beginning of the program file.
A local declaration of an identifier is made inside a block
of code which could be the body of a function.
Globally declared identifiers can be accessed anywhere
in the program.
Locally declared identifiers cannot be accessed outside
of the block they were declared in.
Scope: Example 1
int y = 38;
void fun(int, int);
int main( ){
scope of fun
int z=47;
while(z<400){
scope of z
int a = 90;
z += a++;
scope of a
z++;
}
y = 2 * z;
fun(1, 2); return 0;
}
void fun(int s, int t){
int r = 12;
scope of s & t
s = r + t;
scope of r
int i = 27;
s += i;
scope of i
}
scope of y
Example 2:Global Constants
#include <math.h>
#include <iostream>
using namespace std;
const double PI = 3.14159;
double area(double radius);
//Returns the area of a circle with the specified radius.
double volume(double radius);
//returns the volume of a sphere with the specified radius.
int main(){
double radius_of_both, area_of_circle,
volume_of_sphere.
cout << " Enter a radius to use for both a circle "
<< " and a sphere (in inches): “
cin >> radius_of_both;
area_of_circle = area(radius_of_both);
volume_of_sphere = volume(radius_of_both);
Scope: Example 3
Number in Increment() is the global variable.
#include <iostream>
using namespace std;
int Number; //global variable
void Increment(int IncNum) {
IncNum = IncNum + 3;
cout << IncNum << endl;
Number = Number + 1; }
int main() {
Number = 1;
Increment(Number);
cout << Number << endl;
return 0;
}
Scope: Example
int A,B,C,D;
void Two(int A, int B, int& D) {
B = 21; D = 23;
cout << A << " " << B << " " << C<< " " << D << endl;
}
void One(int A, int B, int& C) {
int D;
// Local variable
A = 10; B = 11; C = 12; D = 13;
cout << A << " " << B<< " " << C<< " " << D << endl;
Two(A,B,C);
}
int main() {
A = 1; B = 2; C = 3; D = 4;
One(A,B,C);
cout << A << " " << B << " " << C<< " " << D << endl;
Two(A,B,C);
cout << A << " " <<B << " " << C << " " << D << endl;
return 0;
}
Scope: Example
Output:
ABCD
ABCD
ABCD
ABCD
ABCD
in
in
in
in
in
One = 10 11 12 13
Two = 10 21 23 23
Main = 1 2 23 4
Two = 1 21 23 23
Main = 1 2 23 4