DOC PREVIEW
Saddleback CS 1B - Topic 2 - Intro to Functions

This preview shows page 1-2-3 out of 10 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1 Topic 2- Functions Chapter 7 in the shrinkwrap Part 1 Announcements  Lab #3 - Functions Topic 2 - Ch 7 - Functions 1 of 162 Divide and Conquer  We break big complex problems into smaller comprehensible problems ● This is how we build big programs  WHY? ● Smaller problems are easier to manage and code ● Smaller code segments are easier to code & test (debug) ● If we can break it into small independent tasks then we can have many programmers each working on their own task Topic 2 - Ch 7 - Functions 2 of 16 Divide & Conquer  Break up large problems into smaller manageable problems Topic 2 - Ch 7 - Functions 3 Hard problem Easy subproblem Easy subproblem Hard subproblem Easy subproblem Easy subproblem of 163 Divide & Conquer Example Calculate a grade point average. Problem statement  We need an interactive program (user will input data) that calculates a students’ GPA given a series of letter grades. Input/Output description ● Input  Letter grades ● Output  Grade point average Algorithm development (set of steps, decomposition outline) 1. Read the letter grade 2. Convert it to a numerical grade 3. Calculate the grade point average 4. Output the grade point average Topic 2 - Ch 7 - Functions 4 Each of these can be thought of as separate tasks! of 16 A divide and conquer block diagram of our problem How can we further break this down? Divide & Conquer Example (2) Topic 2 - Ch 7 - Functions 5 Calculate the Grade Point Avg INPUT letterGrade OUTPUT gradePointAvg CALCULATE gradePointAvg of 164 How do we do this in C++?  Each separate task is called a component, module, subprogram ● Each of these are logical groupings of code  In C++ we call these functions  Each function has its own task ● It is a small program that we can use over and over again by calling it  We have been discussing functions all along ● e.g. toupper, ceil, fabs  Functions should accomplish one task ● This way we can reuse it ● Make it general Topic 2 - Ch 7 - Functions 6 These are provided. We want to create our own functions of 16 Example  Programs often have different tasks ● Input, validate input, get the grade pt value of a letter grade, calculate, gpa, etc..  We can split these up into different functions ● Each function processes its own task ● We call upon it as necessary Topic 2 - Ch 7 - Functions 7 Coding is similar – just have to separate them out into new functions and call them! of 165 The Main function int main ()  Up until now we have used the function main  All C++ programs must have this function  The operating system runs the program until main returns 0 Topic 2 - Ch 7 - Functions 8 of 16 Function basics  All Functions must have a return datatype ● This represents the datatype of the value it is returning ● For main we use int ● Some functions return some value ● Others do not  They still have to have a datatype Example ● we can have a function that calculates the square root of a number.  will return a float ● We can have a function that outputs a header (such as your name, date, class, etc.)  doesn’t return anything but needs a datatype none-the-less  Once we create these functions we can call them over and over again and don’t have to rewrite them every time we want to use them 9 of 166 Function Process For every function we need to.. 1 - Declare the function 2 - Define the function 3 - Use the function Topic 2 - Ch 7 - Functions 10 of 16 Using a Function theSquareRoot = sqrt(num1); Argument  sometimes we need to send data to the function, we use arguments to do this. Argument_List  a list of arguments that follows the function call Variable  if our function returns a value we need a place to store it. NOTE: This function returns a value so we need to either assign that value to a variable or cout it. e.g. cout << sqrt(num1); Topic 2 - Ch 7 - Functions 11 Function Name Argument Variable The parens are required of 167 How do we define a function?  Somewhere in statements you need to have a return statement.  The function terminates when the return statement is executed  The return statement returns the value of the code. Example int AddTwoInts(int num1, int num2) { return num1 + num2; } Topic 2 - Ch 7 - Functions 12 Syntax returnType functionName ( type parameterName...) { statements; return value; } NOTE: We have to let the compiler know what the types are of our arguments (incoming data) of 16 Declaring a function We can declare them Somewhere in our main source file (main.cpp for example)  before int main () We have to tell the compiler about the function We do this using a function prototype Example int AddTwoInts(int num1, int num2); Topic 2 - Ch 7 - Functions 13 Syntax returnType functionName (type parameterName...); Note: this is exactly like the first line of our function definition except the ; of 168 Why use a Prototype? We need the prototype to be first because the compiler reads from top to bottom 1. We can put it in the same file as our int main()  Before the int main() 2. We can put it in a separate header file ◘ We will cover this later 3. We can define the function before the int main() { } (rather then declaring the prototype) and then we won’t need the prototype ◘ This will require that our functions appear in a particular order making our code hard to maintain Topic 2 - Ch 7 - Functions 14 For now we will do this NEVER use this option 1 of 16 Calling our function Now we just need to call our function #include <iostream> using namespace std; int main () { int n1 int n2; int sum; cout << "Enter the first value to be summed: "; cin >> n1; cout << "Enter the second: "; cin >> n2; sum = AddTwoInts(n1, n2); cout << "\nThe sum is: " << sum << endl; return 0; } 15 NOTE: we could have put AddTwoInts here of 16 Prototype goes here9 Putting it all together #include <iostream> using namespace std; int AddTwoInts(int num1, int num2); int main () { int n1, n2, sum; cout << "Enter the first value: "; cin >> n1; cout << "Enter the second: "; cin >> n2; sum = AddTwoInts(n1, n2); cout << "\nThe sum is: " << sum << endl; return 0; } int AddTwoInts(int num1, int num2) { return num1 + num2; } 16 Lab #3  Modify this code of 16 Prototype Calling Function function call Function Definition } How do the values get into the


View Full Document

Saddleback CS 1B - Topic 2 - Intro to Functions

Download Topic 2 - Intro to Functions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Topic 2 - Intro to Functions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Topic 2 - Intro to Functions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?