Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)

Slides:



Advertisements
Similar presentations
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Advertisements

True or false A variable of type char can hold the value 301. ( F )
Computer Science 1620 Loops.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Chapter 5 Functions. Function Subprograms (Functions) A function is a set of instructions that can be used again and again, each time on a possibly different.
Computer Science 1620 Function Scope & Global Variables.
Computer Science 1620 Programming & Problem Solving.
Computer Science 1620 Arrays. Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Program design: 1. read in the.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
The If/Else Statement, Boolean Flags, and Menus Page 180
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
Computer Science 1620 Selection Structures. Write a program that accepts the speed of a vehicle, and determines whether that person is speeding assume.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Functions A function is a snippet of code that performs a specific task or tasks. You use a multitude of functions daily when you do such things as store.
Chapter 6: Functions.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
1 Chapter 9 Scope, Lifetime, and More on Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
C++ Tutorial Hany Samuel and Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2006 by Douglas.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Seventh Edition.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Section 4 - Functions. All of the programs that we have studied so far have consisted of a single function, main(). However, having more than one function.
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig02_07.cpp (1 of 2) 1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
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.
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
CHAPTER 6 USER-DEFINED FUNCTIONS Made By- Kartik Belwal.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Chapter 6 Functions. Topics Basics Basics Simplest functions Simplest functions Functions receiving data from a caller Functions receiving data from a.
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Pass-by-Value - default passing mechanism except.
Functions and Libraries. The idea of a function Functions in programming A function is a block of code that has been given a name. To invoke that code.
Chapter 1.2 Introduction to C++ Programming
User-Written Functions
Chapter 1.2 Introduction to C++ Programming
Variables A piece of memory set aside to store data
Introduction to C++ October 2, 2017.
User-defined Functions
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Functions.
Chapter 6: User-Defined Functions I
COMS 261 Computer Science I
Instructor: Hidayah Elias Course: COMPUTER PROGRAMMING (BFC 2042)
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

Computer Science 1620 Functions

Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2) x … x 1 3! = 3 x 2 x 1 = 6 5! = 5 x 4 x 3 x 2 x 1= 120 0! = 1

Write a program that takes a number n from the user, and calculates the factorial of that number.

#include using namespace std; int main() { int n; cout << "Please enter a number:"; cin >> n; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } cout << fixed << setprecision(0); cout << n << "! = " << factn << endl; return 0; } Write a program that takes a number from the user, and calculates the factorial of that number.

#include using namespace std; int main() { int n; cout << "Please enter a number:"; cin >> n; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } cout << fixed << setprecision(0); cout << n << "! = " << factn << endl; return 0; } Write a program that takes a number from the user, and calculates the factorial of that number.

#include using namespace std; int main() { int n; cout << "Please enter a number:"; cin >> n; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } cout << fixed << setprecision(0); cout << n << "! = " << factn << endl; return 0; } Write a program that takes a number from the user, and calculates the factorial of that number. We'll use a double to store the factorial, since factorials are typically very big numbers.

#include using namespace std; int main() { int n; cout << "Please enter a number:"; cin >> n; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } cout << fixed << setprecision(0); cout << n << "! = " << factn << endl; return 0; } Write a program that takes a number from the user, and calculates the factorial of that number.

#include using namespace std; int main() { int n; cout << "Please enter a number:"; cin >> n; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } cout << fixed << setprecision(0); cout << n << "! = " << factn << endl; return 0; } Write a program that takes a number from the user, and calculates the factorial of that number.

#include using namespace std; int main() { int n; cout << "Please enter a number:"; cin >> n; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } cout << fixed << setprecision(0); cout << n << "! = " << factn << endl; return 0; } Write a program that takes a number from the user, and calculates the factorial of that number.

#include using namespace std; int main() { int n; cout << "Please enter a number:"; cin >> n; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } cout << fixed << setprecision(0); cout << n << "! = " << factn << endl; return 0; } Write a program that takes a number from the user, and calculates the factorial of that number.

