CHAP07 - Chapter 7 Functions Dale/Weems/Headington 1...

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

View Full Document Right Arrow Icon
1 Chapter 7 Functions Dale/Weems/Headington
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Chapter 7 Topics Writing a Program Using Functional Decomposition Writing a Void Function for a Task Using Function Arguments and Parameters Differences between Value Parameters and Reference Parameters Using Local Variables in a Function Function Preconditions and Postconditions
Background image of page 2
3 Functions every C++ program must have a function called main program execution always begins with function main any other functions are subprograms and must be called
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Function Calls One function calls another by using the name of the called function next to ( ) enclosing an argument list. A function call temporarily transfers control from the calling function to the called function.
Background image of page 4
5 FunctionName ( Argument List ) The argument list is a way for functions to communicate with each other by passing information. The argument list can contain 0, 1, or more arguments, separated by commas, depending on the function. Function Call Syntax
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Two Parts of Function Definition int Cube ( int n ) heading { body     return n * n * n ; }
Background image of page 6
7 What is in a heading? int Cube ( int n ) type of value returned name of  function parameter list
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 What is in a prototype? A prototype looks like a heading but must end with a semicolon; and its parameter list just needs to contain the type of each parameter. int Cube( int ); // prototype
Background image of page 8
9 When a function is called, temporary memory is set up ( for its value parameters and any local variables, and also for the function’s name if the return type is not void). Then the flow of control passes to the first statement in the function’s body. The called function’s body statements are executed until one of these occurs: return statement (with or without a return value), or, closing brace of function body . Then control goes back to where the function was called.
Background image of page 9

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

View Full DocumentRight Arrow Icon
10 #include <iostream> int Cube ( int ) ; // prototype using namespace std; void main ( ) { int yourNumber ; arguments int myNumber ; yourNumber = 14 ; myNumber = 9 ; cout << “My Number = “ << myNumber ; cout << “its cube is “ << Cube ( myNumber ) << endl ; cout << “Your Number = “ << yourNumber ; cout << “its cube is “ << Cube ( yourNumber ) << endl ; }
Background image of page 10
11 To Compile Successfully, before a function is called in your program, the compiler must previously process either the function’s prototype, or the function’s definition (heading and body)
Background image of page 11

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

View Full DocumentRight Arrow Icon
12 A C++ function can return in its identifier at most 1 value of the type which was specified (called the return type) in its heading and prototype but, a void-function cannot return any value in its identifier
Background image of page 12
Write a void function called DisplayMessage ( ) which you can call from main ( ) to describe the pollution index value it receives as a parameter. Your city describes a pollution Index
Background image of page 13

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

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/13/2011 for the course CSC 140 taught by Professor Lebre during the Spring '04 term at Moraine Valley Community College.

Page1 / 52

CHAP07 - Chapter 7 Functions Dale/Weems/Headington 1...

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

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