CMPT 201 Functions.

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1 Created by David Mann, North Idaho College.
Advertisements

User Defined Functions
Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
Modular Programming With Functions
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chapter 5 Functions.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
An Introduction to Programming with C++ Fifth Edition
Chapter 6: User-Defined Functions I
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Chapter 6: User-Defined Functions I
Chapter 4 Procedural Abstraction and Functions That Return a Value.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 4 Procedural Abstraction and Functions That Return a Value.
Iterative Constructs Review l What are named constants? Why are they needed? l What is a block? What is special about declaring a variable inside a block?
CPS120: Introduction to Computer Science Decision Making in Programs.
Today’s Lecture Predefined Functions. Introduction to Functions  Reuse Issue  Building Blocks of Programs  Two types of functions  Predefined  Programmer.
Chapter 4 Procedural Abstraction and Functions That Return a Value.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
Functions. Let’s look at the 2 programs of evaluating: y=2^3+2^5+2^6; #include using namespace std; int main() { int y=0; int partResult=1; for (int i=1;
CPS120: Introduction to Computer Science Lecture 14 Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 4 Procedural Abstraction and Functions That Return a Value.
Copyright © 2015 Pearson Education, Ltd.. All rights reserved. Chapter 4 Procedural Abstraction and Functions That Return a Value.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
L what are predefined functions? l what is? n function name n argument(s) n return value n function call n function invocation n nested function call l.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Copyright © 2015 Pearson Education, Ltd.. All rights reserved. Chapter 4 Procedural Abstraction and Functions That Return a Value.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 4 Functions.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Functions + Overloading + Scope
Chapter 9: Value-Returning Functions
Functions.
Chapter 6: User-Defined Functions I
Procedural Abstraction and Functions That Return a Value
CMPT 238 Data Structures C++ Review
Why they aren't really random
Functions and an Introduction to Recursion
Procedural Abstraction and Functions That Return a Value
Procedural Abstraction and Functions That Return a Value
Predefined Functions Revisited
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Function Basics TBC=15 Dr. Xiao Qin.
Chapter 4 Procedural Abstraction and Functions That Return a Value 1
Iterative Constructs Review
Lesson 5 Functions I A function is a small program which accomplishes a specific task. For example, we invoke (call) the function, sqrt(x), in the library.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
CMPE 180A Data Structures and Algorithms in C++ February 1 Class Meeting Department of Computer Engineering San Jose State University Spring 2018 Instructor:
توابع در C++ قسمت اول اصول كامپيوتر 1.
6 Chapter Functions.
Functions October 23, 2017.
Iterative Constructs Review
Chapter 6: User-Defined Functions I
Chapter 9: Value-Returning Functions
Fundamental Programming
Predefined Functions Revisited
Functions Imran Rashid CTO at ManiWeber Technologies.
CS 144 Advanced C++ Programming January 31 Class Meeting
6 Functions.
Chapter 4 Procedural Abstraction and Functions That Return a Value 1
CPS125.
Presentation transcript:

CMPT 201 Functions

Top Down Design (Divide and Conquer) Break the algorithm into subtasks Subtasks, or functions in C++, make programs Easier to understand Easier to change Easier to write Easier to test Easier to debug Easier for teams to develop

Predefined Functions C++ comes with libraries of predefined functions Example: sqrt function #include <cmath> root = sqrt(9.0); The number, 9, is called the argument returns, or computes, the square root of a number

pow() #include <cmath> cout<<“2.5 to the power 3.0 is “<< pow(2.5, 3.0); The sequence of the two arguments matters!

Random Number Generation Really pseudo-random numbers Ri = (Ri-1 * 7) % 11 1. Seed the random number generator only once #include <cstdlib> #include <ctime> srand(time(0)); 2. The rand() function returns a random integer that is greater than or equal to 0 and less than RAND_MAX (32767);

Random Numbers Use % and + to scale to the number range you want Generate a random number from 0-99 int num = rand() % 100; Generate a random number from 1-6 int die = (rand() % 6) + 1; Generating a random number x where 10<=x<=20?

Revisit: Guess a number Generate a random lucky number between 1 and 10

User-Defined Functions Function declaration Type Function_Name(Type Parameter); int product(int x, int y);

User-Defined Functions Function definition Type Function_Name(Type Parameter) { //code } int product(int x, int y) // function header { int result = x * y; return result; }

The Function Call int main() { int number = product (5, 6); ... } int product(int x, int y) int result = x * y; return result;

Note When a return statement is executed, the function call ends.

Exercise (Multiple returns with if-else) Build our own my_abs() function.

Functions that Return a Boolean Value bool isPrime (int number); //returns true if number is a prime number, false otherwise Nested loops: Consider using function calls (for inner loop) to make the program more readable

Note You can place the whole function definition before the main. Procedural Abstraction If a function is well designed, the programmer can use the function as if it were a black box. char grade (int score) // return a student’s grade based on his/her score

Local Variables vs. Global Variables Are declared in a function Cannot be used from outside the function Have the function as their scope Global variables Available to all functions (including main) Declared outside any function body e.g., Global named constant

Call-by-value int square (int n) { n = n * n; return n; } int main () int number = 10; int result = square(number); cout<<result<<“ “<<number;

Overloaded Functions C++ allows more than one definition for the same function name different numbers of arguments different types of arguments double avg(double n1, double n2) { return ((n1 + n2) / 2); } double avg(double n1, double n2, double n3) { return (( n1 + n2 + n3) / 3); }

Overloaded Functions cout << avg( 10, 20, 30); double avg(double n1, double n2) { return ((n1 + n2) / 2); } double avg(double n1, double n2, double n3) { return (( n1 + n2 + n3) / 3); } Which one are we calling? cout << avg( 10, 20); cout << avg( 10, 20, 30);

To summarize.. Overloaded functions Must have different numbers of formal parameters AND / OR Must have at least one different type of parameter