# Structured programming

*226,874*pages on

this wiki

Requires: Basic programming

This is where you really learn how to program. In this unit you'll learn to solve real problems. This is more an art than a science, and we can not communicate to you the skills you will need. These you'll have to develop yourself, mostly by practising. What we can do is just to provide the tools you may need...

## Functions

C and C++ are both organized around the *function* concept. A function is like a machine: it takes some input and gives some output. But, since we're working with computers, the input and output is *information*, i.e.: numbers, text, graphics...

So let's consider a simple function. It takes an integer number and returns its square:

int square(int x) { return x*x; }

Simple enough, isn't it? But, how to use it? In your previous programs, you used to type `int main()`

farily often, and we didn't give to you a proper explanation.
Main is the *main* function in a C or C++ program, the one you're really running when you start the program. So, you always need to provide one! Let's try the following:

#include<stdio.h>

int square(int x) { return x*x; }

int main() { int a=6; printf("The square of %d is %d\n",a,square(a)); }

So, if you need to square many times in your program, it is convenient to have the function so as you don't have to type it so often...

A function may take more than an argument, like for example

int sum(int a, int b) { return a+b; }

### Exercises

1. Using the function `square`

, write the program which gives you the squares of the first 10 numbers.

2. Create a function `multiply`

, which returns the product of two numbers, and use to write a program to provide the multiplication table for number 6.

## Real numbers

Let's suppose that you want to use non-integer numbers. You have to declare them as *float*. This stands for *floating point*, and we'll explain this to you later... Check the following program:

#include<stdio.h> #include<math.h> int main() { float x=2.0; printf("The square root of %g is %g\n",x,sqrt(x)); }

You will notice a few things: we've two of these funny `#include`

lines, the new one being called `math`

. The reason is that the mathematical functions are not by default included by the C-C++ compilers... The second is the use of `sqrt(x)`

, i.e.: the square root of `x`

. And the third is that we don't use `%d`

, but `%g`

, because we want to see the decimal point.

### Exercises

1. Write a program to get the square roots of the first 10 integers.

## More about loops

You've learnt how to use a basic loop. You may want to do things like the following:

- Changing the step: instead of 1, 2, 3... use 2, 4, 6...

The funny code at the end of our classical loop is `i++;`

. This is one of the most curious points in C. It means: increment `i`

by 1. Change it so as it increments by 2. How to do it?

In C, `a+=b;`

means *increment a by b. So you may try the next loop:*

for (i=2;i<=10;i+=2) printf("The square of %d is %d\n",i,i*i);

- What about a countdown?

Easy:

for (int i=10;i>=1;i--)

This will take 10,9,8,7,6,5,4,3,2,1. If you need to descend faster, you may try
`i-=2`

.

- Nested loops. Now for some real fun, try the next program

#include<stdio.h> int main() { int i,j; for (i=1;i<=5;i++) for (j=1;i<=5;j++) printf("%3d\n",i*j); }

The main feature introduced by this program is the nested loop structure. For each value of *i*, do all values of *j*. So we do for the pairs i,j: (1,1) (1,2) (1,3) (1,4) (1,5) (2,1) (2,2) ... (5,5).

- What if I have to do many things in the loop? This is the easiest. Put them in braces:

for (i=1;i<=10;i++) { printf("Trying number %d\n",i); printf("Whose square is %d\n",i*i); }

### Exercises

Deeper into loops (for, do-while, until...), conditionals (logical operators), switch-case...

Functions

Lots of problems and examples, second level

Now read: Data structures