Functions. Lecture 6 COP 3014 Spring February 11, 2018

Similar documents
Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

Lecture 04 FUNCTIONS AND ARRAYS

6.096 Introduction to C++

Lab Instructor : Jean Lai

C++ Programming: From Problem Analysis to Program Design, Third Edition

Chapter 3 - Functions

Lecture 5 Tao Wang 1

Unit 7. Functions. Need of User Defined Functions

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Outline. Functions. Functions. Predefined Functions. Example. Example. Predefined functions User-defined functions Actual parameters Formal parameters

C++ Programming Lecture 11 Functions Part I

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

C++ Functions. Last Week. Areas for Discussion. Program Structure. Last Week Introduction to Functions Program Structure and Functions

More on Functions. Lecture 7 COP 3014 Spring February 11, 2018

CSCE 206: Structured Programming in C++

Flow Control. CSC215 Lecture

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

Objectives. In this chapter, you will:

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

Introduction to Programming

Lecture 04 FUNCTIONS AND ARRAYS

Functions and Recursion

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

4. C++ functions. 1. Library Function 2. User-defined Function

Lecture 5: Methods CS2301

Chapter 7 User-Defined Methods. Chapter Objectives

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

C Functions. 5.2 Program Modules in C

M.CS201 Programming language

Why Is Repetition Needed?

Computer Science & Engineering 150A Problem Solving Using Computers

Test #2 October 8, 2015

Introduction. What is function? Multiple functions form a larger program Modular programming

Introduction to C++ Introduction to C++ 1

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

Chapter 2 Basic Elements of C++

Review. Modules. CS 151 Review #6. Sample Program 6.1a:

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

Programming Language. Functions. Eng. Anis Nazer First Semester

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Chapter 3 Function Basics

Statements execute in sequence, one after the other, such as the following solution for a quadratic equation:

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

Chapter 3 - Functions

Introduction to C++ Systems Programming

LECTURE 06 FUNCTIONS

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Chapter 6: Functions

C Programming for Engineers Functions

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

CSC 307 DATA STRUCTURES AND ALGORITHM ANALYSIS IN C++ SPRING 2011

Chapter 2: Basic Elements of C++

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 15 - C++ As A "Better C"

C++ How to Program, 9/e by Pearson Education, Inc. All Rights Reserved.

Computing and Statistical Data Analysis Lecture 3

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Functions that Return a Value. Approximate completion time Pre-lab Reading Assignment 20 min. 92

Function. Mathematical function and C+ + function. Input: arguments. Output: return value

1 Lexical Considerations

Chapter 3 - Functions. Chapter 3 - Functions. 3.1 Introduction. 3.2 Program Components in C++

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

CS-201 Introduction to Programming with Java

Top-Down Design Predefined Functions Programmer-Defined Functions Procedural Abstraction Local Variables Overloading Function Names

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

C++ Quick Guide. Advertisements

Functions and an Introduction to Recursion Pearson Education, Inc. All rights reserved.

Cpt S 122 Data Structures. Introduction to C++ Part II

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

A Fast Review of C Essentials Part I

CISC 1110 (CIS 1.5) Introduc2on to Programming Using C++

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

CS110D: PROGRAMMING LANGUAGE I

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

Lecture 19: Functions, Types and Data Structures in Haskell

Standard Version of Starting Out with C++, 4th Edition. Chapter 6 Functions. Copyright 2003 Scott/Jones Publishing

Lexical Considerations

6.1. Chapter 6: What Is A Function? Why Functions? Introduction to Functions

Decision Making in C

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

LECTURE 5 Control Structures Part 2

Chapter Procedural Abstraction and Functions That Return a Value. Overview. Top-Down Design. Benefits of Top Down Design.

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

Chapter 4. Procedural Abstraction and Functions That Return a Value

6 Functions. 6.1 Focus on Software Engineering: Modular Programming TOPICS. CONCEPT: A program may be broken up into manageable functions.

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Transcription:

Functions Lecture 6 COP 3014 Spring 2018 February 11, 2018

Functions A function is a reusable portion of a program, sometimes called a procedure or subroutine. Like a mini-program (or subprogram) in its own right Can take in special inputs (arguments) Can produce an answer value (return value) Similar to the idea of a function in mathematics With functions, there are 2 major points of view Builder of the function responsible for creating the declaration and the definition of the function (i.e. how it works) Caller somebody (i.e. some portion of code) that uses the function to perform a task

Why write and use functions? Divide-and-conquer Can breaking up programs and algorithms into smaller, more manageable pieces This makes for easier writing, testing, and debugging Also easier to break up the work for team development Reusability Functions can be called to do their tasks anywhere in a program, as many times as needed Avoids repetition of code in a program Functions can be placed into libraries to be used by more than one program

Using Functions The user of a function is the caller. Use a function by making calls to the function with real data, and getting back real answers. Consider a typical function from mathematics: f(x) = 2x + 5 In mathematics, the symbol x is a placeholder, and when you run the function for a value, you plug in the value in place of x. Consider the following equation, which we then simplify: y = f(10) // must evaluate f(10) y = 2 * 10 + 5 // plug in 10 for x y = 20 + 5 y = 25 // so f(10) results in 25 In programming, we would say that the call f(10) returns the value 25.

Using Functions C++ functions work in largely the same way. General format of a C++ function call: functionname(argumentlist) The argumentlist is a comma-separated list of arguments (data being sent into the method). Use the call anywhere that the returned answer would make sense. In keeping with the declare before use policy, a function call can be made ONLY if a declaration (or definition) of the function has been seen by the compiler first. This can be done by placing a declaration above the call This is handled in libraries by including the header file for the library with a #include directive

Using Functions There is a pre-defined math function sqrt, which takes one input value (of type double) and returns its square root. Sample calls: double x = 9.0, y = 16.0, z; z = sqrt(36.0); //returns 6.0 (stored in z) z = sqrt(x); //returns 3.0 (stored in z) z = sqrt(x + y); //returns 5.0(stored in z) cout<<sqrt(100.0);// prints the returned 10.0 cout<<sqrt(49); //due to automatic type conversion rules we // can send an int where a double is // expected. This call returns 7.0 cout<<sqrt(sqrt(625.0)); // function calls can // be nested. Inner function returns first, // and its return value is passed to the outer // function. This line returns 5.0

Predefined Functions There are many predefined functions available for use in various libraries. These typically include standard libraries from both C and C++ These may also include system-specific and compiler-specific libraries depending on your compiler Typically, C libraries will have names that are prefixed with the letter c. (cmath, cstdlib, cstring) To make such functions available to a program, the library must be included with the #include directive at the top of your file. Examples: #include <iostream> // common I/O routines #include <cmath> #include <cstdlib> // common math functions // common general C // functions

Building Functions The builder of a function (a programmer) is responsible for the declaration (also known as prototype) and the definition. A function declaration, or prototype, specifies three things: the function name usual naming rules for user-created identifiers the return type the type of the value that the function will return (i.e. the answer sent back) the parameter list a comma separated list of parameters that the function expects to receive (as arguments) every parameter slot must list a type (this is the type of data to be sent in when the function is called) parameter names can be listed (but optional on a declaration) parameters are listed in the order they are expected Declaration Format: return-type function-name( parameter-list );

Examples: // GOOD function prototypes int Sum(int x, int y, int z); double Average (double a, double b, double c); bool InOrder(int x, int y, int z); int DoTask(double a, char letter, int num); double Average (double, double, double); // Note: no parameter names here // okay on a declaration

Examples: // BAD prototypes (i.e. illegal) double Average(double x, y, z); // Each parameter must list a type PrintData(int x); // missing return type int Calculate(int) // missing semicolon int double Task(int x); // only one return type allowed!

Defining a Function a function definition repeats the declaration as a header (without the semi-colon), and then adds to it a function body enclosed in a block The function body is actual code that is implemented when the function is called. In a definition, the parameter list must include the parameter names, since they will be used in the function body. These are the formal parameters. Definition Format: return-type function-name( parameter-list ) { function-body (declarations and statements) } To send the return value out, use the keyword return, followed by an expression that matches the expected return type return expression;

Definition Examples: int Sum(int x, int y, int z) // add the three parameters and return the sum { int answer; answer = x + y + z; return answer; } double Average (double a, double b, double c) // add the parameters, divide by 3, return the result { return (a + b + c) / 3.0; }

Definition Examples: More than one return statement may appear in a function definition, but the first one to execute will force immediate exit from the function. bool InOrder(int x, int y, int z) /* answers yes/no to the question "are these parameters in order, smallest to largest?" Returns true for yes, false for no. */ { if (x <= y && y <= z) return true; else return false; }

Scope of Identifiers The scope of an identifier (i.e. variable) is the portion of the code where it is valid and usable A global variable is declared outside of any blocks, usually at the top of a file, and is usable anywhere in the file from its point of declaration. When in doubt, make it global == BAD PROGRAMMING PRACTICE Best to avoid global variables (except for constants, enumerations. Sometimes) Function names usually global. (prototypes placed at the top of a file, outside any blocks)

Scope of Identifiers A variable declared within a block (i.e. a compound statement) of normal executable code has scope only within that block. Includes function bodies Includes other blocks nested inside functions (like loops, if-statements, etc) Does not include some special uses of block notation to be seen later (like the declaration of a class which will have a separate scope issue) Variables declared in the formal parameter list of a function definition have scope only within that function. These are considered local variables to the function. Variables declared completely inside the function body (i.e. the block) are also local variables

void functions and empty parameter lists Parameter lists Mathematical functions must have 1 or more parameters C++ functions can have 0 or more parameters To define a function with no parameters, leave the parintheses empty Same goes for the call. (But parintheses must be present, to identify it as a function call) Return Types A mathematical function must return exactly 1 answer A C++ function can return 0 or 1 return value To declare a function that returns no answer, use void as the return type A void function can still use the keyword return inside, but not with an expression (only by itself). One might do this to force early exit from a function. To CALL a void function, call it by itself do NOT put it in the middle of any other statement or expression

Functions and the compiler The reason for the declare-before-use rule is that the compiler has to check all function CALLS to make sure they match the expectations. the expectations are all listed in a function declaration function name must match arguments passed in a call must match expected types and order returned value must not be used illegally Decisions about parameters and returns are based on type-checking. legal automatic type conversions apply when passing arguments into a funcion, and when checking what is returned against the expected return type