Beruflich Dokumente
Kultur Dokumente
Basics of C++
Our first program: Hello world
Format of a program
Syntax of literals, keywords, symbols, variables
Data types
int (short, long, unsigned)
float/double
char
Arithmetic operations
Assignment (compound) operators
Standard input (cin) and output (cout)
More complex arithmetic operations and operator
precedence
Advantages:
Main is organized into modular pieces
When you need to change something, you only
Functions
Without function
With function
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
/* traditional first
program */
/* traditional first
program */
int main()
{
cout << "Hello world"
<< endl;
return 0;
}
void Hello()
{
cout << "Hello world"
<< endl;
}
int main()
{
Hello();
return 0;
}
Syntax of Functions
A program is a collection of functions and classes
main and other programmer defined functions
only main is executed automatically when the program starts
other functions must be called to be executed
Programmer-defined functions
piece of code to do a specific job
must be declared before using (calling)
return-type function-name(parameters)
{
local variables
statements
}
to execute a function just write its name (and arguments
for parameters)
function
after the function ends, execution order goes back to the caller
void Hello()
{
cout << "Hello world"
<< endl;
}
int main()
{
Hello();
return 0;
}
return-type function-name(parameters)
{
local-variables
statements
}
<<
<<
<<
<<
<<
<<
<<
endl;
endl;
endl;
endl;
endl;
endl;
endl;
call it
Consider a generic function to calculate the area of
any circle and display it
how will the function know the radius?
solution: parameters
function is defined without knowing the value of the radius
value of radius is passed to the function when it is called
is 3
Output Screen
The area of a circle with radius 3 is 28.26
is 2.5
Output Screen
The area of a circle with radius 3 is 28.26
The area of a circle with radius 2.5 is 19.625
is 1.5
area = 3.14*radius*radius;
cout << "The area of a circle with radius " << radius << " is "
<< area << endl;
}
Output Screen
int main()
{
float r;
r = 3;
calculate_area(r);
calculate_area(2.5);
calculate_area(r / 2);
return 0;
}
it should be a parameter.
If you wont pass the initial value, then it should be a local
variable.
Example: in calculate_area function
area is a local variable since we do not pass its initial value from main
radius is a parameter since we need to pass its initial value from main
the song
each verse repeats using a different animal
refrain parts repeat within verses
a partial output (with two animals only) below:
Old McDonald
McDonalds Farm
(Design of Functions)
Which functions do we need?
a function for only Ee-igh, Ee-igh, oh!
a function for the refrain (nakarat)
a function for And on his farm ... line
animal is parameter
noise
Enter the name of an animal: sheep
Enter noise that a sheep makes: baah
Old MacDonald had a farm, Ee-igh, Ee-igh, oh!
And on his farm he had a sheep, Ee-igh, ee-igh, oh!
With a baah baah here
And a baah baah there
Here a baah, there a baah, everywhere a baah baah
Old MacDonald had a farm, Ee-igh, Ee-igh, oh!
operator >>
Values that are input are stored in variables and then
passed to function verse as arguments
see macinput2.cpp
=
cin
Definition:
type variable_names_separated_by_comma;
{
cout << "enter animal ";
string animal;
cin >> animal;
cout << "enter noise a " << animal << " makes ";
string noise;
cin >> noise;
}
Variable Initialization
Variables have garbage (junk values) until
they are assigned a value using assignment operator or
myint = 5;
an uninitialized variable !
not a syntax error, compiler may or may not warn you!
You may initialize variables at the declaration
int mynumber = 5;
After initialization, you may change the value stored in a
function
e.g. you cannot define a local variable animal in Verse since
Reading Assignment
Section 3.3 (page 83)
Case Study: Pizza Slices
Similar to circle area calculation program
Run the program pizza.cpp
when the function finishes, the function call is replaced by the returned
value
caller functions
void function call is not used in an expression, i.e. no value is
associated with a void function
Head();
DoThat();
Verse("cow", "moo");
suppose circlearea function takes the radius as parameter and returns the
area.
In the program we call circlearea as an expression (you have to use the
returned value somewhere)
area = circlearea(r);
cout << circlearea(12) << endl;
if (circlearea(r/2) >= 100)
{
cout << large circle << endl;
}
circlearea(r);
exam
Example use of function sqrt
see usemath.cpp
how did we use sqrt function?
in cout as an expression
Function Syntax
return-type func-name(parameters)
{
local variables
statements
}
Example: Function to calculate volume of a sphere
function body
Function Syntax
double SphereVol(double radius)
{
return 4.0*radius*radius*radius*acos(-1)/3;
}
Function heading/prototype shows return type.
Only one return is a good programming style to have control of bigger functions
"Monday" for 1,
"Saturday" for 6
return "Sunday";
day) return "Monday";
day) return "Tuesday";
day) return "Wednesday";
day) return "Thursday";
day) return "Friday";
day) return "Saturday";
string dayName;
int
dayNum;
cout << " enter day (0-6): ";
cin >> dayNum;
dayName = WeekDay(dayNum);
Which is/are correct use of WeekDay
function? Why?
cout << WeekDay(5) << endl;
int j = WeekDay(0);
cout << WeekDay(2.1) << endl;
string s = WeekDay(22);
WeekDay(3);
Function documentation
Functions usually have a precondition
What conditions (e.g. value of parameters) must be true for the
function to work as intended?
If there are no parameters, then no precondition
Some functions work for every parameter value
no precondition
See pizza2.cpp
Alternatively:
Every year divisible by 400 is a leap year
Otherwise, years divisible by 100 are not leap years
Otherwise, years divisible by 4 are leap years
Otherwise, not a leap year
Boolean function
bool IsLeapYear(int year);
// pre: year > 0
// post: return true if year is a leap year
brackets)
void display (string name)
{
cout << Hello << name << endl;
}
first call
Problem of function declaration order
You cannot call a function before you declare it
Function
Declarations
Prototypes
Function Prototypes
*** Do not forget semicolon after the prototype
definition ***
no semicolon after the parameters in normal definition