C++ Programming: Functions

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

Chapter 6 - Notes User-Defined Functions I

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

C++ Programming Lecture 11 Functions Part I

Engineering Problem Solving with C++, Etter/Ingber

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

Introduction to C++ Introduction to C++ 1

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

Chapter 2. Outline. Simple C++ Programs

Functions. Lecture 6 COP 3014 Spring February 11, 2018

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

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

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

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

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


Chapter 7 - Notes User-Defined Functions II

BITG 1113: Function (Part 2) LECTURE 5

C++ Program Flow Control: Selection

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

Functions and Recursion

Computer Science & Engineering 150A Problem Solving Using Computers

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

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

Exercise 1.1 Hello world

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

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

Introduction to Programming

CS242 COMPUTER PROGRAMMING

Introduction to C++ Systems Programming

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

Programmazione. Prof. Marco Bertini

Programming Language. Functions. Eng. Anis Nazer First Semester

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

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

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

Understand Execution of a Program

Downloaded from

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

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

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

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

A SHORT COURSE ON C++

Chapter 6: Functions

Object Oriented Design

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

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

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

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

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

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

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

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

Chapter 7 User-Defined Methods. Chapter Objectives

Computing and Statistical Data Analysis Lecture 3

CT 229 Java Syntax Continued

Lab Instructor : Jean Lai

Chapter 3 - Functions

C++ Programming: make

Department of Computer and Mathematical Sciences. Lab 10: Functions. CS 1410 Intro to Computer Science with C++

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

LECTURE 06 FUNCTIONS

Using Free Functions

Intermediate Programming, Spring 2017*

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Informatica 3 Syntax and Semantics

Chapter 4 Computer Science with C++ Name: Review Worksheet A Mr. Ferwerda

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

6. User Defined Functions I

Operations. Making Things Happen

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

COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS

Pointer Data Type and Pointer Variables

Implementing Abstract Data Types (ADT) using Classes

Functions and Recursion

CS313D: ADVANCED PROGRAMMING LANGUAGE

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

Pointers and Strings Prentice Hall, Inc. All rights reserved.

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

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

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

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

CSE 303: Concepts and Tools for Software Development

6.5 Function Prototypes and Argument Coercion

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

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

IS 0020 Program Design and Software Tools

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

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

LAB: INTRODUCTION TO FUNCTIONS IN C++

C++ For Science and Engineering Lecture 12

I/O Streams and Standard I/O Devices (cont d.)

Methods CSC 121 Fall 2014 Howard Rosenthal

C++ PROGRAMMING SKILLS Part 3 User-Defined Functions

CS201- Introduction to Programming Current Quizzes

Programming: Java. Chapter Objectives. Chapter Objectives (continued) Program Design Including Data Structures. Chapter 7: User-Defined Methods

Chapter 3 - Functions

CSCI-1200 Data Structures Spring 2015 Lecture 2 STL Strings & Vectors

Control Statements: Part Pearson Education, Inc. All rights reserved.

Transcription:

C++ Programming: Functions Domingos Begalli Saddleback College, Computer Science CS1B, Spring 2018 1 / Domingos Begalli CS1B Sprint 2018 C++ Introduction 1/22 22

we will cover predefined functions user-defined functions value-returning functions function prototype void functions formal and actual parameters value and reference parameters 2 / Domingos Begalli CS1B Sprint 2018 C++ Introduction 2/22 22

Basics in math... f(x) = 3 x 1 f(4) = 3 4 1 = 5 4 is the argument of f() and 5 the value of f() at x = 4 in c++ lingo, x is the formal and 4 the actual parameter 3 / Domingos Begalli CS1B Sprint 2018 C++ Introduction 3/22 22

Pre-Defined Functions in a c++ predefined function - sqrt() #include <iostream > #include <cmath > int main() { double x = 4; double y; // x i s the actual parameter below : y = 3 * sqrt(x) - 1; std::cout << "y: " << y << std::endl; return 0; } 4 / Domingos Begalli CS1B Sprint 2018 C++ Introduction 4/22 22

Function Libraries function header file description parameter result type abs(x) <cmath> absolute value of x int,double int,double ceil(x) <cmath> next whole number double double floor(x) <cmath> previous whole number double double pow(x,y) <cmath> returns x y double double exp(x) <cmath> returns e x double double sqrt(x) <cmath> returns + x double double cos(x) <cmath> cosine of x double double islower(x) <cctype> returns 1 if x is lowercase int int isupper(x) <cctype> returns 1 if x is uppercase int int tolower(x) <cctype> returns lowercase value of x int int toupper(x) <cctype> returns uppercase value of x int int time(x) <ctime> current time time_t time_t 5 / Domingos Begalli CS1B Sprint 2018 C++ Introduction 5/22 22

User-Defined Functions can return a value or not value returning functions functions that have a return type return a value of specific type void functions functions that do not have a return type can have a return statement but do not return a value 6 / Domingos Begalli CS1B Sprint 2018 C++ Introduction 6/22 22

User-Defined Functions c++ user-defined function syntax function definition: Type funcname(formal parameters) { statements } function call: funcname(actual parameters); parameters: (Type1 identifier1, Type2 identifier2... ) 7 / Domingos Begalli CS1B Sprint 2018 C++ Introduction 7/22 22

User-Defined Functions algebra.. and corresponding c++ user-defined function f(x) = 3x 2 1; f(4) = 3 4 2 1 = 47 #include <iostream > double calcsq(double x) { return 3 * x * x - 1; } int main() { double y, x = 4; y = calcsq(x); std::cout << "y: " << y << std::endl; return 0; } // prints y : 47 8 / Domingos Begalli CS1B Sprint 2018 C++ Introduction 8/22 22

