Functions Jordi Cortadella Department of Computer Science.

Slides:



Advertisements
Similar presentations
Introduction to Programming (in C++) Subprograms: procedures and functions Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science,
Advertisements

Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Computer Science 1620 Loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Writing and Testing Programs Drivers and Stubs Supplement to text.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
Introduction to Programming (in C++) Introduction Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Introduction to Programming (in C++) Data and statements Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Reasoning with invariants Jordi Cortadella Department of Computer Science.
Data types and their representation Jordi Cortadella Department of Computer Science.
Prime numbers Jordi Cortadella Department of Computer Science.
The power of logarithmic computations Jordi Cortadella Department of Computer Science.
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
Recursion Jordi Cortadella Department of Computer Science.
Introduction to Programming (in C++) Algorithms on sequences. Reasoning about loops: Invariants. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept.
Approximate computations Jordi Cortadella Department of Computer Science.
Sequences Jordi Cortadella Department of Computer Science.
Search algorithms for vectors Jordi Cortadella Department of Computer Science.
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
C++ Tutorial Hany Samuel and Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2006 by Douglas.
Greatest Common Divisor Jordi Cortadella Department of Computer Science.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Structures Jordi Cortadella Department of Computer Science.
Sudoku Jordi Cortadella Department of Computer Science.
First steps Jordi Cortadella Department of Computer Science.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Reusing computations Jordi Cortadella Department of Computer Science.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
FUNCTIONS - What Is A Function? - Advantages Function Declaration
Vectors Jordi Cortadella Department of Computer Science.
Parameter passing Jordi Cortadella Department of Computer Science.
Combinatorial problems Jordi Cortadella Department of Computer Science.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Introduction to Computer Programming
Jordi Cortadella, Ricard Gavaldà, Fernando Orejas
While loop statement condition list
Reasoning with invariants
Intro to Programming Week # 6 Repetition Structure Lecture # 10
For loops, nested loops and scopes
Combinatorial problems
CS150 Introduction to Computer Science 1
Chapter 4 Loops Case Studies
Jordi Cortadella Department of Computer Science
Chapter 5 Function Basics
Jordi Cortadella Department of Computer Science
Jordi Cortadella Department of Computer Science
Starting Out with C++: From Control Structures through Objects
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Pass by Reference.
Summary Two basic concepts: variables and assignments Basic types:
Let’s all Repeat Together
CS150 Introduction to Computer Science 1
Search algorithms for vectors
CS1201: Programming Language 2
Life is Full of Alternatives
Jordi Cortadella Department of Computer Science
do/while Selection Structure
Main() { int fact; fact = Factorial(4); } main fact.
Introduction to Functions
Introduction to Algorithms and Programming COMP151
Presentation transcript:

Functions Jordi Cortadella Department of Computer Science

Maximum of two numbers #include using namespace std; // This program reads two numbers and // prints the maximum value of both int main() { int x, y; cin >> x >> y; int m; if (x > y) m = x; else m = y; cout << "The maximum value is " << m << endl; } Introduction to Programming© Dept. CS, UPC2

Defining a function // Returns the max value of a and b int max(int a, int b) { int m; if (a > b) m = a; else m = b; return m; } maxmax int a int b int Introduction to Programming© Dept. CS, UPC3

Defining a function // Returns the max value of a and b int max(int a, int b) { int m; if (a > b) m = a; else m = b; return m; } We can call the function in many ways: s = max(x, y); z = max(n – 1, 3x); Introduction to Programming© Dept. CS, UPC4

Defining a function // Returns the max value of a and b int max(int a, int b) { if (a > b) return a; else return b; } int max(int a, int b) { if (a > b) return a; return b; } Introduction to Programming© Dept. CS, UPC5

Maximum of two numbers (III) // Returns the max value of a and b int max(int a, int b) { if (a > b) return a; else return b; } // This program reads two numbers and // prints the maximum value of both int main() { int x, y; cin >> x >> y; cout << "The maximum value is " << max(x, y) << endl; } Introduction to Programming© Dept. CS, UPC6

Maximum of three numbers // Returns the max value of a and b int max(int a, int b) { if (a > b) return a; else return b; } // This program reads three numbers // and prints their maximum value int main() { int x, y, z; cin >> x >> y >> z; cout << "The maximum value is " << max(x, max(y, z)) << endl; } xmaxmax maxmax x y z Introduction to Programming© Dept. CS, UPC7

Maximum of three numbers // Returns the max value of a and b int max(int a, int b) { if (a > b) return a; else return b; } // This program reads three numbers // and prints their maximum value int main() { int x, y, z; cin >> x >> y >> z; cout << "The maximum value is " << max(x, max(y, z)) << endl; } max(max(x, y), z) xmaxmax maxmax x y z Introduction to Programming© Dept. CS, UPC8

// Returns |x| int absolute(int x) { if (x >= 0) return x; return -x; } Absolute valueMathC++ == != > < >= <= a = b; Assignment if (a == b) … Condition Introduction to Programming© Dept. CS, UPC9

Calculate x y Algorithm: repeated multiplication x  x  x    x y times // Pre: y  0 // Returns x y int power(int x, int y); Introduction to Programming© Dept. CS, UPC10

Calculate x y Algorithm: repeated multiplication x  x  x    x y times yxi p=x i Introduction to Programming© Dept. CS, UPC11

// Pre: y  0 // Returns x y int power(int x, int y) { int p = 1; int i = 0; // Repeat y times while (i < y) { p = px; i = i + 1; // p = x i } return p; } Calculate x yyxi p=x i Introduction to Programming© Dept. CS, UPC12

Factorial Introduction to Programming© Dept. CS, UPC13

Calculate n!i f = i! // Pre: n  0 // Returns n! int factorial(int n); Introduction to Programming© Dept. CS, UPC14

Factorial // Pre: n  0 // Returns n! int factorial(int n) { int f = 1; int i = 0; while (i < n) { i = i + 1; f = fi; // f = i! } return f; } i f = i! Introduction to Programming© Dept. CS, UPC15

Summary Functions are used to group sequences of statements. Why should we use functions? – Re-use of code, modularity and readability When designing a loop, try to find a property that characterizes all iterations, e.g., p=x i (power) f=i! (factorial) Use the property to define the loop components (condition, initialization, body of the loop). Introduction to Programming© Dept. CS, UPC16