Example: Given a set of n objects, the number of combinations of those objects taken r at a time can be calculated as: ie. Given a class of 112 students, the number of possible groups of 5 students is:

Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers, and displays the results.

#include using namespace std; int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= (n-r); i++) { factnr *= i; } double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers, and displays the results.

#include using namespace std; int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= (n-r); i++) { factnr *= i; } double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers, and displays the results.

#include using namespace std; int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= (n-r); i++) { factnr *= i; } double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers, and displays the results.

#include using namespace std; int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= (n-r); i++) { factnr *= i; } double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers, and displays the results. Here's where we need to calculate C n r

Problem Breakdown: double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double result = factn / (factr * factnr); Calculate n! Calculate r! Calculate (n-r)! Combine results into C n r

Problem Breakdown: double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double result = factn / (factr * factnr); Calculate n! Calculate r! Calculate (n-r)! Combine results into C n r

Problem Breakdown: double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double result = factn / (factr * factnr); Calculate n! Calculate r! Calculate (n-r)! Combine results into C n r

Problem Breakdown: double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= n-r; i++) { factnr *= i; } double result = factn / (factr * factnr); Calculate n! Calculate r! Calculate (n-r)! Combine results into C n r

Problem Breakdown: double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= n-r; i++) { factnr *= i; } double result = factn / (factr * factnr); Calculate n! Calculate r! Calculate (n-r)! Combine results into C n r

#include using namespace std; int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= n-r; i++) { factnr *= i; } double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers.

Does this solve our problem? Yes Any problems with previous code? duplication!!! the calculation for factorial is the same, but the code to calculate is repeated 3 times the only change is the stopping condition value, and the variable that we set.

Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers. double = 1.0; for (int i = 1; i ; i++) { factn *= i; } #include using namespace std; int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= n-r; i++) { factnr *= i; } double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; }

Imagine a "factorial machine" one input, for a number n one output, for the computed number n! factorial

Imagine a "factorial machine" one input, for a number n one output, for the computed number n! 1 0 factorial

Imagine a "factorial machine" one input, for a number n one output, for the computed number n! x result double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } questions where does this code go? how do we set the input (x)? how do we get the output (result)?

Function a subprogram a piece of code that can be re-used called from other places in the program example: code to write out my name and location cout << ”Robert" << endl; cout << "Lethbridge" << endl;

Function written outside of main every function has a: return type name parameter list you call a function by typing its name, followed by args #include using namespace std; void whoAmI() { cout << ”Robert" << endl; cout << "Lethbridge" << endl; } int main() { whoAmI(); return 0; }

#include using namespace std; void whoAmI() { cout << ”Robert" << endl; cout << "Lethbridge" << endl; } int main() { whoAmI(); return 0; } Robert Lethbridge Robert Lethbridge Output: Start Here

#include using namespace std; void whoAmI() { cout << ”Robert" << endl; cout << "Lethbridge" << endl; } int main() { whoAmI(); return 0; } Robert Lethbridge Robert Lethbridge Output: At this point, we move to the first line in the function whoAmI

#include using namespace std; void whoAmI() { cout << "Robert" << endl; cout << "Lethbridge" << endl; } int main() { whoAmI(); return 0; } Robert Lethbridge Robert Lethbridge Output: Perform output

#include using namespace std; void whoAmI() { cout << "Robert" << endl; cout << "Lethbridge" << endl; } int main() { whoAmI(); return 0; } Robert Lethbridge Robert Lethbridge Output: Perform output

#include using namespace std; void whoAmI() { cout << "Robert" << endl; cout << "Lethbridge" << endl; } int main() { whoAmI(); return 0; } Robert Lethbridge Robert Lethbridge Output: Function ended, start again right after function call

#include using namespace std; void whoAmI() { cout << "Robert" << endl; cout << "Lethbridge" << endl; } int main() { whoAmI(); return 0; } Robert Lethbridge Robert Lethbridge Output: At this point, we move to the first line in the function whoAmI

