Presentation is loading. Please wait.

Presentation is loading. Please wait.

Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.

Similar presentations


Presentation on theme: "Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution."— Presentation transcript:

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

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

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

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

5 Functions A function is like a robot. It can receives a list of arguments. It can returns a value.

6 Examples of functions int 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 ()

7 Examples of functions int 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

8 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

9 A function calls another function int 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

10 A function calls another function int 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

11 A function calls another function int 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 7 g

12 A function calls another function int 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

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

14 Function definition and function prototype // A programmer-defined square function #include 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; }

15 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

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

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

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

19 #include 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

20 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

21 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

22 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

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

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

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

26 Cube a variable using call by value #include 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; }

27 Cube a variable using call by reference #include 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; }

28 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 Contains function prototypes for the standard input/output library functions, and information used by them.

29 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

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

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

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


Download ppt "Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution."

Similar presentations


Ads by Google