1 Engineering Problem Solving With C++ An Object Based Approach Chapter 5 Functions.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Introduction to C Programming
User Defined Functions
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Chapter Five Functions
Modular Programming With Functions
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Chapter 5 Functions.
An Introduction to Programming with C++ Fifth Edition
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)
 Monday, 9/30/02, Slide #1 CS106 Introduction to CS1 Monday, 9/30/02  QUESTIONS (on HW02, etc.)??  Today: Libraries, program design  More on Functions!
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Chapter 6: Functions.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Copyright © 2012 Pearson Education, Inc. Chapter 6 Modular Programming with Functions.
1 Chapter 9 Scope, Lifetime, and More on Functions.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
CPS120: Introduction to Computer Science Functions.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
CPS120: Introduction to Computer Science Lecture 14 Functions.
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Chapter 9 Functions Dept of Computer Engineering Khon Kaen University.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
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.
Chapter 6 Functions. Topics Basics Basics Simplest functions Simplest functions Functions receiving data from a caller Functions receiving data from a.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Chapter 9: Value-Returning Functions
User-Written Functions
A Lecture for the c++ Course
Chapter 5 Functions.
FUNCTIONS IN C++.
CSCI 161: Introduction to Programming Function
User-defined Functions
Modular Programming with Functions
User Defined Functions
User-defined Functions
Pass by Reference.
Chapter 6: User-Defined Functions I
Chapter 9: Value-Returning Functions
The Function Prototype
Fundamental Programming
Programming fundamentals 2 Chapter 1: Functions (Sub-Algorithms)
Predefined Functions Revisited
Functions Imran Rashid CTO at ManiWeber Technologies.
Single-Result Functions & Modularity
CS1201: Programming Language 2
Presentation transcript:

1 Engineering Problem Solving With C++ An Object Based Approach Chapter 5 Functions

2 A program can be thought of as a collection of sub parts or sub tasks: input data analyze data output results In C++ these sub tasks are often organized into functions.

3 Functions Complex problems can be broken down into sub tasks, each of which is easy to implement in C++. What are some other advantages to using functions, as opposed to writing the entire solution in main? –Multiple programmers –Testing/Debugging/Maintaining –Reduce duplication of code

4 Functions Pre-defined –standard libraries User defined

5 Pre-defined Functions - Example #include using namespace std; int main() { double angle; cout << “input angle in radians: “; cin >> angle; cout << “\nthe sine of the angle is “ << sin(angle) << endl; return 0; }//end main

6 Programmer Defined Functions Terminology Function Prototype –describes how a function is called Function Call Function Arguments –used in the function call Function Definition –function header –function body Formal Parameters –used in function definition Formal parameters must agree with arguments in order, number and data type, but the identifiers can be different.

7 Programmer Defined Functions Can be defined to –return a single value to the calling function –perform a task –change the value of multiple variables

8 Value Returning Functions A function returns a single value to the calling program The function header declares the type of value to be returned A return statement is required in the body of the function

9 n! example n! = n*(n-1)*(n-2)*…*1 n is a positive integer 0! is 1 by definition

10 //function definition: n! = n*(n-1)*(n-2)*…*1, // 0! is 1 by definition //Function fact returns n! //Function fact assumes n is non-negative integer int fact(int n) //function header, NO SEMICOLON {int nfact = 1; while(n>1) {nfact = nfact*n; n--; }//end while block return(nfact); }//end fact Example - factorial function

11 Calling a function - a function prototype is required int fact(int n);//function prototype, semicolon required // parameter identifier is optional #include using namespace std; int main() { int n; cin >> n; if(n>=0) cout<<n<<“! is ” <<fact(n)<<endl; //n is the argument else cout <<“factorial not defined for negative numbers” <<endl; return 0; }//end main

12 Calling a function- second example int fact(int);//function prototype, //parameter identifier is optional #include using namespace std; int main() { int n, factorial; cin >> n; if(n>=0) {factorial = fact(n);//function call cout << n <<“! is ” << factorial << endl; } else cout << “factorial not defined for negative numbers” << endl; return 0; }//end main

13 2 Points of Style When Writing Value Returning Functions Formal parameters are used to pass information to the function. cin statements are usually not required. A return statement returns a value to the calling program. cout statements are usually not required. Use library functions as model (sin, log, etc)

14 void Functions A void function may be called to perform a particular task (clear the screen) modify data perform input and output A void function does not return a value to the calling program return statement is optional if a return statement is used, it has the following form –return;

15 Example of void function //output formatted date //function definition void print_date(int mo, int day, int year) //function header {string month; switch(mo) {case 1: month = “January”; break; case 2: month = “February”; break; … case 12: month = “December”; }//end switch cout << month << ‘ ’ << day << “, << year << endl; return;//return is optional } //end print date

16 Parameter Passing - pass by value Pass by value –the default in C++ (except when passing arrays as arguments to functions) –formal parameter receives the value of the argument –changes to the formal parameter do not affect the argument

17 #include using namespace std; int fact(int);//function prototype int main() {int n, factorial; cin >> n; if(n>=0) {factorial = fact(n);//function call cout << n <<“! is “ << factorial << endl; }//end if return 0; }//end main int fact(int n)//function header, NO SEMICOLON { int nfact = 1; while(n>1) {nfact = nfact*n; n--; }//end while block return(nfact); } //end fact

18 Parameter Passing - pass by reference Pass by reference –append an & to the parameter data type in both the function prototype and function header void get_date(int& day, int& mo, int& year) –formal parameter receives the address of the argument –any changes to the formal parameter directly change the value of the argument

19 Example - pass by reference #include using namespace std; void swap(double&, double&); //function prototype int main() { double x=5, y=10; swap(x,y);//function call; x y are arguments cout >> “x = “ << x << ‘,’ << “ y= “ << y << endl; return 0; }//end main Output is: x = 10, y = 5

20 Example - pass by reference //Function swap interchanges the values of two variables //function definition void swap(double& x, double& y)//function header { double temp;//local variable temp temp = x; x=y; y=temp; return;//optional return statement }//end swap

21 Practice! - What is the output? #include using namespace std; void fun(int&, int&, int); int main() {int c1=1, c2=2, c3=3; cout << c1 << ‘,’ << c2 << ‘,’ << c3 << endl; fun(c1,c2,c3); cout << c1 << ‘,’ << c2 << ‘,’ << c3 << endl; fun(c3, c2, c1); cout << c1 << ‘,’ << c2 << ‘,’ << c3 << endl; return 0; } void fun(int& a1, int& a2, int a3) {a1++; a2++; a3--; } 1,2,3 2,3,3 2,4,4

22 Storage Class and Scope Scope refers to the portion of the program in which it is valid to reference a function or a variable Storage class refers to the lifetime of a variable

23 Scope Local scope - a local variable is defined within a function or a block and can be accessed only within the function or block that defines it Global scope - a global variable is defined outside the main function and can be accessed by any function within the program file. Strongly discouraged!!