Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.

Slides:



Advertisements
Similar presentations
Spring Semester 2013 Lecture 5
Advertisements

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Chapter 5 Functions.
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.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
 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.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Chapter 6: Functions.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
1 FUNCTIONS - I Chapter 5. 2 What are functions ? Large programs can be modularized into sub programs which are smaller, accomplish a specific task and.
CPS120: Introduction to Computer Science Functions.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
1 CS161 Introduction to Computer Science Topic #9.
© 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.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
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.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Programming Fundamentals Enumerations and Functions.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
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.
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
Dr. Shady Yehia Elmashad
CSC113: Computer Programming (Theory = 03, Lab = 01)
Dr. Shady Yehia Elmashad
CSCI 161: Introduction to Programming Function
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
Dr. Shady Yehia Elmashad
Introduction to Functions
Chapter 6 Methods: A Deeper Look
6 Chapter Functions.
Chapter 6: User-Defined Functions I
Chapter 9: Value-Returning Functions
Functions Imran Rashid CTO at ManiWeber Technologies.
6 Functions.
Presentation transcript:

Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015

Outlines 2 Introduction Math Library Functions Function Definitions with Multiple Parameters Return values Argument Passing Declaration, definitions and calling of user- defined functions Functions with no type functions with Empty Parameter List 2 December 2015

Introduction Experience has shown that the best way to develop and maintain a large program is to construct it from small, simple pieces, or components. – This technique is called divide and conquer. – Any sequence of instructions that appears in a program more than once can be encapsulated into a function. In C++ we will have two types of functions: Pre-defined “such as functions in the class math ‘Cmath’ ”. User/programmer defined Another reason to use functions (and the reason they were invented, long ago) is to reduce program size. 32 December 2015

Program Components in C++ The C++ Standard Library provides a rich collection of functions. Functions you write are referred to as user-defined functions or programmer-defined functions. invokedfunction call A function is invoked by a function call, and when the called function completes its task, – either returns a result or – simply returns control to the caller. ”if it is void return value” 42 December 2015

Program Components in C++ The function’s code is stored in only one place in memory, even though the function is executed many times in the program.

Math Library Functions global functions header files Function prototypes for global functions are placed in header files, so that the global functions can be reused in any program that includes the header file and that can link to the function’s object code. The header file provides a collection of functions that enable you to perform common mathematical calculations. All functions in the is called simply by specifying the name of the function followed by parentheses containing the function’s arguments. cos (x) Some math library functions are summarized in Fig – In the figure, the variables x and y are of type double. 62 December 2015

7

8

Function Declarations vs. Definitions Example Int add(int x, int y); // Declaration (or prototype)... Void main() {... cout << x << “! is “ << add(x,y) << endl; // calling the function... }... Int add(int x, int y)// Definition {... } 9 Before a function may be called by any other function it must be either defined or declared. 2 December 2015

102 December 2015

Function Components 112 December 2015

Function Declarations vs. Definitions, cont. The compiler refers to the function prototype to check that calls to function contain – the correct numbers and types of arguments – and that the types of the arguments are in the correct order. Each argument must be consistent with the type of the corresponding parameter. If the arguments passed to a function do not match the types specified in the function’s prototype, the compiler attempts to convert the arguments to those types. 122 December 2015

Example 2, cont. #include long factorial(int); // declaration Void main() { int x; cout “ << endl; cin >> x; cout << x << “! is “ << factorial(x) << endl; } How might we call our function from a main() function? When a function is declared separately from its definition, this is called a forward declaration. Forward declarations need only to specify return type, parameters type, parameter number and order of parameters. Parameter names are irrelevant. 132 December 2015

Example 2, cont. The definition consists of a line called the declarator, followed by the function body. The definition contains the actual code for the function. long factorial(int n) // declarator { long result = 1, k = n;// function body. while(k > 1) { result *= k--; } return result; } 142 December 2015

Function with no return results If the function does return a result, the statement return expression ; If the function does not return a result (i.e., it has a void return type), control returns when the program reaches the function-ending right brace, or by execution of the statement return; 152 December 2015

Functions with no return results. The use of void // void function example #include using namespace std; void printmessage (int x) { for (int i=0; i<=x; i++) cout << "I'm a function!“<< x ; } void main () { printmessage (5); } 162 December 2015

Function Definitions with Multiple Parameters called A function is a group of statements that is executed when it is called from some point of the program. Function format with multiple parameters : ReturnType funcName ( parameter1, parameter2,...) { statements } Multiple parameters are specified in both the function prototype and the function header as a comma-separated list. Function with no parameters is specified by writing either void or nothing at all in parentheses. ReturnType funcName ( ) or ReturnType funcName (void) 172 December 2015

