Topic 2 Functions Chapter 7 in the shrinkwrap Part 1 Announcements Lab 3 Functions Topic 2 Ch 7 Functions 1 of 16 1 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 2 of 16 Topic 2 Ch 7 Functions Divide Conquer Break up large problems into smaller manageable problems Hard problem Easy subproblem Hard subproblem Easy subproblem Topic 2 Ch 7 Functions Easy subproblem Easy subproblem 3 of 16 2 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 Each of these can be thought of as separate tasks 4 of 16 Topic 2 Ch 7 Functions Divide Conquer Example 2 A divide and conquer block diagram of our problem Calculate the Grade Point Avg INPUT letterGrade CALCULATE gradePointAvg OUTPUT gradePointAvg How can we further break this down 5 of 16 Topic 2 Ch 7 Functions 3 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 These are provided We want to create our own functions Functions should accomplish one task This way we can reuse it Make it general Topic 2 Ch 7 Functions 6 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 Coding is similar just have to separate them out into new functions and call them Topic 2 Ch 7 Functions 7 of 16 4 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 16 5 Function Process For every function we need to 1 Declare the function 2 Define the function 3 Use the function 10 of 16 Topic 2 Ch 7 Functions Using a Function theSquareRoot sqrt num1 Variable Function Name The parens are required Argument 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 of 16 6 How do we define a function Syntax returnType functionName type parameterName statements return value 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 NOTE We have to let the compiler return num1 num2 know what the types are of our arguments incoming data Topic 2 Ch 7 Functions 12 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 Syntax returnType functionName type parameterName Example int AddTwoInts int num1 int num2 Note this is exactly like the first line of our function definition except the Topic 2 Ch 7 Functions 13 of 16 7 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 For now we will do this 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 NEVER use this option 1 14 of 16 Topic 2 Ch 7 Functions Calling our function Now we just need to call our function include iostream using namespace std int main int n1 int n2 int sum Prototype goes here cout Enter the first value to be summed cin n1 cout Enter the second cin n2 NOTE we could have put AddTwoInts here sum AddTwoInts n1 n2 cout nThe sum is sum endl return 0 15 of 16 8 Putting it all together include iostream using namespace std Prototype int AddTwoInts int num1 int num2 Calling Function int main int n1 n2 sum cout Enter the first value cin n1 cout Enter the second cin n2 function call sum AddTwoInts n1 n2 cout nThe sum is sum endl return 0 int AddTwoInts int num1 int num2 return num1 num2 Function Definition Lab 3 Modify this code 16 of 16 Hown1do the values get into thenum2function sum n2 num1 5 4 Output 5 4 9 9 5 4 main passes the values of n1 n2 to AddTwoInt int main int n1 n2 sum n1 5 n2 4 sum AddTwoInts n1 n2 cout n1 n2 sum return 0 5 4 int AddTwoInts int num1 int num2 return num1 num2 9 The function is called the values of n1 n2 are passed into their respective positions num1 num2 In this case the n1 is the first argument will be sent to the first parameter in the function num1 If we had put n2 first then the value in n2 would be sent to num1 The value of num1 num2 is returned the function call gets the 17 …
View Full Document