Learning To Program With Haiku Lesson 3
Learning To Program With Haiku Lesson 3
Lesson 3
Written by DarkWyrm
There is only so much that can be done with direct manipulation like return 1 + 1; which is why we
have variables in programming. A variable is simply a storage container for information, and just like
any real world container, they come in different shapes, sizes, and uses.
#include <stdio.h>
int main(void)
{
// Declaring one integer variable named a
int a;
a = 1;
b = 2;
c = 3;
return a + b + c;
}
Variables can be declared one at a time, such as our variable a, or several at once, like b and c. In
addition to declaring variables for our use in functions, many times we'll get them for free because
many functions require input data to do their work. These variables are called parameters or
arguments.
Function parameters are declared both in a function's declaration and definition. They are listed in a
comma-separated list. Functions which do not take any arguments use the word void to say so.
Speaking of types, there are more types of data than just integers in C++. Each type takes up a different
amount of space in memory, measured in bytes. This is important to remember because the number of
bytes a variable occupies has a direct impact on how much information it can hold. This difference is
evident in the range of values a char can hold as opposed to a short. Type sizes vary from platform to
platform, but here is a pretty good list for Haiku on a 32-bit processor.
Using printf()
Now do you remember the weirdness that we saw a little bit ago with printf()? Let's take another
look at it.
#include <stdio.h>
int main(void)
{
int a;
int b, c;
a = 1;
b = 2;
c = 3;
return a + b + c;
}
printf is one of a few functions which take a variable number of arguments. Its first parameter is
always a string. Depending on the number of placeholders in the string, though, it may or may not have
additional parameters following the string. For example, in our above example, the string has three %d
placeholders, one each for a, b, and c. Three placeholders, three “extra” parameters. Here are some
other possible placeholders for printf that we'll use later on. Note that this is not an exhaustive list to
printf – there are many more options, but these will suffice for now.
Operators
Operators give us ways of working with variables and numbers without calling functions. +, -, and *
are all examples of operators, but C++ has many more than just these. Here are the arithmetic operators
that we'll need for now.
The -- and ++ operators need a little more explanation than is possible in the table. Let's take a look at
some code to explain it best.
#include <stdio.h>
int main(void)
{
int a = 1;
int b = 2;
return 0;
}
Whew! We covered a lot of stuff in this lesson, but using all of it let's us do all sorts of fancy stuff. Let's
put it to use.
#include <stdio.h>
int main(void)
{
int a = 3;
int b = 4;
printf("For the triangle with legs %d and %d, the hypotenuse will be %g\n",
a,b,hypotenuse(a,b));
return 0;
}
hypotenuse() returns a double because we want some sort of precision beyond whole numbers. It is
also the return type for sqrt().
Bug Hunt
Hunt #1
Code
int sum(int first, int second, int third)
{
return first + second + third;
}
int main(void)
{
int a = 3;
int b = 4;
return 0;
}
Errors
foo.cpp: In function ‘int main()’:
foo.cpp:14: error: ‘c’ was not declared in this scope
Hunt #2
Code
#include <stdio.h>
int main(void)
{
int x1,y1,x2,y2;
x1 = 3;
y1 = 3;
x2 = 8;
y2 = 3;
return 0;
}
Errors
foo.cpp: In function ‘double distance(int, int, int, int)’:
foo.cpp:8: error: ‘sqrt’ was not declared in this scope
Project
Using the equation Interest = Principal * rate * time, calculate and print the simple interest incurred on
a principal of $20000 at a rate of 5% per month for 24 months. Use a function to do the actual interest
calculations.