Presentation is loading. Please wait.

Presentation is loading. Please wait.

Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.

Similar presentations


Presentation on theme: "Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3."— Presentation transcript:

1 Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3

2 Review: Functions Remember the black box metaphor: This function would be defined as: double pow (double x, double y); And this function would be called using something like: double myNumber = pow(5.12, 3.689);

3 Review: Functions in C++ headers We can utilize header files like cmath to perform computations we don't want to write from scratch. log, log10, cos, sin, tan, atan, acos, pow and many more. Read documentation for these functions online: http://www.cplusplus.com/reference/cmath/

4 Review: User Input We can read what the user types in using cin: int age; cout << “How old are you? “; cin >> age; If we want the user to type in a string, we can use getline(): string fullName; cout << “What is your first and last name? “; getline(cin, fullName); Remember: We use getline for strings because cin stops reading when it encounters a space.

5 Making your own functions returnType functionName (paramType param1, paramType param2) { // statements inside your function return results; } int multiplyThree (int num1, int num2, int num3) { return num1 * num2 * num3; } How else could we write this function?

6 Making your own functions pow() is a function that already exists in the math.h header, but what if we had to write it? int pow (int base, int exponent) { int result; //Do some stuff we haven't learned yet... //The code would multiply base by itself //exponent # of times. return result; }

7 Making your own functions What about something other than math? Let's write a function that will print out some blank lines. That could be helpful if we are printing a lot to the screen. void makeSomeSpace () { cout << endl; } How else could we write this function? Note: void is a special return type that means the function doesn't return anything

8 Note: Keywords Some words cannot be used as variable names or function names. That is because they are reserved for use by C++. You cannot name a function “int”, for example.

9 Return Value If you write in your function that you are going to return a specific type, you must return that type. If your function does not return anything, you can say it returns “void” void myFunction(int param1,...) {...}

10 Parameters When you call a function, you have to pass the parameters in the same order that they are specified in the function definition. Additionally, they must match whatever data types are specified in the function definition.

11 Parameters Look at our pow() function again: int pow (int base, int exponent) { int result; //Do some stuff we haven't learned yet... //The code would multiply base by itself //exponent # of times. return result; } We could call the function like this and it would return 8: int twoCubed = pow(2, 3); If we switched the parameters, it would return 9. int twoCubed = pow(3, 2);

12 Scope In functions, the parameters passed into the function can only be seen and used by that function. If you modify a variable inside a function, you cannot see the 'change' back in main() or in another function. There are, as always, exceptions. But we will not cover those today. A variable defined within a function is said to be local to that function. Example Code: Scope.cpp

13 Side Effects A side effect is when a function does more than just return a value. For example: if a function for calculating the area of a square prints something to the screen, the printed output is a side effect. Think back to the black box metaphor. If you are using a function called multiplyThree, all you expect to happen is to receive a product as a result. You don't know what is going on inside that function – just what result you expect. You don't expect it to print anything to the screen. So, when you write functions, they should only do what makes sense based on the name of that function.

14 Practice Writing Functions Take your programs from last week and modify them to use one or more functions: A simple greeting: One function to ask the user for their name, another to say “Hello, THEIR_NAME” Ask the user for two numbers and perform arithmetic (+, -, *, /). Use a function to ask for one number, and call it twice to get both numbers. Modify my HomeworkGradeDivFixed.cpp program (on the website) to ask the user for the 3 grades and the maximum homework grade. Use a function to get the 3 grades. Modify the math programs – SphereVolume.cpp, CtoF.cpp to use a function.

15 Practice Writing Functions Ask the user to type in hours and minutes. Write a function that prints it in the format “HH:MM” Ask the user to type in minutes (can be greater than 60). Write a function that converts minutes to hours and minutes Calculate area of a rectangle Calculate the surface area of a sphere: S = 4 π r 2 “Encode” a letter by adding a number to it.

16 Practice Writing PseudoCode for Functions We haven't learned a lot of the programming constructs we want to use in functions yet, such as conditionals and loops, but we can still write the basic skeleton of our functions we plan to write in the future. Inside those functions, we can write our algorithms (like we did the first day) as comments. When we learn how to write the code for those algorithms, we can come back to the functions and fill them in. 1) Write a program that tells the user to answer a series of questions (you choose) with either a Y or N (uppercase or lowercase). Each time they answer the question, use a function you wrote to check if they followed your directions. 2) Write a program that asks the user for 3 whole numbers. Write a function to determine which of the 3 numbers is the highest, another function to determine which of the 3 numbers is the lowest, and another function to determine which is in the middle. 3) Write a program that uses a function to convert a numeric grade to a letter grade.

17 Exam #1 Next Week! The exam will be during the first half of class. After that, we will continue with a normal lecture. Closed book/note/computer. No UNIX cheat sheet. I will only ask about commands that you should have memorized just by doing your assignments. Review Sheet Questions?

18 Homework #4 Posted online


Download ppt "Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3."

Similar presentations


Ads by Google