Programming in C++ Language (0901 230) Lecture 5: Functions-Part1 Dr. Lubna Badri.

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1 Created by David Mann, North Idaho College.
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Functions  Programmer-Defined Functions  Local Variables in Functions  Overloading Function Names  void Functions,  Call-By-Reference Parameters in.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1 ; Programmer-Defined Functions Two components of a function definition.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
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.
Introduction to Computers and Programming Introduction to Methods in Java.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
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.
 2007 Pearson Education, Inc. All rights reserved C Functions.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Copyrights ©2005 Pearson Addison-Wesley. All rights reserved. 1 Chapter 4 Procedural Abstraction and Functions That Return a Value.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 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
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 4 Procedural Abstraction and Functions That Return a Value.
© 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.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 3 Procedural Abstraction and Functions That Return a Value.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 4 Procedural Abstraction and Functions That Return a Value.
Chapter 6: User-Defined Functions
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Procedural Abstraction and Functions That Return a Value.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
Chapter 4 Procedural Abstraction and Functions That Return a Value.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Top-Down Design SLO1.8 Explain the benefits of dividing large programming tasks to smaller.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 4 Procedural Abstraction and Functions That Return a Value.
Copyright © 2015 Pearson Education, Ltd.. All rights reserved. Chapter 4 Procedural Abstraction and Functions That Return a Value.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
TMC1414/TMC1413 I NTRODUCTION T O P ROGRAMMING Lecture 06 Function.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Copyright © 2015 Pearson Education, Ltd.. All rights reserved. Chapter 4 Procedural Abstraction and Functions That Return a Value.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 4 Functions.
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Programming Fundamentals Enumerations and Functions.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
Functions Procedural Abstraction Flow of Control INFSY 307 Spring 2003 Lecture 4.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 6: User-Defined Functions I
Procedural Abstraction and Functions That Return a Value
Dr. Shady Yehia Elmashad
Procedural Abstraction and Functions That Return a Value
Procedural Abstraction and Functions That Return a Value
Chapter 4 Procedural Abstraction and Functions That Return a Value 1
Programming Fundamentals Lecture #7 Functions
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
Dr. Shady Yehia Elmashad
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
Dr. Shady Yehia Elmashad
Functions Declarations CSCI 230
Chapter 6 Methods: A Deeper Look
Chapter 9: Value-Returning Functions
Chapter 4 Procedural Abstraction and Functions That Return a Value 1
Presentation transcript:

Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri

Lecture Overview Introduction to Functions –Dietel & Dietel: Chapter 3. –D.S. Malik: Chapters 6 and 7.

Functions: Divide and Conquer  To write a program  Develop the algorithm that the program will use  Translate the algorithm into the programming language  Top Down Design (also called stepwise refinement)  Break the algorithm into subtasks  Break each subtask into smaller subtasks  Eventually the smaller subtasks are trivial to implement in the programming language

Introduction to Functions Divide and conquer technique –Construct a large program from small, simple pieces (e.g., components) Functions –Facilitate the design, implementation, operation and maintenance of large programs C++ Standard Library math functions

Benefits of top-down design Subtasks, or functions in C++, make programs –Easier to understand –Easier to change –Easier to write –Easier to test –Easier to debug –Easier for teams to develop

Program Components in C++ Programs written by –combining new functions with “prepackaged” functions in the C++ standard library. –The standard library provides a rich collection of functions. Functions are invoked by a function call –A function call specifies the function name and provides information (as arguments) that the called function needs –Boss to worker analogy: A boss (the calling function or caller) asks a worker (the called function) to perform a task and return (i.e., report back) the results when the task is done.

Function Example

Function Components Functions Declaration and Definition Function Call Local variables –Known only in the function in which they are defined –All variables declared in function definitions are local variables Parameters –Local variables passed when the function is called that provide the function with outside information

Function Declaration Tells the return type Tells the name of the function Tells how many arguments are needed Tells the types of the arguments Tells the formal parameter names –Formal parameters are like placeholders for the actual arguments used when the function is called –Formal parameter names can be any valid identifier Example: double total_cost(int number_par, double price_par); // Compute total cost including 5% sales tax on // number_par items at cost of price_par each

Function Definition Provides the same information as the declaration Describes how the function does its task Example: double total_cost(int number_par, double price_par) { const double TAX_RATE = 0.05; //5% tax double subtotal; subtotal = price_par * number_par; return (subtotal + subtotal * TAX_RATE); } function header function body

Function Definitions Create customized functions to –Take in data –Perform operations –Return the result Format for function definition: return-value-type function-name ( parameter-list ) { declarations and statements } Example : int square( int y) { return y * y; }

Placing Definitions A function call must be preceded by either –The function’s declaration or –The function’s definition If the function’s definition precedes the call, a declaration is not needed Placing the function declaration prior to the main function and the function definition after the main function leads naturally to building your own libraries in the future.

The Return Statement Ends the function call Returns the value calculated by the function Syntax: return expression; – expression performs the calculation or –expression is a variable containing the calculated value Example: return subtotal + subtotal * TAX_RATE;

Local Variables Variables declared in a function: –Are local to that function, they cannot be used from outside the function –Have the function as their scope Variables declared in the main part of a program: –Are local to the main part of the program, they cannot be used from outside the main part –Have the main part as their scope 3.5

Global Constants Global Named Constant –Available to more than one function as well as the main part of the program –Declared outside any function body –Declared outside the main function body –Declared before any function that uses it Example: const double PI = ; double volume(double); int main() {…} –PI is available to the main function and to function volume

Global Variables Global Variable -- rarely used when more than one function must use a common variable –Declared just like a global constant except const is not used. –Generally make programs more difficult to understand and maintain.

Scope of Variables

Calling function square

Formal Parameters are Local Variables Formal Parameters are actually variables that are local to the function definition –They are used just as if they were declared in the function body –Do NOT re-declare the formal parameters in the function body, they are declared in the function declaration The call-by-value mechanism –When a function is called the formal parameters are initialized to the values of the arguments in the function call

Software Engineering Tip To promote software reusability, every function should be limited to performing a single, well-defined task, and the name of the function should express that task effectively. Such functions make programs easier to write, test, debug and maintain.

Error Prevention Tip A small function that performs one task is easier to test and debug than a larger function that performs many tasks.

Header Files Header files –Contain function prototypes for library functions –,, etc. –Load with #include Example: #include Custom header files –Defined by the programmer –Save as filename.h –Loaded into program using #include "filename.h"

Void Functions Do not return a value. May or may not have formal parameters Void functionName() { Statements }

Predefined Functions  C++ comes with libraries of predefined functions  Example: sqrt function  the_root = sqrt(9.0);  returns, or computes, the square root of a number  The number, 9, is called the argument  the_root will contain

Function Libraries Predefined functions are found in libraries The library must be “included” in a program to make the functions available An include directive tells the compiler which library header file to include. To include the math library containing sqrt(): #include

Function Calls sqrt(9.0) is a function call –It invokes, or sets in action, the sqrt function –The argument (9), can also be a variable or an expression A function call can be used like any expression –bonus = sqrt(sales) / 10; –Cout << “The side of a square with area “ << area << “ is “ << sqrt(area);

Math library functions.

Function Call Syntax Function_name (Argument_List) –Argument_List is a comma separated list: (Argument_1, Argument_2, …, Argument_Last) Example: –side = sqrt(area); –cout << “2.5 to the power 3.0 is “ << pow(2.5, 3.0);