Lecture 5 – Function (Part 2) FTMK, UTeM – Sem 1 2013/2014.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms The C Programming Language.
Advertisements

Chapter 7: User-Defined Functions II
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 6: Functions by.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 6: Functions Starting Out with C++ Early Objects
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.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Seventh Edition.
Parameter Passing Mechanisms Reference Parameters Read § §
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Parameter Passing Mechanisms Reference Parameters § §
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.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Chapter 6 Functions. Topics Basics Basics Simplest functions Simplest functions Functions receiving data from a caller Functions receiving data from a.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Eighth Edition.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Objectives: How to define and call functions. Function declarations and how they differ from function definitions. How arguments are passed to functions.
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 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
Pointers and Dynamic Arrays
Programming what is C++
Chapter 10: Void Functions
9. FUNCTIONS.
C Basics.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
User-Defined Functions
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Chapter 6: Functions Starting Out with C++ Early Objects
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
6.12 Default Arguments.
Chapter 6: Functions Starting Out with C++ Early Objects Ninth Edition
More About Data Types & Functions
Functions.
6 Chapter Functions.
Functions, Part 2 of 3 Topics Functions That Return a Value
Object Oriented Programming in java
Chapter 6: Functions Starting Out with C++ Early Objects Ninth Edition
COP 3330 Object-oriented Programming in C++
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Introduction to Programming
Object-Oriented Programming (OOP) Lecture No. 44
Instructor: Hidayah Elias Course: COMPUTER PROGRAMMING (BFC 2042)
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Functions, Part 2 of 3 Topics Functions That Return a Value
Standard Version of Starting Out with C++, 4th Edition
Functions, Part 2 of 3 Topics Functions That Return a Value
Classes and Objects Systems Programming.
Presentation transcript:

Lecture 5 – Function (Part 2) FTMK, UTeM – Sem /2014

 At the end of this lecture, you should be able to  use reference variables as parameters  when to use function that pass by value and pass by reference 2

 A mechanism that allows a function to work with the original argument from the function call, not a copy of the argument  Allows the function to modify values stored in the calling environment  Provides a way for the function to ‘return’ more than one value 3

 A reference variable is an alias for another variable  Defined with an ampersand ( & ) void getDimensions(int &, int &);  Changes to a reference variable are made to the variable it refers to  Use reference variables to implement passing parameters by reference 4

The & here in the prototype indicates that the parameter is a reference variable. Here we are passing the value by reference. (Program Continues) 5

The & also appears here in the function header. Program 6-25 (Continued) 6

 Each reference parameter must contain &  Space between type and & is unimportant  Must use & in both prototype and header  Argument passed to reference parameter must be a variable – cannot be an expression or constant  Use when appropriate – don’t use when argument should not be changed by function, or if function needs to return only 1 value 7

8 Source: whats-the-difference-between-passing-by-reference-vs-passing-by-value

 The C++ run-time library provides several functions for performing specific operations such as performing complex mathematical operations.  The C++ Standard Library can be categorized into two parts:  The Standard Function Library: This library consists of general-purpose,stand-alone functions that are not part of any class. The function library is inherited from C.  The Object Oriented Class Library: This is a collection of classes and associated functions.  Standard C++ Library incorporates all the Standard C libraries also, with small additions and changes to support type safety. 9

 The standard function library is divided into the following categories:  I/O  String and character handling  Mathematical  Time, date, and localization  Dynamic allocation  Miscellaneous  Wide-character functions 10

 Standard C++ Object Oriented Library defines an extensive set of classes that provide support for a number of common activities, including I/O, strings, and numeric processing. This library includes following:  The Standard C++ I/O Classes  The String Class  The Numeric Classes  The STL Container Classes  The STL Algorithms  The STL Function Objects  The STL Iterators  The STL Allocators  The Localization library  Exception Handling Classes  Miscellaneous Support Library 11

 Terminates the execution of a program  Can be called from any function  Can pass an int value to operating system to indicate status of program termination  Usually used for abnormal termination of program  Requires cstdlib header file  Use carefully 12

 Use an integer value to indicate program status  Often, 0 means successful completion, non- zero indicates a failure condition  Example: exit(0);  Can use the two constants define in cstdlib header that are commonly passed, to indicate success or failure: exit(EXIT_SUCCESS); exit(EXIT_FAILURE); 13

 Do you know how to use reference variables as parameters?  Do you know why and when to use function that pass by reference?  Do you know why and when to use function that pass by value? 14