bg287

csc110: 2010 03 25 Notes: Procedures

Posted in csc110 by bnmng on 2010 03/25

Homework Read Bronson 6.2, 6.3

Today: Procedures, Parameters,

Procedure:
Lets say: we need code to sort a lest of values. The code for sorting is ten lines, and we do it seven times.. that means 70 lines of code.

It would be more efficient to write the code once and call it.

A procedure, or function, is a deviation from the normal flow of control.

A procedure is called by a line of code. When a procedure is called, it runs. When the procedure is done, the program continues at the line after the line that called it. The program counter keeps track of where the program is.

For example, a procedure called “Rectangle”

/// below this is in the main
s1=5;
s2=10;
Rectangle (s1, s2, ar, peri);
/// above this is in the main

//below this is the procedure defined outside the main
void Rectangle (Side1, Side2, Area, Perimeter)

Terminology: Formal Parameters and Actual Parameters. Formals are in the heading of the procedure, actual parameters are in the function call.

Difference between functions and procedures. Functions yield a specific value to the name of the function.

What this means (yeilding a value to the name of the fuction) is this:

Imagine a function called sqrt. You can do this:
cout << sqrt(x);
In the line above, the name of the function is used as if it is a value.

Call by value: Pass a copy.
Call by reference: Pass the address (not exactly but close).

You can't do a cout of a procedure because it doesn't return a value.

There are three things you need to do with functions and procedures: Prototype; Call; Definition.

//Welcome Program
//This program prints a Welcome Home message
#include
void Print2Lines(); //These two lines are prototypes.
void Print4Lines(); // The are to functions what declarations are to variables.

Int main()
{
Print2Lines(); //This is the call
cout << “Welcome Home” << endl;
Print4Lines();
}

void Print2Lines() //heading
{
cout << “*****************” << endl;
cout << “*****************” << endl;
}

void Print4Lines() //heading
{
cout << “*****************” << endl;
cout << “*****************” << endl;
cout << “*****************” << endl;
cout << “*****************” << endl;
}

Here's a better example, passing arguments

//Welcome Program
//This program prints a Welcome Home message
#include
void PrintLines( int ); //This is the prototype.

Int main()
{
PrintLines(2); //This is the call
cout << “Welcome Home” << endl;
PrintLines(4);
}

void PrintLines( int numlines ) //heading
{

for (countline=1, countline<=numlines, countline++) {
cout << “*****************” << endl;
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: