Chapter 6 - Notes User-Defined Functions I

Similar documents
Chapter 7 - Notes User-Defined Functions II

C++ Programming Lecture 11 Functions Part I

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

Programming Language. Functions. Eng. Anis Nazer First Semester

C++ Programming: Functions

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

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

Introduction to Programming

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

Using Free Functions

Engineering Problem Solving with C++, Etter/Ingber

Chapter 2. Outline. Simple C++ Programs

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

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

Summary of basic C++-commands

CT 229 Java Syntax Continued

Computer Science & Engineering 150A Problem Solving Using Computers

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

Chapter 3 - Functions

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.6

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

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

C Functions. 5.2 Program Modules in C

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

Functions and Recursion

Downloaded from

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Lab Instructor : Jean Lai

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

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Objectives. In this chapter, you will:

A. Introduction to Function 1. Modular Programming input processing output functions library functions 2. Function 1. Benefit of Using Functions

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

Functions. Systems Programming Concepts

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

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

CSCE 110 PROGRAMMING FUNDAMENTALS

C++ Overview. Chapter 1. Chapter 2

ANSI C Programming Simple Programs

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

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

Python Lists: Example 1: >>> items=["apple", "orange",100,25.5] >>> items[0] 'apple' >>> 3*items[:2]


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

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 (!

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

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

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

Fundamentals of Programming & Procedural Programming

Function I/O. Function Input and Output. Input through actual parameters. Output through return value. Input/Output through side effects

C++ PROGRAMMING SKILLS Part 3 User-Defined Functions

BITG 1113: Function (Part 2) LECTURE 5

(created by professor Marina Tanasyuk) FUNCTIONS

The C++ Language. Arizona State University 1

Function I/O. Last Updated 10/30/18

CS Exam 2 Study Suggestions

UNIT- 3 Introduction to C++

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

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

3.1. Chapter 3: The cin Object. Expressions and Interactivity

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

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

Methods CSC 121 Fall 2016 Howard Rosenthal

Chapter 4. Procedural Abstraction and Functions That Return a Value

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

Methods CSC 121 Fall 2014 Howard Rosenthal

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

C Program Structures

CMSC 202 Midterm Exam 1 Fall 2015

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

C Programs: Simple Statements and Expressions

Introduction to Engineering gii

Downloaded from Chapter 2. Functions

Pre- Defined Func-ons in C++ Review for Midterm #1

Methods CSC 121 Spring 2017 Howard Rosenthal

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

Functions. A function is a subprogram that performs a specific task. Functions you know: cout << Hi ; cin >> number;

A SHORT COURSE ON C++

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

Lecture 14. Daily Puzzle. Math in C. Rearrange the letters of eleven plus two to make this mathematical statement true. Eleven plus two =?

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

CSc Introduction to Computing

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Getting started with C++ (Part 2)

Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey

LECTURE 02 INTRODUCTION TO C++

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Lab 1. largest = num1; // assume first number is largest

Chapter 3 - Functions

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Outline. Why do we write functions? Introduction to Functions. How do we write functions? Using Functions. Introduction to Functions March 21, 2006

The Number object. to set specific number types (like integer, short, In JavaScript all numbers are 64bit floating point

Variable and Data Type 2

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

Transcription:

Chapter 6 - Notes User-Defined Functions I I. Standard (Predefined) Functions A. A sub-program that performs a special or specific task and is made available by pre-written libraries in header files. B. Table of some Predefined Functions Function Standard Header File Purpose Parameter(s) Type Result abs(x) <cstdlib> Returns the absolute value of its argument: abs(-7) = 7 ceil(x) Returns the smallest whole number that is not less than x: ceil(56.34) = 57.0 cos(x) Returns the cosine of angle x: cos(0.0) = 1.0 exp(x) x Returns e, where e = 2.718: exp(1.0) = 2.71828 (radians) fabs(x) Returns the absolute value of its argument: fabs(-5.67) = 5.67 floor(x) Returns the largest whole number that is not greater than x: floor(45.67) = 45.00 pow(x,y) y Returns x ; if x is negative, y must be a whole number: pow(0.16, 0.5) = 0.4 tolower(x) <cctype> Returns the lowercase value of x if x is uppercase; otherwise, returns x toupper(x) <cctype> Returns the uppercasevalue of x if x is lowercase; otherwise, returns x Page 1

C. Examples of Predefined functions used in Programs #include <iostream> #include main ( ) base, expo, answer ; cout << "Please enter two eger numbers: " ; cin >> base >> expo ; answer = pow( base, expo ) ; cout << endl ; cout << base << " taken to the power of " << expo ; cout << " equals " << answer << endl << endl ; system("pause") ; return 0 ; OUTPUT Please enter two eger numbers: 2 4 2 taken to the power of 4 equals 16 Press any key to continue... NOTE that the above pre-defined function returns a value. Therefore, something must be done with the value. In the case of the example, the returned value is saved o the variable answer. Another way to use a pre-defined function that returns a value is to put it o a cout statement like the following example: Page 2

#include <iostream> #include main ( ) base, expo ; cout << "Please enter two eger numbers: " ; cin >> base >> expo ; cout << endl ; cout << base << " taken to the power of " << expo ; cout << " equals " << pow( base, expo ) << endl << endl ; system("pause") ; return 0 ; OUTPUT Please enter two eger numbers: 2 4 2 taken to the power of 4 equals 16 Press any key to continue... II User-Defined Functions A. Two Main Types 1. Value-Returning Functions ( functions that have a data type ) i. A function that may or may not use parameters, but always returns a single value to the calling function. 2. Void Functions ( functions that do not have a data type ) i. A function that may or may not use parameters, but does not return a value to the calling function. Page 3

III. Value-Returning Functions A. One of three (3) ways to use: 1. Save the value to memory in an assignment statement 2. Use the value in an expression 3. Pr the value to the screen or output file B. Parts of the Function 1. The Header: A single line where the data type of the function is listed first, then the name of the function, then inside of parenthesis, the are found (if any). Example of Header: sumit ( X, Y ) data type of parameter function name function data type 2. The Body: The statements used to perform the function. This consists of any legal C++ statements that can be used in the body of the main function. Declaration statements, assignment statements, Input/Output statements, and even other function calls are all found in the body of the function. Example of Body: function header sumit ( X, Y ) sum ; sum = X + Y ; function body return sum ; Page 4

3. Function Definition: The function definition consists of both the function header and function body. C. Syntax #include <iostream> area ( x_length, y_width ) return x_length * y_width ; ; function header body of function main () length, width, tot_area ; cout << "Please enter the length and width of a rectangle here: "; cin >> length >> width ; tot_area = area( length, width ); cout << endl; function called actual parameters cout << "The rectangle's area is " << tot_area << " square inches"; cout << endl << endl ; system("pause"); return 0; O R You can write functions with a prototype and define the function after the main program like it is done in your book and the following page. Page 5

#include <iostream> area ( x_length, y_width ); prototype main () length, width, tot_area ; cout << "Please enter the length and width of a rectangle here: "; cin >> length >> width ; tot_area = area( length, width ); cout << endl; function called actual parameters cout << "The the rectangle's area is " << tot_area << " square inches"; cout << endl << endl ; system("pause"); return 0; area ( x_length, y_width ) return x_length * y_width ; function header body of function D. Syntax for Formal Parameter List 1. datatype name ( data type identifier, data type identifier,... ) Examples: float divideit ( numerator, float denominator ) sumit ( num1, num2, num3, num4 ) bool islarger ( num1, num2 ) E. Syntax for Function Call 1. functionname ( actual parameter list ) Page 6

F. Syntax for Actual Parameter List 1. ( expression or variable, expression or variable,... ) Examples: divideit ( 4, 5 ) sumit ( var1, (10 + 2) / 6, var3 * 2, divideit ( 6, 3 ) ) islarger ( 5.34, var7 ) 2. Notice that the actual parameter list can consist of regular numbers (called literals), a variable that stores a value, a mathematical expression that returns a single value, or even another value returning function. 3. ALSO - A function does not have to receive parameters. In this case the function would be called with just the name of the function and an open and close parenthesis, with nothing between the parenthesis. Or Example: dothis ( ) or dothis ( void ) G. The return Statement 1. Once the function computes the value to be returned, the function returns this value via the return statement. 2. Syntax for return Statement return expression ; 3. An expression can be anything that returns a single value: a number (literal), a mathematical expression, a variable, or even another valuereturning function. 4. REMEMBER: the data type of the value being returned must match the data type of the function. Page 7

H. Function Prototype 1. Defined as the function heading without the body of the function. It is followed by a semicolon. 2. When defining a function after the main (function) program, a prototype must be used before the main (function) program begins. This is because, like variables, a function must be declared before it can be used. 3. Syntax of a Prototype datatype functionname ( ) ; #include <iostream> area ( x_length, y_width ); prototype main () length, width, tot_area ; cout << "Please enter the length and width of a rectangle here: "; cin >> length >> width ; tot_area = area( length, width ); cout << endl; function called actual parameters cout << "The the rectangle's area is " << tot_area << " square inches"; cout << endl << endl ; system("pause"); return 0; area ( x_length, y_width ) return x_length * y_width ; function header body of function Page 8