#include using namespace std; void whoAmI() { cout << "Robert" << endl; cout << "Lethbridge" << endl; } int main() { whoAmI(); return 0; } Robert Lethbridge Robert Lethbridge Output: Perform output

#include using namespace std; void whoAmI() { cout << "Robert" << endl; cout << "Lethbridge" << endl; } int main() { whoAmI(); return 0; } Robert Lethbridge Robert Lethbridge Output: Perform output

#include using namespace std; void whoAmI() { cout << "Robert" << endl; cout << "Lethbridge" << endl; } int main() { whoAmI(); return 0; } Robert Lethbridge Robert Lethbridge Output: Function ended, start again right after function call

#include using namespace std; void whoAmI() { cout << "Robert" << endl; cout << "Lethbridge" << endl; } int main() { whoAmI(); return 0; } Robert Lethbridge Robert Lethbridge... Output: At this point, program terminates

Example: Write a program with a function called factorial that computes the factorial of 5 and writes the value to output. Call that function from the main function.

#include using namespace std; void factorial() { double result = 1.0; for (int i = 1; i <= 5; i++) { result *= i; } cout << fixed << setprecision(0); cout << result; } int main() { cout << "The factorial of 5 is "; factorial(); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of 5 and writes the value to output. Call that function from the main function.

#include using namespace std; void factorial() { double result = 1.0; for (int i = 1; i <= 5; i++) { result *= i; } cout << fixed << setprecision(0); cout << result; } int main() { cout << "The factorial of 5 is "; factorial(); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of 5 and writes the value to output. Call that function from the main function.

#include using namespace std; void factorial() { double result = 1.0; for (int i = 1; i <= 5; i++) { result *= i; } cout << fixed << setprecision(0); cout << result; } int main() { cout << "The factorial of 5 is "; factorial(); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of 5 and writes the value to output. Call that function from the main function.

#include using namespace std; void factorial() { double result = 1.0; for (int i = 1; i <= 5; i++) { result *= i; } cout << fixed << setprecision(0); cout << result; } int main() { cout << "The factorial of 5 is "; factorial(); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of 5 and writes the value to output. Call that function from the main function.

#include using namespace std; void factorial() { double result = 1.0; for (int i = 1; i <= 5; i++) { result *= i; } cout << fixed << setprecision(0); cout << result; } int main() { cout << "The factorial of 5 is "; factorial(); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of 5 and writes the value to output. Call that function from the main function.

#include using namespace std; void factorial() { double result = 1.0; for (int i = 1; i <= 5; i++) { result *= i; } cout << fixed << setprecision(0); cout << result; } int main() { cout << "The factorial of 5 is "; factorial(); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of 5 and writes the value to output. Call that function from the main function.

Imagine a "factorial machine" one input, for a number n one output, for the computed number n! x result double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } questions where does this code go? how do we set the input (x)? how do we get the output (result)?

Function Input sending data to the function accomplished through a list of parameters a list of variables contained in parameter list we set these variables in the call using arguments a list of expressions

Function Inputs declared as parameters function can have 0 or more parameters specify the type and the variable name Function arguments the value of each argument is assigned to corresponding parameter variable void square(int x) { cout << x*x; } int main() { cout "2 x 2 = "; square(2); cout "(2+1) x (2+1) = "; square(2+1); return 0; }

#include using namespace std; void square(int x) { cout << x*x; } int main() { cout "2 x 2 = "; square(2); cout "\n(2+1) * (2+1) = "; square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Start Here

#include using namespace std; void square(int x) { cout << x*x; } int main() { cout "2 x 2 = "; square(2); cout "\n(2+1) * (2+1) = "; square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Perform Output

#include using namespace std; void square(int x) { cout << x*x; } int main() { cout "2 x 2 = "; square(2); cout "\n(2+1) * (2+1) = "; square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: At this point, we move to the first line in the function square The parameter x gets the value of the expression in the call, which is 2

