Notes
Outline
COM1100
Fundamentals of Computer Science –Winter 2000
Lecture 6
01/20/99
Reading material this week
Textbook
Chapter 4 : Decision
4.1 The if statement
4.2 Relational Operator
Chapter 5 : Functions (5.1 ~ 5.4)
Chapter 3 : (optional)
is not exactly like our graphics package
Package 1 – Patterns
NameUseFunctions
Package 2 – Recitation 2
Package 3 – Lab 2
Tuesday’s lecture
Review of previous lectures
C++ programming style
Flow of control
if statement
Today’s lecture
Functions
Constant
C++ source file Layout
More on graphic functions
Functions
The typical way of getting something done in C++ is to call (invoke) a function to do it.
In addition to required main() function, C++ program can also contain any number of additional function.
Purpose
Make code reusable
Make program understandable
A function cannot be called unless it has been previously declared and implemented.
Function Declaration
Function declaration is also referred as function signature (or prototype).
The function signature specifies the interface between the function writer and the function user.
Syntax:
return_type function_name(para1, para2, … , paraN);
Function Signature --1
return_type function_name(para1, para2, … , paraN);
The function signature consists of three parts:
Function name: Typically something meaningful. By convention it starts with a capital (PaintCircle, GetMouse)
built-in functions start with lowercase (sin, cos)
Function name follows the same rule as identifier.
Function Signature --2
return_type function_name(para1, para2, … , paraN);
Function return value type : Specifies what type of value function returns when it completes its task.
Function return value can be used in all situations where a value of the return type can be used (assignment of the value, output, expressions).
Function Signature --3
return_type function_name(para1, para2, … , paraN);
Function arguments : Specifies all values (and their types) that the caller needs to supply (value arguments), as well as variables (and their types) that will be used to return results back to the caller (reference arguments).
Function Signature -- Examples
Examples:
int Increment(int n);
int Max(int x, int y);
void DrawLine(short x1, short y1, short x2, short y2);
void means nothing will be returned by the function.
Function implementation
return_type function_name(para1, para2, …, paraN)
{
statements;
}
double abs(double x); // function declaration
double abs(double x) { // function implementation
if ( x >=0 )   return x;
else return –x;
}
Function implementation – return statement
Syntax: return expression;
Purpose: Exit a function returning the value of the expression as the function result.
When the return statement is processed, the function exists immediately.
Function comments
Comments are for human reader, not compilers, and there is no universal standard for the layout of a function comment.
Basically, you may indicate:
Purpose
Receives(arguments)
Returns of a function.
Function comments -- Example
int Max(int x, int y); // function declaration
int Max(int x, int y)
/* Purpose: computer the maximum of two integers
     Receives: x, y – two integers
     Returns: the larger of the two inputs
*/
{
if (x > y)  return x;
else     return y;
}
Using a function
Once a function has been defined, it can be called by the user, provided the right number and kind of arguments is supplied.
The user (caller) of a function maybe the main() function or other function (including itself -- recursive).
Functions with defined return value can be used anywhere where a data value of the specified type can be used.
Functions that return void are called in a standalone function call.
Using a function -- Example
int Max(int x, int y); // function declaration
int Max(int x, int y) { // function implementation
if (x > y)    return x;
else        return y;
}
Int main() {
int m = 5, n = 9;
int z = Max(m, n); // function used in an assignment
cout << “The larger one is : ” << z << endl;
m = RequestInt(“Enter number 1 : “);
n = RequestInt(“Enter number 2 : “);
// function used in an output statement
cout << “The larger number is : ” << Max(m, n) << endl;
return 0;
}
Magic number -- 1
Circum = 2 * 3.1416 * radius;
Age = 2000 – YearOfBirth;
Quite frequently, literal data used within a program have a more general meaning that is recognized outside the context of the program.
Numbers are normally referred by programmers as magic numbers.
Magic number -- 2
To avoid the problem of having a magic number spread throughout a program in many places and to permit clear identification of more universal constants, such as p, C++ allows the programmer to give these constant these own symbolic name.
Instead of using the number throughout the program, symbolic name is used instead.
const declaration and use
const float PI = 3.1416;
const int currentYear = 2000;
Once declared, a const variable can be used in any C++ statement in place of the number it represents.
Circum = 2 * PI * radius;
Age = currentYear – YearOfBirth;
C++ source file layout
Header comment
#include statements
Constants
Global variables
Function declarations
Function implementations
main() function
Header comments
/*******************************
Project: COM1100 Lab 1
File: Shell.cpp
Purpose: Warmup VC++ IDE
Compiler: Visual C++ 6.0
Programmer: Yuhong Yin & Alice Brown
Sequence: 8 (or 9)
Date : 01/17/2000
******************************/
#include statements
This section lists all included header files
#include <iostream.h>
used for pre-defined headers
<>  - Standard directory,
#include "IOTools.h"
#include "Graphics.h“
used for user-defined headers
“ ”  - current directory & standard directory,
Constants
 This section contains constants that are needed throughout the program file.
 Constant names are uppercase.
const int MAX_GRADE = 100;
const double CLOCK_RADIUS = 5.0;
const double PI = 3.14156;
Global Variables
This section contains the definitions of global variables
double annual_raise; //  this year’s raise for all     //  employee
Every global variable must have a comment explaining its purpose
Function declarations
This section lists all functions’ declarations of the program.
double distance(double x1, double y1, double x2, double y2);
int max(int x, int y);
Function implementations
double distance(double x1, double y1, double x2, double y2) {
implementation;
       }
int max(int x, int y) {
implementation;
}
int main() {
….
return 0;
}
Picture Lab
First defines a function that paints a simple square shaped picture at a given location and of a specified size.
The main program then calls this function several times, testing that the function is well defined and illustrating simple user interfaces.
Picture Lab
Define the function Picture to display a square picture that will fit inside the selected square.
You must use at least one rectangle, one oval, one circle, and two lines and at least three different colors.
More graphic functions
ClearDrawing();
ClearGraphicsWindow();
DrawLine(x1, y1, x2, y2);
FrameRect(x1, y1, x2, y2);
FrameOval(x1, y1, x2, y2);
FrameCircle(x, y, r);
ShowText(“const string”);
MoveTo(x, y);
Today’s lecture
Functions
Constant
C++ Source File Layout
More on Graphic functions
Lecture next week
More on functions
Arguments pass by value
Arguments pass by reference
Flow of control
Repetition – for statement
Format output