18

Function Definitions with Multiple Parameters, cont. { code to perform a task } ReturnType funcName (parameters) ---- ; Var = funcName (arguments); //function call ---; 19 function call There must be one argument in the function call for each parameter in the function definition. Arguments is the name of the values that the function call passes to the function for the parameters. 2 December 2015

Example #include using namespace std; int addition (int a, int b) { int r; r=a+b; return r; } 202 December 2015

int main () { int z; z = addition (5,3); cout << "The result is " << z; return 0; } The output of program: The result is 8 Example, cont. 212 December 2015

int main () { int x=5, y=3, z; z = addition (5,3); cout << "The result is " << z << '\n'; cout << "The second result is " << addition (5,3) << '\n'; cout << "The third result is " << addition (x,y) << '\n'; z= 4 + addition (x,y); cout << "The fourth result is " << z << '\n'; return 0; } The result is 8 The second result is 8 The third result is 8 The fourth result is 12 Example (some different calling methods), cont. 222 December 2015

Reference Parameters “Argument Passing” There are two ways to pass arguments to functions in C++: Pass by VALUE Pass by REFERENCE Pass by VALUE The value of a variable is passed along to the function The function creates copies of the argument passed to it. If the function modifies that value, the modifications stay within the scope of that function and do not affect the original variable’s value in memory. Pass by REFERENCE A reference to the variable is passed along to the function. The called function access the caller’s data directly. If the function modifies that value, the modifications appear also within the scope of the calling function, i.e. The function can change the original variable’s value in memory. 232 December 2015

Argument Passing by Value Passing Constants int z; z = addition (5,3); Passing Variables int x=5, y=3, z; z= addition (x,y); 242 December 2015

25 Problem: i- What is the output of the following program? void cubev(int ); void main( ) { int n=5; cout<< n<<endl; cubev( n); cout<< n<<endl; } void cubev(int x){ x= x * x * x ; } Call by Value 2 December 2015

Pass by VALUE x 5 cubev 5 n Main() 26 x 125 cubev 5 n Main() Start of the call At the end of call 2 December 2015

27 Problem: i- What is the output of the following program? void cubev(int ); void main( ) { int n=5; cout<< n<<endl; cubev( n); cout<< n<<endl; } void cubev(int x){ x= x * x * x ; } 5555 Prob: ii - Modify this program to give the cube using Call by Reference? Call by Value 2 December 2015

28 Solution: Cuber function using Call by Reference void cuber(int &); void main( ){ int n=5; cout<< n<<endl; cuber(n ); cout<< n<<endl; } void cuber(int &x){ x= x * x * x ; } December 2015

Pass by reference 5 n Main() x cuber n Main() x cuber Start of the call At the end of call 2 December 2015

Why use Pass By Reference? Because you really want changes made to a parameter to persist in the scope of the calling function. 1. You need to return more than one value to the calling function. 2. Because you are passing a large amounts of data Passing by reference passes merely a reference (pointer) to the data, not the data itself. 302 December 2015

Why use Pass By Reference? 1- Because you want to return two values void order (int & a, int & b) { if(a > b) {int temp = a; //swap them a = b; b = temp; } void main() { int num1 = 21, num2 = 12; //this pair not ordered order (num1, num2); cout << “num1 = “ << num1 << endl; cout << “num2 = “ << num2 << endl; } 312 December 2015

Pass by reference 21 num1 Main() 12 num2 a b order 12 num1 Main() 21 num2 a b order 322 December 2015

Your turn (1/4) 3. Write a function called foo() that displays the word foo. 4. A one-statement description of a function is referred to as a function d_________ or a p_________. 5. The statements that carry out the work of the function constitute the function _________. 6. A program statement that invokes a function is a function _________. 7. The first line of a function definition is referred to as the _________. 332 December 2015

Your turn (2/4) 8. A function argument is a. a variable in the function that receives a value from the calling program. b. a way that functions resist accepting the calling program’s values. c. a value sent to the function by the calling program. d. a value returned by the function to the calling program. 342 December 2015

Your turn (3/4) 13. How many values can be returned from a function? 14. True or false: When a function returns a value, the entire function call can appear on the right side of the equal sign and be assigned to another variable. 16. A function that doesn’t return anything has return type _________. 352 December 2015

Your turn (4/4) 17. Here’s a function: int times2(int a) { return (a*2); } Write a main() program that includes everything necessary to call this function. 362 December 2015

Answers 3. void foo() { cout << “foo”; } 4. declaration, prototype 5. body 6. call 7. declarator 8. C 13. one 14. Ttrue 16. Void 17. int times2(int a); // prototype main() { int alpha = times2(37); // function call }