#include using namespace std; void square(int x) { cout << x*x; } int main() { cout "2 x 2 = "; square(2); cout "\n(2+1) * (2+1) = "; square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Perform Output. Note that x has value 2.

#include using namespace std; void square(int x) { cout << x*x; } int main() { cout "2 x 2 = "; square(2); cout "\n(2+1) * (2+1) = "; square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Function ended, start right after function call.

#include using namespace std; void square(int x) { cout << x*x; } int main() { cout "2 x 2 = "; square(2); cout "\n(2+1) * (2+1) = "; square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Perform Output

#include using namespace std; void square(int x) { cout << x*x; } int main() { cout "2 x 2 = "; square(2); cout "\n(2+1) * (2+1) = "; square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: At this point, we move to the first line in the function square The parameter x gets the value of the expression in the call, which is 2+1 = 3

#include using namespace std; void square(int x) { cout << x*x; } int main() { cout "2 x 2 = "; square(2); cout "\n(2+1) * (2+1) = "; square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Perform Output. Note that x has value 3.

#include using namespace std; void square(int x) { cout << x*x; } int main() { cout "2 x 2 = "; square(2); cout "\n(2+1) * (2+1) = "; square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Function ended, start right after function call.

#include using namespace std; void square(int x) { cout << x*x; } int main() { cout "2 x 2 = "; square(2); cout "\n(2+1) * (2+1) = "; square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Program terminates.

Example: Write a program with a function called factorial that computes the factorial of x (x is an inputted number) and writes the value to output. Call that function from the main function with the value 5.

#include using namespace std; void factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } cout << fixed << setprecision(0); cout << result; } int main() { cout << "The factorial of 5 is "; factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and writes the value to output. Call that function from the main function with the value 5.

#include using namespace std; void factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } cout << fixed << setprecision(0); cout << result; } int main() { cout << "The factorial of 5 is "; factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and writes the value to output. Call that function from the main function with the value 5.

#include using namespace std; void factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } cout << fixed << setprecision(0); cout << result; } int main() { cout << "The factorial of 5 is "; factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and writes the value to output. Call that function from the main function with the value 5.

#include using namespace std; void factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } cout << fixed << setprecision(0); cout << result; } int main() { cout << "The factorial of 5 is "; factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and writes the value to output. Call that function from the main function with the value 5.

#include using namespace std; void factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } cout << fixed << setprecision(0); cout << result; } int main() { cout << "The factorial of 5 is "; factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and writes the value to output. Call that function from the main function with the value 5.

#include using namespace std; void factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } cout << fixed << setprecision(0); cout << result; } int main() { cout << "The factorial of 5 is "; factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and writes the value to output. Call that function from the main function with the value 5.

#include using namespace std; void factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } cout << fixed << setprecision(0); cout << result; } int main() { cout << "The factorial of 5 is "; factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and writes the value to output. Call that function from the main function with the value 5.

Imagine a "factorial machine" one input, for a number n one output, for the computed number n! x result double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } questions where does this code go? how do we set the input (x)? how do we get the output (result)?

Function Output receiving data from a function accomplished through a return statement recall that an expression in C++ has a value 2; // the value of this expression is ; // the value of this expression is 5 2 > 3; // the value of this expression is false (0) i = 2; // the value of this expression is 2 a function call is an expression with a value square(2); square(2+1); what is the value of these expressions?

Function Output given by the return statement Function call the value of the function call is the value that is returned the type of the value returned by a function call is given by its return type int square(int x) { int result = x * x; return result; } int main() { cout "2 x 2 = "; cout << square(2); cout "(2+1) x (2+1) = "; cout << square(2+1); return 0; }

#include using namespace std; int square(int x) { int result = x * x; return result; } int main() { cout "2 x 2 = "; cout << square(2); cout "(2+1) x (2+1) = "; cout << square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Start here

