Chapter 5 Functions DDC 2133 Programming II.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Chapter 7: User-Defined Functions II
Chapter 5 Functions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 5 Function Basics.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
Introduction to Java Programming, 4E Y. Daniel Liang.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
1 Chapter 5 Methods. 2 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
1 Topic 04 Methods Programming II/A CMC2522 / CIM2561 Bavy Li.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Chapter 6 Functions 1. Opening Problem 2 Find the sum of integers from 1 to 10, from 20 to 37, and from 35 to 49, respectively.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
1 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Chapter 5: Methods 1. Objectives To declare methods, invoke methods, and pass arguments to a method (§ ). To use method overloading and know ambiguous.
Methods 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Introducing Methods.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
Programming in C++ 1. Learning Outcome  At the end of this slide, student able to:  Understand the usage of classes and functions.  Understand static.
1 Chapter 6 Methods. 2 Objectives F To declare methods, invoke methods, and pass arguments to a method. F To use method overloading and know ambiguous.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Liang, Introduction to C++ Programming, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Advanced Function Features.
FUNCTIONS IN C++. DEFINITION OF A FUNCTION A function is a group of statements that together perform a task. Every C++ program has at least one function,
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 6 Functions.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
1 Chapter 6 Methods. 2 Objectives F To declare methods, invoke methods, and pass arguments to a method. F To use method overloading and know ambiguous.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Methods. Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
C# Programming Methods.
Classes - Intermediate
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Methods Dr. Musab Zghoul.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 5 Functions Lecturer: Mrs Rohani Hassan.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Methods.
INF120 Basics in JAVA Programming AUBG, COS dept Lecture 07 Title: Methods, part 1 Reference: MalikFarrell, chap 1, Liang Ch 5.
Reference: COS240 Syllabus
Suppose we want to print out the word MISSISSIPPI in big letters.
Chapter 5 Function Basics
Chapter 5 Methods.
Chapter 6: Methods CS1: Java Programming Colorado State University
Chapter 6 Functions.
Chapter 5 Function Basics
Chapter 5 Functions.
Chapter 6 Methods 1.
Chapter 3 Methods.
Chapter 5 Methods.
Chapter 6 Methods.
Chapter 5 Function Basics
METHODS (FUNCTIONS) By: Lohani Adeeb khan.
Group Status Project Status.
CS2011 Introduction to Programming I Methods (II)
Chapter 6 Methods.
Chapter 5 Methods.
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Week 4 Lecture-2 Chapter 6 (Methods).
Chapter 5 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Unit-1 Introduction to Java
Functions I Creating a programming with small logical units of code.
Corresponds with Chapter 5
Chapter 6: Methods CS1: Java Programming Colorado State University
Chapter 4 Test Review First day
Presentation transcript:

Chapter 5 Functions DDC 2133 Programming II

Objectives To create functions, invoke functions, and pass arguments to a function (§5.2-5.4). To understand the differences between pass-by-value and pass-by-reference (§§5.5-5.6). To use function overloading and understand ambiguous overloading (§5.7). To use function prototypes for declaring function headers (§5.8). To create header files for reusing functions (§5.11). To determine the scope of local and global variables (§5.13). To develop applications using the C++ mathematical functions (§5.14). To design and implement functions using stepwise refinement (§5.15). DDC 2133 Programming II

Introducing Functions A function is a collection of statements that are grouped together to perform an operation. DDC 2133 Programming II

Introducing Functions, cont. Function signature is the combination of the function name and the parameter list. The variables defined in the function header are known as formal parameters. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. DDC 2133 Programming II

Introducing Functions (cont.) A Function may return a value. The returnValueType is the data type of the value the function returns. If the function does not return a value, the returnValueType is the keyword void. DDC 2133 Programming II

Calling Functions Listing 5.1 Testing the max Function This program demonstrates calling a Function max to return the largest of the int values TestMax DDC 2133 Programming II

Calling Functions (cont.) animation Calling Functions (cont.) DDC 2133 Programming II

Trace Function Invocation animation Trace Function Invocation i is now 5 DDC 2133 Programming II

Trace Function Invocation animation Trace Function Invocation j is now 2 DDC 2133 Programming II

Trace Function Invocation animation Trace Function Invocation invoke max(i, j) DDC 2133 Programming II

Trace Function Invocation animation Trace Function Invocation invoke max(i, j) Pass the value of i to num1 Pass the value of j to num2 DDC 2133 Programming II

Trace Function Invocation animation Trace Function Invocation declare variable result DDC 2133 Programming II

Trace Function Invocation animation Trace Function Invocation (num1 > num2) is true since num1 is 5 and num2 is 2 DDC 2133 Programming II

Trace Function Invocation animation Trace Function Invocation result is now 5 DDC 2133 Programming II

Trace Function Invocation animation Trace Function Invocation return result, which is 5 DDC 2133 Programming II

Trace Function Invocation animation Trace Function Invocation return max(i, j) and assign the return value to k DDC 2133 Programming II

Trace Function Invocation animation Trace Function Invocation Execute the print statement DDC 2133 Programming II

Call Stacks DDC 2133 Programming II

i is declared and initialized animation Trace Call Stack i is declared and initialized DDC 2133 Programming II

j is declared and initialized animation Trace Call Stack j is declared and initialized DDC 2133 Programming II

animation Trace Call Stack Declare k DDC 2133 Programming II

animation Trace Call Stack Invoke max(i, j) DDC 2133 Programming II

pass the values of i and j to num1 and num2 animation Trace Call Stack pass the values of i and j to num1 and num2 DDC 2133 Programming II

Trace Call Stack animation (num1 > num2) is true DDC 2133 Programming II

Trace Call Stack animation Assign num1 to result DDC 2133 Programming II

Return result and assign it to k animation Trace Call Stack Return result and assign it to k DDC 2133 Programming II

Execute print statement animation Trace Call Stack Execute print statement DDC 2133 Programming II

void Functions The preceding section gives an example of a nonvoid function. This section shows how to declare and invoke a void function. Listing 5.2 gives a program that declares a function named printGrade and invokes it to print the grade for a given score. TestVoidFunction DDC 2133 Programming II