Lecture 8 Tao Wang 1

Similar documents
Chapter 6 : Modularity Using Functions (pp )

Lecture 2 Tao Wang 1

Lecture 18 Tao Wang 1

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

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

CSc Introduction to Computing

ภาคการศ กษาท 1 ป การศ กษา 2556

Computer Programming: C++

Lecture 4 Tao Wang 1

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

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Lecture 23: Pointer Arithmetic

Lesson 05 Methods. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Chapter 4 Defining Classes I

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

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

Lecture 5 Tao Wang 1

Programming Language. Functions. Eng. Anis Nazer First Semester

15 FUNCTIONS IN C 15.1 INTRODUCTION

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

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

Chapter 1. Section 1.4 Subprograms or functions. CS 50 - Hathairat Rattanasook

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

Fundamentals of Programming Session 25

M.CS201 Programming language

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Unit 7. Functions. Need of User Defined Functions

Lecture 21 Tao Wang 1

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

C++ for Engineers and Scientists. Third Edition. Chapter 12 Pointers

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

Chapter 10 Introduction to Classes

Getting started with C++ (Part 2)

Come and join us at WebLyceum

Lecture 3 Tao Wang 1

Creating a C++ Program

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


PLT Fall Shoo. Language Reference Manual

BITG 1113: Function (Part 2) LECTURE 5

A A B U n i v e r s i t y

Object Oriented Design

TDDE18 & 726G77. Functions

COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS

C Programming for Engineers Functions

Introduction to Programming

Basics of Java: Expressions & Statements. Nathaniel Osgood CMPT 858 February 15, 2011

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Introduction to Programming (Java) 4/12

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

Functions and Recursion

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

UNIT- 3 Introduction to C++

LECTURE 06 FUNCTIONS

Lecture (07) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Fall 2015, Programming I

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

Probably the best way to start learning a programming language is with a program. So here is our first program:

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Functions. Arash Rafiey. September 26, 2017

6.096 Introduction to C++

Functions. Using Bloodshed Dev-C++ Heejin Park. Hanyang University

Chapter 3 - Functions

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

Arrays. Week 4. Assylbek Jumagaliyev

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

CMPE-013/L. Introduction to C Programming. Unit testing

C++ PROGRAMMING SKILLS Part 3 User-Defined Functions

CSc 10200! Introduction to Computing. Lecture 4-5 Edgardo Molina Fall 2013 City College of New York

Chapter 2: Functions and Control Structures

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

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

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

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

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

Chapter 6: Functions

Advanced Computer Programming

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

Computer Programming C++ Classes and Objects 6 th Lecture

Functions. Cedric Saule

User Defined Functions

Advanced Activities - Information and Ideas

BSM540 Basics of C Language

Chapter 3 - Functions

Object Oriented Design

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

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

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

Function. specific, well-defined task. whenever it is called or invoked. A function to add two numbers A function to find the largest of n numbers

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

AMCAT Procedure functions and scope Sample Questions

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

CS111: PROGRAMMING LANGUAGE II

QUIZ. 0] Define arrays 1] Define records 2] How are arrays and records: (a) similar? (b) different?

SUHAIL T A

Part I: Introduction to Functions

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

Transcription:

Lecture 8 Tao Wang 1

Objectives In this chapter, you will learn about: Sorting 3 numbers review Function overview Function and parameter declarations Function with empty parameter lists Default arguments Put sorting into a function Scientists, Third Edition 2

Sorting of three numbers Solution 1 using if-else Check all possible combinations Solution 2 using if Find the first minimum and swap the results Find the second minimum,... Questions, what if we want to reuse the algorithm and sort another three numbers? Fundamentals of Information Systems, Fifth Edition 3

Functions Functions allow us to organize complex programs Allow use to reuse code A function should have a well defined operation It takes values as inputs (arguments) It outputs some value (return value) i.e. setw(5); setprecision(2); ceil(3.5); pow(3.0, 4.0); log10(1231); Fundamentals of Information Systems, Fifth Edition 4

Function Function prototype Declaration statement Function body Function implementation Function call Action summoning Fundamentals of Information Systems, Fifth Edition 5

Function Prototype return type function name parameter type list double my_function( int, int ); Fundamentals of Information Systems, Fifth Edition 6

Function Body function name parameter type and name list double my_function(int x, int y) { double result; result = x+y; return result; } return value Fundamentals of Information Systems, Fifth Edition 7

Function Call Another function int main() { } double a, b, z; z = my_function(a, b); Function call Argument list Fundamentals of Information Systems, Fifth Edition 8

Function and Parameter Declarations Interaction with a function includes: Passing data to a function correctly when its called Returning values from a function when it ceases operation A function is called by giving the function s name and passing arguments in the parentheses following the function name Figure 6.1 Calling and passing data to a function Scientists, Third Edition 9

Function and Parameter Declarations (continued) Scientists, Third Edition 10

Function and Parameter Declarations (continued) Before a function is called, it must be declared to function that will do calling Declaration statement for a function is referred to as function prototype Function prototype tells calling function: Type of value that will be formally returned, if any Data type and order of the values the calling function should transmit to the called function Function prototypes can be placed with the variable declaration statements above the calling function name or in a separate header file Scientists, Third Edition 11

Calling a Function Requirements when calling a function include: Using the name of the function Enclosing any data passed to the function in the parentheses following the function name Using the same order and type declared in the function prototype Scientists, Third Edition 12

Calling a Function (continued) The items enclosed in the parentheses are called arguments of the called function Figure 6.2 Calling and passing two values to findmax() Scientists, Third Edition 13

Calling a Function (continued) Figure 6.3 The findmax() function receives actual values Scientists, Third Edition 14

Calling a Function (continued) Figure 6.3: The findmax() function does not receive the variables named firstnum and secnum and has no knowledge of the variable names The function receives the values in these variables and must then determine where to store those values before it does anything else Scientists, Third Edition 15

Defining a Function Every C++ function consists of two parts: Function header Function body Function header s purpose: Identify data type of value function returns, provide function with name, and specify number, order, and type of arguments function expects Function body s purpose: To operate on passed data and return, at most, one value directly back to the calling function Scientists, Third Edition 16

Defining a Function (continued) Figure 6.4 The general format of a function Figure 6.5 Storing values in parameters Scientists, Third Edition 17

Placement of Statements General rule for placing statements in a C++ program: All preprocessor directives, named constants, variables, and functions must be declared or defined before they can be used Although this rule permits placing both preprocessor directives and declaration statements throughout the program, doing so results in poor program structure Scientists, Third Edition 18

Functions with Empty Parameter Lists Although useful functions having an empty parameter list are extremely limited, they can occur Function prototype for such a function requires writing the keyword void or nothing at all between the parenthesis following the function s name i.e. void foo(); void foo(void); Scientists, Third Edition 19

Default Arguments C++ provides default arguments in a function call for added flexibility Primary use of default arguments is to extend parameter list of existing functions without requiring any change in calling parameter lists already used in a program Default arguments are listed in the function prototype and transmitted automatically to the called function when the corresponding arguments are omitted from the function call Scientists, Third Edition 20

Default Arguments (continued) Example: Function prototype with default arguments void example(int, int = 5, double = 6.78) Sample valid calls to the example function example(7,2,9.3) // no defaults used example(7,2) // same as example(7, 2, 6.78) example(7) // same as example(7, 5, 6.78) Scientists, Third Edition 21

Sorting 3 numbers using a function Function prototype void sort(int, int, int); Function body void sort(int a, int b, int c); Function call sort(a, b,c); Fundamentals of Information Systems, Fifth Edition 22