#include using namespace std; int square(int x) { int result = x * x; return result; } int main() { cout "2 x 2 = "; cout << square(2); cout "(2+1) x (2+1) = "; cout << square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Perform Output

#include using namespace std; int square(int x) { int result = x * x; return result; } int main() { cout "2 x 2 = "; cout << square(2); cout "(2+1) x (2+1) = "; cout << square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: At this point, we move to the first line in the function square The parameter x gets the value of the expression in the call, which is 2

#include using namespace std; int square(int x) { int result = x * x; return result; } int main() { cout "2 x 2 = "; cout << square(2); cout "(2+1) x (2+1) = "; cout << square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Do calculation. Note that x has value 2.

#include using namespace std; int square(int x) { int result = x * x; return result; } int main() { cout "2 x 2 = "; cout << square(2); cout "(2+1) x (2+1) = "; cout << square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Returns value stored in result The value of the expression square(2) is the return value (4).

#include using namespace std; int square(int x) { int result = x * x; return result; } int main() { cout "2 x 2 = "; cout << square(2); cout "(2+1) x (2+1) = "; cout << square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Perform output.

#include using namespace std; int square(int x) { int result = x * x; return result; } int main() { cout "2 x 2 = "; cout << square(2); cout "(2+1) x (2+1) = "; cout << square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Perform output.

#include using namespace std; int square(int x) { int result = x * x; return result; } int main() { cout "2 x 2 = "; cout << square(2); cout "(2+1) x (2+1) = "; cout << square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: At this point, we move to the first line in the function square The parameter x gets the value of the expression in the call, which is (2+1) = 3

#include using namespace std; int square(int x) { int result = x * x; return result; } int main() { cout "2 x 2 = "; cout << square(2); cout "(2+1) x (2+1) = "; cout << square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Do calculation. Note that x has value 3.

#include using namespace std; int square(int x) { int result = x * x; return result; } int main() { cout "2 x 2 = "; cout << square(2); cout "(2+1) x (2+1) = "; cout << square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Returns value stored in result The value of the expression square(3) is the return value (9).

#include using namespace std; int square(int x) { int result = x * x; return result; } int main() { cout "2 x 2 = "; cout << square(2); cout "(2+1) x (2+1) = "; cout << square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Perform Output:

#include using namespace std; int square(int x) { int result = x * x; return result; } int main() { cout "2 x 2 = "; cout << square(2); cout "(2+1) x (2+1) = "; cout << square(2+1); return 0; } 2 x 2 = 4 (2+1) x (2+1) = 9 Output: Terminate Program!

Example: Write a program with a function called factorial that computes the factorial of x (x is an inputted number) and returns that value. Call that function from the main function with the value 5.

#include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } return result; } int main() { cout << fixed << setprecision(0); cout << "The factorial of 5 is "; cout << factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and returns that value. Call that function from the main function with the value 5.

#include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } return result; } int main() { cout << fixed << setprecision(0); cout << "The factorial of 5 is "; cout << factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and returns that value. Call that function from the main function with the value 5.

#include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } return result; } int main() { cout << fixed << setprecision(0); cout << "The factorial of 5 is "; cout << factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and returns that value. Call that function from the main function with the value 5.

#include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } return result; } int main() { cout << fixed << setprecision(0); cout << "The factorial of 5 is "; cout << factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and returns that value. Call that function from the main function with the value 5.

#include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } return result; } int main() { cout << fixed << setprecision(0); cout << "The factorial of 5 is "; cout << factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and returns that value. Call that function from the main function with the value 5.

#include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } return result; } int main() { cout << fixed << setprecision(0); cout << "The factorial of 5 is "; cout << factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and returns that value. Call that function from the main function with the value 5.

#include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } return result; } int main() { cout << fixed << setprecision(0); cout << "The factorial of 5 is "; cout << factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and returns that value. Call that function from the main function with the value 5.

#include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } return result; } int main() { cout << fixed << setprecision(0); cout << "The factorial of 5 is "; cout << factorial(5); cout << endl; return 0; } Write a program with a function called factorial that computes the factorial of x and returns that value. Call that function from the main function with the value 5.

