Chapter 5 Function Basics

Slides:



Advertisements
Similar presentations
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Advertisements

Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Chapter 5 Functions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 5 Function Basics.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
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.
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:
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
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.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
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.
Chapter 5: Methods 1. Objectives To declare methods, invoke methods, and pass arguments to a method (§ ). To use method overloading and know ambiguous.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
Programming in C++ 1. Learning Outcome  At the end of this slide, student able to:  Understand the usage of classes and functions.  Understand static.
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.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
Section 4 - Functions. All of the programs that we have studied so far have consisted of a single function, main(). However, having more than one function.
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.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
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.
Chapter 5 Function Basics
Chapter 5 Methods.
Functions and an Introduction to Recursion
Chapter 6: Methods CS1: Java Programming Colorado State University
Chapter 6 Functions.
Chapter 5 Functions DDC 2133 Programming II.
Chapter 5 Function Basics
Chapter 5 Functions.
Chapter 6 Methods 1.
Chapter 3 Methods.
Chapter 6 Methods.
User Defined Functions
Chapter 6 Methods: A Deeper Look
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
6 Chapter Functions.
Group Status Project Status.
Chapter 5 Function Basics
Chapter 6 Methods.
Chapter 5 Methods.
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Chapter 6: User-Defined Functions I
Week 4 Lecture-2 Chapter 6 (Methods).
Functions and an Introduction to Recursion
Chapter 5 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Fundamental Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
Unit-1 Introduction to Java
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

Chapter 5 Function Basics

Motivations A method is a construct for grouping statements together to perform a function. Using a method, you can write the code once for performing the function in a program and reuse it by many other programs. For example, often you need to find the maximum between two numbers. Whenever you need this function, you would have to write the following code: If you define this function for finding a maximum number between any two numbers in a method, you don’t have to repeatedly write the same code. You need to define it just once and reuse it by any other programs. int result; if (num1 > num2) result = num1; else result = num2;

Objectives To define functions (§5.2). To invoke functions with a return value (§5.3). To invoke functions without a return value (§5.4). To pass arguments (§5.5). To develop reusable code that is modular, easy-to-read, easy-to-debug, and easy-to-maintain (§5.6). To use function overloading and understand ambiguous overloading (§5.7). To use function prototypes for function headers (§5.8). To create header files for reusing functions (§5.9). To separate function headers from implementation (§5.10). To develop functions for generating random characters (§5.11). To develop applications using the C++ mathematical functions (§5.12). To develop applications using the C++ character functions (§5.13).

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

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.

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.

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.

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.cpp

TestMax.cpp #include <iostream> using namespace std; int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } int main(){ int i = 5; int j = 2; int k = max(i, j); cout << "The maximum between " << i << " and " << j << " is " << k <<endl; system("PAUSE"); return 0;} TestMax.cpp

Calling Functions, cont. animation Calling Functions, cont.

Trace Function Invocation animation Trace Function Invocation i is now 5

Trace Function Invocation animation Trace Function Invocation j is now 2

Trace Function Invocation animation Trace Function Invocation invoke max(i, j)

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

Trace Function Invocation animation Trace Function Invocation declare variable result

Trace Function Invocation animation Trace Function Invocation (num1 > num2) is true since num1 is 5 and num2 is 2

Trace Function Invocation animation Trace Function Invocation result is now 5

Trace Function Invocation animation Trace Function Invocation return result, which is 5

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

Trace Function Invocation animation Trace Function Invocation Execute the print statement