Unit 06 - Unit 06 Functions Unit Objectives • Learn what...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Unit 06 Functions Unit Objectives • Learn what a function is and why they are useful • Write your first function. • Learn how to return data from a function you write back to the main function. • Learn how to declare functions so you can put the function's code some place else. • Learn how to generate random numbers (or how to let someone else do it for you.) Introduction In this unit and the next one we will be concerned with functions . You may not have realized, but you have been writing a function since we first created a 'Hello World' program. The 'main' function is the user- written function the Operating System looks for when you execute your program. So far, its the only function we have written but this is about to change. Task 1 - Writing your second function. For review, here is that program again. You are probably ready to write this program from memory by now! #include <iostream> using namespace std; int main(void) { cout << "Hello World!" << endl; return 1; } Unless I've made an error, this is the program. Now lets call a function. This is a program we have already seen (or should understand) but let's look at it one more time while discuss functions. #include <iostream> #include <cmath> using namespace std; int main(void) { float fX = 2.756; float fAns = 0.0; fAns = sin( fX ); cout << "The answer is:" << fAns << endl; return 1; } Above we call the function sin( ) which takes one argument (a float) and returns the sine of that value (another float) which we can assign to a variable or print directly to the screen. Someone was nice enough to write the code to calculate the sine of a value, and we can use that code by including the file 'cmath'. The function sin encapsulates that code and defines the interface we must adhere to in order to use the code properly. Now we are going to write our own function. What if you wanted the program to print "Hello, World" three times. Can you think of a way to modify the program above to do that? One possibility is #include <iostream> using namespace std; int main(void){ cout << "Hello World!" << endl; cout << "Hello World!" << endl; cout << "Hello World!" << endl; return 1; } Let's say you want to perform the same calculation 10 times. Do you cut and paste that command 10 times? What about a 100? What about a 1,000? If we were only going to use this code in one spot, we could just write a FOR loop, but - like with the sine function - we may want to use the code in more than one place, even in different programs. We need something more flexible....
View Full Document

{[ snackBarMessage ]}

Page1 / 21

Unit 06 - Unit 06 Functions Unit Objectives • Learn what...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online