Lab Instructor : Jean Lai

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

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

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

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

Chapter 7 - Notes User-Defined Functions II

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

Chapter 3 - Functions

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

(created by professor Marina Tanasyuk) FUNCTIONS

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

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Functions. Lecture 6 COP 3014 Spring February 11, 2018

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

Local and Global Variables

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

CS 31 Discussion 1A, Week 4. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

Functions. Introduction :

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

Partha Sarathi Mandal

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

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

Computer Programming

Building on the foundation. Now that we know a little about cout cin math operators boolean operators making decisions using if statements

Functions and Recursion

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

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

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

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

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

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

C++ Programming Lecture 11 Functions Part I

Lecture 04 FUNCTIONS AND ARRAYS

Chapter Four: Loops II

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

Chapter 3 Function Basics

Chapter 10 - Notes Applications of Arrays

Week 4 EECS 183 MAXIM ALEKSA. maximal.io

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

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

Programming Language. Functions. Eng. Anis Nazer First Semester

Problem Solving: Storyboards for User Interaction

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Why Is Repetition Needed?

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

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

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

Lab # 02. Basic Elements of C++ _ Part1

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

CS2141 Software Development using C/C++ C++ Basics

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

Introduction to Programming

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

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

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.

Other operators. Some times a simple comparison is not enough to determine if our criteria has been met.

Chapter 3 - Functions

A SHORT COURSE ON C++

6.096 Introduction to C++

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

CSCE Practice Midterm. Data Types

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Engineering Problem Solving with C++, Etter/Ingber

C++ Quick Guide. Advertisements

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

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

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

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

Array Elements as Function Parameters

Exceptions, Case Study-Exception handling in C++.

CMSC 202 Midterm Exam 1 Fall 2015

Functions. Function Prototypes. Function prototype is needed if the function call comes before the function definition in the program.

Lecture 04 FUNCTIONS AND ARRAYS

C Functions. 5.2 Program Modules in C

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

Introduction to C ++

EECS402 Lecture 02. Functions. Function Prototype

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

#include <iostream> using namespace std;

do { statements } while (condition);

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

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

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

CSCE 110 PROGRAMMING FUNDAMENTALS

Object oriented programming

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

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

CSCE 206: Structured Programming in C++

Functions. Systems Programming Concepts

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

Unit 6. Thinking with Functions

CS 103 Lecture 3 Slides

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

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

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

CHAPTER 4 FUNCTIONS. 4.1 Introduction

Transcription:

Lab Instructor : Jean Lai

Group related statements to perform a specific task. Structure the program (No duplicate codes!) Must be declared before used. Can be invoked (called) as any number of times. Can call other functions. main() is also a function!

C++ has many built-in functions that can be used. Must declare (#include) the libraries before using the functions. Used perform specific tasks. Some of the common functions include sqrt(), abs(), rand(), srand(), time() etc.

double sqrt (double x); Returns the square root of x #include <iostream> #include <cmath> /* required library for sqrt */ using namespace std; int main () { double y = sqrt (25); // y is the square root of 25 cout << sqrt (45.9); // print the square root of 45.9 return 0;

int abs (int n); Returns the absolute value of n. int rand (); Returns a pseudo-random integer in the range between 0 and RAND_MAX. void srand (unsigned int seed); Initialize the pseudo-random number generator using a specific integer as the seed number.

#include <iostream> #include <cstdlib> /* required library for abs, rand, srand */ #include <time> /* required library for time */ using namespace std; int main () { int y = abs(-25); // y is the absolute value of -25 cout << abs(49); // print the absolute value of 49 srand (time (NULL)); // using current time as the seed number int v1 = rand() % 100; // v1 in the range 0 to 99 int v2 = rand() % 100 + 1; // v2 in the range 1 to 100 int v3 = rand() % 30 + 1985; // v3 in the range 1985-2014 return 0;

return_type function_name (argument_list) { //function body //do something return type what kind of value the function returns, can be any of C++ data types: int, double, bool, char, string, etc. or void if the function does not return a value. function_name name of the function similar to variable names. argument_list a list of comma separated arguments (also known as parameters) function body list of statements accomplishing a task.

return_type argument_list function_name int sum (int x, int y) { //returning the sum of x and y int result = x + y; return result; function body NOTE: Use keyword return to exit the function. Must return the value of same return type.

return_type argument_list function_name int main () { //do something return 0; function body

double avg (double x, double y) { return (x + y) / 2; bool isprime (int x) { return true;

To execute the function. Argument types must match argument_list in the function heading. Is an expression. Can be used within a larger expression if the return type is not void.

Function heading: int sum (int x, int y) { Calling the function: int result = sum (8, 17); or cout << The sum is << sum(8, 17);

bool isprime (int x) { if (isprime(7)) cout << 7 is prime. ; void printhello (int x) { int x = 8; printhello(x);

#include <iostream> using namespace std; int sum(int x, int y) { return x + y; int main() { int x, y; cin >> x, y; cout << x << + << y << = << sum(x, y); return 0;

#include <iostream> using namespace std; int sum(int x, int y); int main() { int x, y; cin >> x, y; cout << x << + << y << = << sum(x, y); return 0; int sum(int x, int y) { return x + y;

Extent of visibility of the variable. Exist only inside the block (curly braces { ) where it is declared. Can be either local or global scope.

int sum (int x, int y) { int z = x + y; local variable z, return z; not accessible outside the function! local variable r, accessible to all within its block { for (int r = 1; r <= 5; r++ ) { for (int c = 1; c <= r; c++) { cout << * ; local variable c, only accessible cout << endl; within inner loop!

int main () { int n = 8; for (int r = 1; r <= n; r++ ) { for (int c = 1; c <= n; c++) { cout << * ; cout << endl; return 0; local variable n, visible to all nested within the same block {!

A copy of the original value is passed into the function. Original value is not altered. May use different variable names but must have same data type. Seen as a local variable in the function. Once function exits, the local variable is discarded.

void changen(int n) { n = n + 7; int main () { int n = 8; cout << The original value of n : << n; changen(n); cout << The updated value of n : << n ; The value of n is not changed by the call to changen. return 0;

The original variable is passed into the function. Original value will be altered. May use different variable names but must have same data type. Must attach the ampersand sign & to the parameters in argument list. Once function exits, the changes still remain!

void swap(int& x, int& y) { int temp = x; x = y; y = temp; int main () { int x = 2, y = 5 cout << Original x : << x << y : << y; swap(x, y); cout << Updated x : << x << y : << y; return 0; The value of x and y is changed by the call to swap.

C++ allows ampersand associated either with type name or parameter. void swap(int& x, int& y); is the same as void swap(int &x, int &y);