(created by professor Marina Tanasyuk) FUNCTIONS

Similar documents
1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

Lab Instructor : Jean Lai

Programming Language. Functions. Eng. Anis Nazer First Semester

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

Introduction to Programming

Using Free Functions

Chapter 3 - Functions

Functions. Systems Programming Concepts

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

C Functions. 5.2 Program Modules in C

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

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

Chapter 4. Procedural Abstraction and Functions That Return a Value

Functions and Recursion

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

CS 106B Lecture 2: C++ Functions

Advanced features of the Calculate signal tool

Understanding main() function Input/Output Streams

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

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

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

4. Structure of a C++ program

A SHORT COURSE ON C++

PIC 10A. Lecture 3: More About Variables, Arithmetic, Casting, Assignment

2 nd Week Lecture Notes

Chapter 2. Outline. Simple C++ Programs

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

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

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

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.

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

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

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

Your first C++ program

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

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

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Chapter 6 - Notes User-Defined Functions I

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

Chapter 3 - Functions

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

Lecture 2:- Functions. Introduction

Why Is Repetition Needed?

Chapter Two: Fundamental Data Types

11. Arrays. For example, an array containing 5 integer values of type int called foo could be represented as:

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

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

CSc 110, Spring Lecture 7: Graphics, return values and math

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

C++ Programming Lecture 11 Functions Part I

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

Chapter 3 Function Basics

Tutorial 12 Craps Game Application: Introducing Random Number Generation and Enumerations

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

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

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

Chapter 3. Numeric Types, Expressions, and Output

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

Expressions, Input, Output and Data Type Conversions

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

C++ PROGRAMMING SKILLS Part 3 User-Defined Functions

Chapter Four: Loops II

Fundamentals of Programming & Procedural Programming

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

Engineering Problem Solving with C++, Etter/Ingber

CSCE 110 PROGRAMMING FUNDAMENTALS

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

Pointers. Reference operator (&) ted = &andy;

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

Functions. Introduction :

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

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point?

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

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

Chapter Five: Functions. by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

Introduction to Programming

Introduction to Programming using C++

12. Pointers Address-of operator (&)

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

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

Computer Programming : C++

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

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM

Reference operator (&)

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

9. Arrays. Compound Data Types: type name [elements]; int billy [5];

Introduction to C++: I

Object-Based Programming. Programming with Objects

C++ basics Getting started with, and Data Types.

Variables. Data Types.

Methods: A Deeper Look

Mentor Graphics Predefined Packages

21. Exceptions. Advanced Concepts: // exceptions #include <iostream> using namespace std;

PHY4321 Summary Notes

Excel Functions & Tables

CSE123. Program Design and Modular Programming Functions 1-1

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

Computing and Statistical Data Analysis Lecture 3

Chapter 4: Basic C Operators

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

Transcription:

FUNCTIONS (created by professor Marina Tanasyuk) In C++, a function is a group of statements that is given a name, and which can be called from some point of the program. The most common syntax to define a function is: type name ( parameter1, parameter2,...) { statements } Where: - type is the type of the value returned by the function. - name is the identifier by which the function can be called. - parameters (as many as needed): Each parameter consists of a type followed by an identifier, with each parameter being separated from the next by a comma. Each parameter looks very much like a regular variable declaration (for example: int x), and in fact acts within the function as a regular variable which is local to the function. The purpose of parameters is to allow passing arguments to the function from the location where it is called from. - statements is the function's body. It is a block of statements surrounded by braces { } that specify what the function actually does.

Let's have a look at an example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 // function example #include <iostream> using namespace std; int addition (int a, int b) { int r; r=a+b; return r; } int main () { int z; z = addition (5,3); cout << "The result is " << z; } The result is 8 This program is divided in two functions: addition and main. Remember that no matter the order in which they are defined, a C++ program always starts by calling main. In fact, main is the only function called automatically, and the code in any other function is only executed if its function is called from main (directly or indirectly).

In the example above, main begins by declaring the variable z of type int, and right after that, it performs the first function call: it calls addition. The call to a function follows a structure very similar to its declaration. In the example above, the call to addition can be compared to its definition just a few lines earlier: The parameters in the function declaration have a clear correspondence to the arguments passed in the function call. The call passes two values, 5 and 3, to the function; these correspond to the parameters a and b, declared for function addition. At the point at which the function is called from within main, the control is passed to function addition: here, execution of main is stopped, and will only resume once the addition function ends. At the moment of the function call, the value of both arguments (5 and 3) are copied to the local variables int a and int b within the function. Then, inside addition, another local variable is declared (int r), and by means of the expression r=a+b, the result of a plus b is assigned to r; which, for this case, where a is 5 and b is 3, means that 8 is assigned to r.

The final statement within the function: return r; Ends function addition, and returns the control back to the point where the function was called; in this case: to function main. At this precise moment, the program resumes its course on main returning exactly at the same point at which it was interrupted by the call to addition. But additionally, because addition has a return type, the call is evaluated as having a value, and this value is the value specified in the return statement that ended addition: in this particular case, the value of the local variable r, which at the moment of the return statement had a value of 8. Therefore, the call to addition is an expression with the value returned by the function, and in this case, that value, 8, is assigned to z. It is as if the entire function call (addition(5,3)) was replaced by the value it returns (i.e., 8). Then main simply prints this value by calling: cout << "The result is " << z;

A function can actually be called multiple times within a program, and its argument is naturally not limited just to literals: 1 // function example 2 #include <iostream> 3 using namespace std; 4 int subtraction (int a, int b) { 5 int r; 6 r=a-b; 7 return r; 8 } 9 int main () { 10 int x=5, y=3, z; 11 z = subtraction (7,2); 12 cout << "The first result is " << z << '\n'; 13 cout << "The second result is " << 14 subtraction (7,2) << '\n'; 15 cout << "The third result is " << subtraction 16 (x,y) << '\n'; 17 z = 4 + subtraction (x,y); 18 cout << "The fourth result is " << z << '\n'; 19 } 20 21 The first result is 5 The second result is 5 The third result is 2 The fourth result is 6

Math functions Header <cmath> declares a set of functions to compute common mathematical operations and transformations: Trigonometric functions Exponential and logarithmic functions Power functions: pow(base, power) - > pow(3, 2) = 3 ^ 2 = 9 sqrt(value) - > sqrt(16) = 4 Rounding and remainder functions: ceil(value) - > round up value - > ceil(2.3) = 3 floor(value) - > round down value - > floor(2.8) = 2 round(value) - > round to nearest - > round(2.3) = 2 Minimum, maximum, difference functions Other functions: abs(value) -> abs(-4) = 4

rand() function (included in <cstdlib> library) Returns a pseudo- random integral number in the range between 0 and RAND_MAX. RAND_MAX - this value is library- dependent, but is guaranteed to be at least 32767 on any standard library implementation. A typical way to generate trivial pseudo- random numbers in a determined range using rand is to use the modulo of the returned value by the range span and add the initial value of the range: rand() % v2 + v1 v1 is the starting point of the range, including (by default is 0) v2 is how many numbers should be in the range Examples: v1 = rand() % 100; // v1 in the range 0 to 99 v2 = rand() % 100 + 1; // v2 in the range 1 to 100 v3 = rand() % 30 + 1985; // v3 in the range 1985-2014