Imagine a "factorial machine" one input, for a number n one output, for the computed number n! x result double result = 1.0; for (int i = 1; i <= x; i++) { result *= i; } cout << result; questions where does this code go? how do we set the input (x)? how do we get the output (result)?

Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers, and displays the results.

#include using namespace std; int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= n-r; i++) { factnr *= i; } double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers. Rewrite this program using the factorial function.

#include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= n-r; i++) { factnr *= i; } double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers.

#include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= n-r; i++) { factnr *= i; } double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers.

#include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double factn = factorial(n); double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= n-r; i++) { factnr *= i; } double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers.

#include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double factn = factorial(n); double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= n-r; i++) { factnr *= i; } double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers.

#include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double factn = factorial(n); double factr = factorial(r); double factnr = 1.0; for (int i = 1; i <= n-r; i++) { factnr *= i; } double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers.

#include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double factn = factorial(n); double factr = factorial(r); double factnr = 1.0; for (int i = 1; i <= n-r; i++) { factnr *= i; } double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers.

#include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double factn = factorial(n); double factr = factorial(r); double factnr = factorial(n – r); double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers.

#include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; double result = factorial(n) / (factorial(r) * factorial(n – r)); cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << result << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers.

#include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int main() { int n, r; cout << "Please enter n and r:"; cin >> n >> r; cout << fixed << setprecision(0); cout << n << " choose " << r << " = " << factorial(n) / (factorial(r) * factorial(n – r)) << endl; return 0; } Write a program that takes two integers n and r from the user, and calculates C n r for those two numbers.

The anatomy of a function: double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } function header (declaration) function body (definition) return type function name parameter list

Functions with no return value sometimes, we do not need a value back from our function we indicate this with the void return type return keyword in void function simply exits function void square(int x) { int result = x * x; cout << result; } void inverse(int x) { if (x == 0) return; double result = 1.0 / x; cout << result; }

