Lecture 5 functions 1 ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

Slides:



Advertisements
Similar presentations
Spring Semester 2013 Lecture 5
Advertisements

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec Procedure declaration procedure p( a, b : integer, f :
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Lecture 2 Introduction to C Programming
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 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.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Introduction to C Programming
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Chapter 6: Functions.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
 2006 Pearson Education, Inc. All rights reserved Arrays.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
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.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Array (continue).
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
ECE122 Feb. 22, Any question on Vehicle sample code?
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Methods.
Part III © Copyright by Pearson Education, Inc. All Rights Reserved.
Programming Fundamentals Enumerations and Functions.
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays + Functions Outline 6.5Passing Arrays to Functions.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Constructors and Destructors
Chapter 7: User-Defined Functions II
FUNCTIONS In C++.
Chapter 10: Void Functions
Programming Fundamentals Lecture #7 Functions
User-Defined Functions
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 6 Methods: A Deeper Look
1) C program development 2) Selection structure
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company.
Constructors and Destructors
Chapter 9: Value-Returning Functions
Presentation transcript:

Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.

6.5 Function Prototypes and Argument Coercion A function prototype (also called a function declaration) tells the compiler ▫ the name of a function ▫ the type of data returned by the function ▫ the number of parameters the function expects to receive ▫ the types of those parameters and ▫ the order in which the parameters of those types are expected. 2 © by Pearson Education, Inc. All Rights Reserved.

3

6.5 Function Prototypes and Argument Coercion(cont.) The compiler refers to the function prototype to check that calls to function contain the correct number and types of arguments and that the types of the arguments are in the correct order. In addition, the compiler uses the prototype to ensure that the value returned by the function can be used correctly in the expression that called the function (e.g., a function call that returns void cannot be used as the right side of an assignment statement). 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. © by Pearson Education, Inc. All Rights Reserved. 4

6.6 Functions with Empty Parameter Lists In C++, an empty parameter list is specified by writing either void or nothing at all in parentheses. © by Pearson Education, Inc. All Rights Reserved. 5

6

7

8

6.7 Function control There are three ways to return control to the point at which a function was invoked. 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; If the function does return a result, the statement  return expression ; evaluates expression and returns the value of expression to the caller. © by Pearson Education, Inc. All Rights Reserved. 9

6.17 Function Overloading C++ enables several functions of the same name to be defined, as long as they have different signatures. This is called function overloading. The C++ compiler selects the proper function to call by examining the number, types and order of the arguments in the call. Function overloading is used to create sev-eral functions of the same name that perform similar tasks, but on different data types. © by Pearson Education, Inc. All Rights Reserved. 10

© by Pearson Education, Inc. All Rights Reserved. 11

© by Pearson Education, Inc. All Rights Reserved. 12

7.5 Passing Arrays to Functions To pass an array argument to a function, specify the name of the array without any brack-ets. When passing an array to a function, the array size is normally passed as well, so the function can process the specific number of elements in the array. C++ passes arrays to functions by reference—the called functions can modify the element values in the callers’ original arrays. The value of the name of the array is the address in the computer’s memory of the first element of the array. ▫ Because the start-ing address of the array is passed, the called function knows precisely where the array is stored in memory. © by Pearson Education, Inc. All Rights Reserved. 13

Example: passing array to function Write C++ program using function to calculate the sum of array 14 © by Pearson Education, Inc. All Rights Reserved.