unit06mp - Course Philosophy Dr. Pryor FAQ's ME205 TA Page...

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

View Full Document Right Arrow Icon
Course Philosophy Dr. Pryor FAQ's ME205 TA Page Office Hours Other Resources Syllabus Tutorials Countdown to the due date: 49 days, 2 hours, 41 minutes Home » 06 bam2734 My account Log out Test Center
Background image of page 1

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

View Full DocumentRight Arrow Icon
Units and Reviews 00 01 02 03 04/R1 05 06 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.
Background image of page 2
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> int namespace std; void 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; }
Background image of page 3

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

View Full DocumentRight Arrow Icon
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. A function is a little bit of computer code that can be reused over and over in our program or
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/13/2010 for the course ME me205 taught by Professor Pryor during the Spring '09 term at University of Texas-Tyler.

Page1 / 17

unit06mp - Course Philosophy Dr. Pryor FAQ's ME205 TA Page...

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

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