Functions as expressions the value of a function call is its return value return value can be used in other expressions int square(int x) { int result = x * x; return result; } int main() { cout << square(2);// outputs 4 cout << square(2) + square(3);// outputs 13 cout << square(square(2));// outputs 16 return 0; }

Functions with multiple inputs functions can have as many parameters as you like first argument aligns with first parameter, second with second, etc … int sum(int x, int y) { return x + y; } int main() { cout << sum(2, 3);// outputs 5 return 0; }

More than one return statement a function may have more than one return statement function terminates as soon as return statement is reached int max(int x, int y) { if (x > y) return x; return y; } int main() { cout << max(2, 3) << endl; // outputs 3 cout << max(3, 2) << endl; // outputs 3 return 0; }

Where have we seen functions?

The main function: a function called by operating system can have parameters (more on this later) has a return value 0 – program terminated normally nonzero – program terminated abnormally int main() { return 0; }

Functions can call other functions Example: write a program that takes an integer n, and outputs a table of all values C n r for all values 0 <= r <= n

Without functions: #include using namespace std; int main() { int n; cout << "Please enter n:"; cin >> n; cout << " r n choose r" << endl; cout << " " << endl; for (int r = 0; r <= n; r++) { double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= n-r; i++) { factnr *= i; } double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << setw(3) << r << setw(15) << result << endl; } return 0; }

With function factorial: #include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int main() { int n; cout << "Please enter n:"; cin >> n; cout << " r n choose r" << endl; cout << " " << endl; for (int r = 0; r <= n; r++) { double factn = factorial(n); double factr = factorial(r); double factnr = factorial(n-r); double result = factn / (factr * factnr); cout << fixed << setprecision(0); cout << setw(3) << r << setw(15) << result << endl; } return 0; }

With function factorial: #include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int main() { int n; cout << "Please enter n:"; cin >> n; cout << " r n choose r" << endl; cout << " " << endl; for (int r = 0; r <= n; r++) { double result = factorial(n) / (factorial(r) * factorial(n-r)); cout << fixed << setprecision(0); cout << setw(3) << r << setw(15) << result << endl; } return 0; }

With factorial, looks much better what is a good candidate for a function in this code? calculating n choose r write a function called choose that takes two values n and r, and returns n choose r inputs: n (int), r(int) output: an integer

write a function called choose that takes two values n and r, and calculates and returns n choose r inputs: n (int), r(int) output: an integer int choose(int n, int r) { double result = factorial(n) / (factorial(r) * factorial(n-r)); return static_cast (result); }

write a function called choose that takes two values n and r, and calculates and returns n choose r inputs: n (int), r(int) output: an integer int choose(int n, int r) { double result = factorial(n) / (factorial(r) * factorial(n-r)); return static_cast (result); }

write a function called choose that takes two values n and r, and calculates and returns n choose r inputs: n (int), r(int) output: an integer int choose(int n, int r) { double result = factorial(n) / (factorial(r) * factorial(n-r)); return static_cast (result); }

write a function called choose that takes two values n and r, and calculates and returns n choose r inputs: n (int), r(int) output: an integer int choose(int n, int r) { double result = factorial(n) / (factorial(r) * factorial(n-r)); return static_cast (result); }

write a function called choose that takes two values n and r, and calculates and returns n choose r inputs: n (int), r(int) output: an integer int choose(int n, int r) { double factn = 1.0; for (int i = 1; i <= n; i++) { factn *= i; } double factr = 1.0; for (int i = 1; i <= r; i++) { factr *= i; } double factnr = 1.0; for (int i = 1; i <= n-r; i++) { factnr *= i; } double result = factn / (factr * factnr); double result }

write a function called choose that takes two values n and r, and calculates and returns n choose r inputs: n (int), r(int) output: an integer note: this assumes that we have the factorial function defined in our program!!! int choose(int n, int r) { double result = factorial(n) / (factorial(r) * factorial(n-r)); return static_cast (result); }

write a function called choose that takes two values n and r, and calculates and returns n choose r inputs: n (int), r(int) output: an integer int choose(int n, int r) { double result = factorial(n) / (factorial(r) * factorial(n-r)); return static_cast (result); }

write a function called choose that takes two values n and r, and calculates and returns n choose r inputs: n (int), r(int) output: an integer int choose(int n, int r) { double result = factorial(n) / (factorial(r) * factorial(n-r)); return static_cast (result); }

write a function called choose that takes two values n and r, and calculates and returns n choose r inputs: n (int), r(int) output: an integer int choose(int n, int r) { double result = factorial(n) / (factorial(r) * factorial(n-r)); return static_cast (result); }

With function factorial and choose: #include using namespace std; double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int choose(int n, int r) { double result = factorial(n) / (factorial(r) * factorial(n-r)); return static_cast (result); } int main() { int n; cout << "Please enter n:"; cin >> n; cout << " r n choose r" << endl; cout << " " << endl; for (int r = 0; r <= n; r++) { cout << setw(3) << r << setw(15) << choose(n, r) << endl; } return 0; }

Function Placement 2 key ideas 1. The engine of a C++ program is the main method allocates tasks to other functions program functionality can usually be inferred from a well written main method, as opposed to a poorly written onewell written main methoda poorly written one ie. suppose someone asks: "What does your program do?" "program outputs n choose r for all 0 <= r <= n" 2. the user is typically interested in what function does, not how it does it ex. square root

Main function code is more interesting to user than helper functions thus, it is typically placed near the top of your file, before other functions however, if we place a function after the main function, we can't call it from main C++ must know the parameter types and return type of a function before it can be called, for type checking

With function factorial and choose: #include using namespace std; int main() { int n; cout << "Please enter n:"; cin >> n; cout << " r n choose r" << endl; cout << " " << endl; for (int r = 0; r <= n; r++) { cout << setw(3) << r << setw(15) << choose(n, r) << endl; } return 0; } double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int choose(int n, int r) { double result = factorial(n) / (factorial(r) * factorial(n-r)); return static_cast (result); }

With function factorial and choose: #include using namespace std; int main() { int n; cout << "Please enter n:"; cin >> n; cout << " r n choose r" << endl; cout << " " << endl; for (int r = 0; r <= n; r++) { cout << setw(3) << r << setw(15) << choose(n, r) << endl; } return 0; } double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int choose(int n, int r) { double result = factorial(n) / (factorial(r) * factorial(n-r)); return static_cast (result); } This will not compile.

Function Prototype a function declaration (header) without a definition (body) tells the compiler what the parameters and return type of the function will be, when it is defined allows C++ to do type checking prior to function definition

Function Prototype function header, without body appended with semicolon { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } function header (declaration) function body (definition) double factorial(int x);

With function factorial and choose: #include using namespace std; double factorial(int x); int choose(int n, int r); int main() { int n; cout << "Please enter n:"; cin >> n; cout << " r n choose r" << endl; cout << " " << endl; for (int r = 0; r <= n; r++) { cout << setw(3) << r << setw(15) << choose(n, r) << endl; } return 0; } double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int choose(int n, int r) { double result = factorial(n) / (factorial(r) * factorial(n-r)); return static_cast (result); } This will compile, since main can check types.

Function Prototype since we are only type checking, parameter names are of no use can also be written as (more common): double factorial(int x) double factorial(int)

With function factorial and choose: #include using namespace std; double factorial(int); int choose(int, int); int main() { int n; cout << "Please enter n:"; cin >> n; cout << " r n choose r" << endl; cout << " " << endl; for (int r = 0; r <= n; r++) { cout << setw(3) << r << setw(15) << choose(n, r) << endl; } return 0; } double factorial(int x) { double result = 1.0; for (int i = 1; i <= x; i++) result *= i; return result; } int choose(int n, int r) { double result = factorial(n) / (factorial(r) * factorial(n-r)); return static_cast (result); }

A good (and common) format for your.cc files to follow: preprocessor directives function prototypes int main() { } function definitions

Functions - Examples Write a function called odd that takes an integer number, and returns true if that number is odd, or false if the number is even.

bool odd(int x) { if (x % 2 == 1) { return true; } return false; } Write a function called odd that takes an integer number, and returns true if that number is odd, or false if the number is even.

bool odd(int x) { if (x % 2 == 1) { return true; } return false; } Write a function called odd that takes an integer number, and returns true if that number is odd, or false if the number is even.

bool odd(int x) { if (x % 2 == 1) { return true; } return false; } Write a function called odd that takes an integer number, and returns true if that number is odd, or false if the number is even.

bool odd(int x) { if (x % 2 == 1) { return true; } return false; } Write a function called odd that takes an integer number, and returns true if that number is odd, or false if the number is even.

bool odd(int x) { if (x % 2 == 1) { return true; } return false; } Write a function called odd that takes an integer number, and returns true if that number is odd, or false if the number is even.

bool odd(int x) { return (x % 2 == 1); } Write a function called odd that takes an integer number, and returns true if that number is odd, or false if the number is even.

Functions - Examples Write a function called power that returns a real value b raised to a positive power e.

double power(double b, int e) { double result = 1.0; for (int i = 1; i <= e; i++) result *= b; return result; } Write a function called power that returns a real value b raised to a positive power e.

double power(double b, int e) { double result = 1.0; for (int i = 1; i <= e; i++) result *= b; return result; } Write a function called power that returns a real value b raised to a positive power e.

double power(double b, int e) { double result = 1.0; for (int i = 1; i <= e; i++) result *= b; return result; } Write a function called power that returns a real value b raised to a positive power e.

double power(double b, int e) { double result = 1.0; for (int i = 1; i <= e; i++) result *= b; return result; } Write a function called power that returns a real value b raised to a positive power e.