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

Slides:



Advertisements
Similar presentations
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Advertisements

Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
Chapter 6: User-Defined Functions I
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Using Abstraction to Manage Complexity Abstraction: procedural abstraction & data abstraction. Procedural abstraction=> function development should separate.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Chapter 6: User-Defined Functions I
C++ Functions CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Chapter 6: Functions.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
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.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Programming Fundamentals Enumerations and Functions.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Functions Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 6: User-Defined Functions I
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Chapter 7: User-Defined Functions II
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
Scope, Parameter Passing, Storage Specifiers
6 Chapter Functions.
Chapter 6: User-Defined Functions I
Presentation transcript:

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.

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.

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.

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.

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

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 ()

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

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

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

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

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

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

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.

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

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

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.

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.

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.

#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

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

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

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

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.

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.

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.

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

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

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.

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

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.

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.

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.