User-Defined Functions function prototype/declaration the compiler issues an error if the function does not appear before main() a function declaration before main() keeps the compiler happy allows the function to be defined anywhere, including in an external file also resolves mutual calls: when funca() calls funcb() and funcb() calls funca() function prototype/declaration syntax: Type funcname(formal parameters); 9 / Domingos Begalli CS1B Sprint 2018 C++ Introduction 9/22 22

User-Defined Functions function prototype #include <iostream > double calcsq(double); // function prototype int main() { double y, x = 4; y = calcsq(x); std::cout << "y: " << y << std::endl; return 0; } // prints y : 47 // function d e f i n i t i o n a f t e r main ( ) : double calcsq(double x) { return 3 * x * x - 1; } 10 / Domingos Begalli CS1B Sprint 2018 C++ Introduction 10/22 22

User-Defined Functions the return statement return expr; where expr is either a variable, constant value, or expression the data type computed by expr has to match the calling type there might be multiple returns in a function, but only one executes a function can return only one value such returned value can, in turn, address multiple elements 11 / Domingos Begalli CS1B Sprint 2018 C++ Introduction 11/22 22

void Functions void functions functions that do not return a value have no type associated with them c++11 defines the type void however have two types of parameters: value: formal parameter receives a copy of actual parameter void funcname(type1 var1, type2 var2,... ); reference: formal parameter receives the address instead: void funcname(type1& var1, type2& var2,... ); 12 / Domingos Begalli CS1B Sprint 2018 C++ Introduction 12/22 22

Parameter Types value parameters formal parameters do not affect actual parameters actual parameters have no knowledge of formal parameters the function receives a copy of the actual parameter the function manipulates the copy of the actual parameter 13 / Domingos Begalli CS1B Sprint 2018 C++ Introduction 13/22 22

Parameter Types reference parameters formal parameters DO affect actual parameters formal parameters receive the address of actual parameters in this case, formal parameters are also called reference parameters the function manipulates the memory space of the actual parameter using reference parameters, functions can pass back multiple elements very usefull when calculations involve very large data sets 14 / Domingos Begalli CS1B Sprint 2018 C++ Introduction 14/22 22

Scope of Identifiers local vs global identifiers local: identifiers declared within functions or code blocks global: identifiers declared ouside of function definitions for example:... for (int i = 0; i < 10; ++i) { cout << "i: " << i << endl; }... the variable i is not visible/defined outside the for() loop 15 / Domingos Begalli CS1B Sprint 2018 C++ Introduction 15/22 22

Types of Variables static vs automatic variable local variables are automatic variables memory for automatic variables is allocated at block entry memory for automatic variables is deallocated at block exit global variables are static variables memory for static and global variables remains allocated during program execution the scope of static variables is limited to the code block therefore static variables are NOT global declaring static variables: static typename varname; 16 / Domingos Begalli CS1B Sprint 2018 C++ Introduction 16/22 22

Types of Variables global vs local vs static vs automatic local & global are related to visibility scope whereas static & automatic are related to memory allocation/deallocation: int main() { cout << "notice x changes:\n"; for(int ct=1; ct<=9; ct++) test(); } void test() { static int x = 0; int y = 0; x = x + 1; y = y + 1; cout << x: << x ; cout <<, y: << y << endl; } output notice x changes: x: 1, y: 1 x: 2, y: 1 x: 3, y: 1 x: 4, y: 1 x: 5, y: 1 x: 6, y: 1 x: 7, y: 1 x: 8, y: 1 x: 9, y: 1 17 / Domingos Begalli CS1B Sprint 2018 C++ Introduction 17/22 22

Debugging using drivers and stubs drivers: testing programs that exercises the relevant program stubs: testing functions that exercises the relevant program 18 / Domingos Begalli CS1B Sprint 2018 C++ Introduction 18/22 22

Function Overloading functions have different formal parameter lists when they have a different number of formal parameters, or when... the data types of the formal parameters, in the order listed, differ in at least one position function overloading occurs when two functions have the same name but different formal parameter lists 19 / Domingos Begalli CS1B Sprint 2018 C++ Introduction 19/22 22

Function Overloading example rather than... what about... and call like so: int lagerint(int x, int y); char largerchar(char a, char b); double largerdouble(double u, double v); string largerstr(string c, string d); int larger(int x, int y); char larger(char a, char b); double larger(double u, double v); string larger(string c, string d); larger(3, 5); larger('a', 'E'); more to come when we get to object oriented programming... 20 / Domingos Begalli CS1B Sprint 2018 C++ Introduction 20/22 22

Functions and Default Parameters functions can have default values for parameters void functionname(type1 var1, type2 var2=value,...); if no value is specified for a parameter, the default value is used instead all of the default value parameters must be on the far right parameters of the function a function call should have no arguments to the right of unspecified default parameter default values can be constants, global variables, or function calls caller can specify a value other than default for any defaul parameter constant values cannot be used as default value to a reference parameter program with function prototypes must have default parameters only in the prototype 21 / Domingos Begalli CS1B Sprint 2018 C++ Introduction 21/22 22

Functions and Default Parameters example... void defparfunction(int x, char a='a', int y=3);... int w,z; z = 4; w = 7; char ch='k';... defparfunction(w, ch) // ok... defparfunction(z, 'B', w) // ok... defparfunction(w, z) // i l l e g a l ; must omit z... 22 / Domingos Begalli CS1B Sprint 2018 C++ Introduction 22/22 22