![Page 1: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/1.jpg)
Building Programs from Existing Information
• Solutions for programs often can be developed from previously solved problems.
• Data requirements and solution algorithm provide a programming starting point.
• Data requirements convert into macro definitions and variable declarations.
• Initial algorithm and refinements become comments for lines in main program.
• Place the C++ code for each unrefined step directly under that commented step.
• When program is completed, test with appropriate data and check correctness.
![Page 2: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/2.jpg)
Library Functions
• Code reuse recycles code that has been already written, debugged and tested.
• C++ has many predefined functions that take argument(s) and return some value.
• Functions are stored in libraries and activated by writing a function call.
Example: y = sqrt(x); uses x as input argument, assigns y to the
value of the square root of x.
![Page 3: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/3.jpg)
User-Defined Functions
• Functions can be written by users to perform frequently needed calculations.
• These functions can either be inserted directly into programs, or
• stored in header files or libraries and then compiled with the calling programs.
![Page 4: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/4.jpg)
Function• The function data area is an area of memory that is
allocated each time the function is called, and is lost when the function terminates.
• This data area holds the function's formal parameters, and local variables created within.
• Variables created inside a function are local to that function, and can be used only inside that function. They are not directly accessible from main.
• Functions can be tested externally by building a driver program that defines the function arguments, calls the function, and displays the values returned.
![Page 5: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/5.jpg)
Functions
• A function is like a robot.• It can receives a list of arguments.• It can returns a value.
![Page 6: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/6.jpg)
Examples of functionsint main (){ int a; a = f(3,4); return 0;}
int f(int x, int y){ return (x + y);}
return (x+y)
f(int x, int y)main ()
![Page 7: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/7.jpg)
Examples of functionsint main (){ int a; a = f(3,4); return 0;}
int f(int x, int y){ return (x + y);}
return (x+y)
f(int x, int y)main ()
3, 4
![Page 8: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/8.jpg)
Examples of functions
F(int x, int y)Main ()
int main (){ int a; a = f(3,4); return 0;}
int f(int x, int y){ return (x + y);}
return (x+y)
3, 4
7
![Page 9: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/9.jpg)
A function calls another functionint main (){ int a; a = f(3,4); return 0;}
int f(int x, int y){ return (x + g(y));}
int g(int a){ return (a + 3);}
3, 4
main
g
f
![Page 10: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/10.jpg)
A function calls another functionint main (){ int a; a = f(3,4); return 0;}
int f(int x, int y){ return (x + g(y));}
int g(int a){ return (a + 3);}
3, 4
main
g
f
4
![Page 11: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/11.jpg)
A function calls another functionint main (){ int a; a = f(3,4); return 0;}
int f(int x, int y){ return (x + g(y));}
int g(int a){ return (a + 3);}
3, 4
main f
4
7g
![Page 12: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/12.jpg)
A function calls another functionint main (){ int a; a = f(3,4); return 0;}
int f(int x, int y){ return (x + g(y));}
int g(int a){ return (a + 3);}
3, 4
main f
10
![Page 13: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/13.jpg)
Function prototype• A function must be declared before it can be
referenced.• One way to declare a function is to insert a
function prototype before the main function.• Function prototype tells the C++ compiler the
function name, data types of the function and the arguments, the correct number of arguments, and the correct order of arguments.
• The data type of a function is determined by the type of value returned by the function.
• It does not specify the function operation. You need to provide a definition for each function.
![Page 14: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/14.jpg)
Function definition and function prototype // A programmer-defined square function #include <iostream>
int square (int); // Function prototype int main() { int x; for (x = 1; x <= 10; x++) cout << square(x) << ‘ ‘; cout << endl; return 0; } int square (int y) // Function definition { return y * y; }
![Page 15: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/15.jpg)
Example• square function gets the value of x.• square function calculates x * x.• The result is passed back to the cout function.• cout function displays the result.• This process is repeated ten times using the for
repetition structure.
Result 1 4 9 16 25 36 49 64 81 100
![Page 16: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/16.jpg)
Function without argument
Example: void display_information(void);
• The function display_information is a void function, returns nothing.
• Second void indicates that display_information expects no arguments.
![Page 17: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/17.jpg)
Function with argument
Example: int square(int); // prototype
• The int in parenthesis informs the compiler that function square expects to receive an integer value from the caller.
• The int to the left of square informs the compiler that it returns an integer result to the caller.
![Page 18: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/18.jpg)
Void function with input argument Example: void display_even_num(int start, int end); // prototype
• The int in parenthesis informs the compiler that function display_even_num expects to receive two integer values from the caller.
• The void to the left of the function display_even_num informs the compiler that it returns nothing to the caller.
![Page 19: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/19.jpg)
#include <iostream>
void display_even_number( int start, int end );
int main()
{
int first, last;
first = 2, last = 20;
display_even_number(first, last);
return 0;
}
void display_even_number( int start, int end)
{
int count;
for( count = start; count <= end; count = count + 2)
cout << count << ' ';
cout << endl; }
Void function with input argument
![Page 20: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/20.jpg)
Argument list correspondence• You should be careful to include the correct number of
arguments in the function call.– Example: int add (int num1, int num2); // prototype y = add (num1, num2); // function call y = add (num1) // incorrect
• The order of the actual arguments used in the function call must correspond to the order of the formal parameters listed in the function prototype.
– Example: int subtract (int num1, int num2); // prototype y = subtract (num1, num2); // function call y = subtract (num2, num1) // incorrect
![Page 21: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/21.jpg)
Argument list correspondence• Usually, each actual argument must be of the same data
type as the corresponding formal parameter. • When an int is assigned to a type double variable, it will
not cause any problem since there is no loss of information.
– Example: – double add (double num1, double num2); //prototype – y = add (num1, num2); // function call with int values: num1 = 2 , num2 = 3
result: Y = 5.0 No loss of information
![Page 22: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/22.jpg)
Argument list correspondence
• When we pass an actual argument of type double to a formal parameter of int, loss of the fractional part of the actual argument can lead to an unexpected result.
– int add (int num1, int num2); // prototype y = add (num1, num2); // function call, num1 = 2.2, num2 = 3.6 result: Y = 5 Loss of information
![Page 23: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/23.jpg)
Advantages of using function
• Code reuse. - using existing function as building blocks to create new programs.
• Divide-and-conquer approach makes program development more manageable.
• Avoid repeating code in a program. We can execute a block of code simply by calling the function many times in a program.
![Page 24: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/24.jpg)
Call by value
• Two ways to invoke functions:– Call by value– Call by reference• Call by value: When arguments are passed call by value, – a copy of the argument’s value is made and passed
to the called function. – Changes to the copy do not affect an original
variable’s value in the caller.
![Page 25: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/25.jpg)
Call by reference
When an argument is passed by reference, • the caller actually allows the called
function to modify the original variable’s value.
• It is possible to simulate call by reference by using address operators and indirection operators.
![Page 26: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/26.jpg)
Cube a variable using call by value #include <iostream> int cubeByValue(int); int main() { int number = 5; cout << “The original value of number is “<< number <<endl; number = cubeByValue(number); cout << “The new value of number is “<< number << endl; return 0; } int cubeByValue(int n) { return n * n * n; }
![Page 27: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/27.jpg)
Cube a variable using call by reference #include <iostream> void cubeByReference(int *); int main() { int number = 5; cout << “The original value of number is “<< number <<endl; cubeByReference(&number); cout << “The new value of number is “<< number << endl; return 0; } void cubeBy Reference(int *nPtr) { *nPtr = *nPtr ** nPtr * *nPtr; }
![Page 28: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/28.jpg)
Header Files Each standard library has a corresponding header file
containing • the function prototypes for all the functions in that
library and • definitions of various data types and constants needed
by those functions.Example:Standard library header file
<stdio.h> Contains function prototypes for the standard input/output library functions, and information used by them.
![Page 29: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/29.jpg)
Storage duration• Period during which an identifier exists in
memory.• Some identifiers exists briefly. • Some are repeatedly created and destroyed.• Others exist for the entire execution of a
program.• Storage duration can be of two types:– automatic (auto and register)– static
![Page 30: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/30.jpg)
Storage Classes• C provides four storage classes indicated by
the storage class specifiers:– auto– register– extern– static• An identifier’s storage class helps
determine its storage duration, scope, and linkage.
![Page 31: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/31.jpg)
Scope• The scope of an identifier is the portion of
the program in which the identifier can be referenced.
• Some identifiers can be referenced throughout the program.
• Others can be referenced from only portions of a program.
• Example: When we declare a local variable in a block,
it can be referenced only in that block or in blocks nested within that block.
![Page 32: Building Programs from Existing Information Solutions for programs often can be developed from previously…](https://reader035.vdocuments.net/reader035/viewer/2022070616/5a4d1bf07f8b9ab0599e5a14/html5/thumbnails/32.jpg)
Linkage
• An identifier’s linkage determines for a multiple-source-file program where an identifier is known only
– in the current source file or – in any source file with proper declaration.