Presentation is loading. Please wait.

Presentation is loading. Please wait.

Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.

Similar presentations


Presentation on theme: "Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function."— Presentation transcript:

1 Function 2

2 User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function definitions

3 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

4 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.

5 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 using namespace std; void PrintSumAve ( double, double ); int main ( ) { double x, y; cout << "Enter two numbers: "; cin >> x >> y; PrintSumAve ( x, y ); return 0; }

6 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; }

7 Pass by Value: Example Data areas after call to PrintSumAve() :

8 Programming Scope of Identifiers

9 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.

10 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.

11 int y = 38; void fun(int, int); int main( ){ int z=47; while(z<400){ int a = 90; z += a++; z++; } y = 2 * z; fun(1, 2); return 0; } void fun(int s, int t){ int r = 12; s = r + t; int i = 27; s += i; } Scope: Example 1 scope of i scope of r scope of s & t scope of a scope of z scope of y scope of fun

12 Example 2:Global Constants #include 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);

13 Scope: Example 3 Number in Increment () is the global variable. #include 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; }

14 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; }

15 Scope: Example Output: ABCD in One = 10 11 12 13 ABCD in Two = 10 21 23 23 ABCD in Main = 1 2 23 4 ABCD in Two = 1 21 23 23 ABCD in Main = 1 2 23 4

16


Download ppt "Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function."

Similar presentations


Ads by Google