Академический Документы
Профессиональный Документы
Культура Документы
7-Functions
7-1 Definition
By using functions we can structure our programs in a more modular way,
accessing all the potential that structured programming can offer to us in C++.
A function is a group of statements that is executed when it is called from some point
of the program. The following is its format:
{ statements }
where:
type is the data type specifier of the data returned by the function.
name is the identifier by which it will be possible to call the function.
parameters (as many as needed): Each parameter consists of a data type
specifier followed by an identifier, They allow to pass arguments to the function
when it is called.
statements is the function's body. It is a block of statements surrounded by
braces { }.
Example # 7-1
The result is 8
28
College of Engineering C++ &Numerical Lecturer
Civil Department Methods Muhammad I. Omer
Lecture #7
return (r);
Example #7-4
// example-7-4 default values in functions 6
29
College of Engineering C++ &Numerical Lecturer
Civil Department Methods Muhammad I. Omer
Lecture #7
#include <iostream> 5
using namespace std;
int divide (int a, int b=2)
{
int r;
r=a/b;
return (r);}
int main ()
{ cout << divide (12);
cout << endl;
cout << divide (20,4);
return 0;}
7-4 Recursivity.
Recursivity is the property that functions have to be called by themselves. It is
useful for many tasks, like sorting or calculates the factorial of numbers. For example,
to obtain the factorial of a number (n!) the mathematical formula would be:
n! = n * (n-1) * (n-2) * (n-3) ... * 1
more concretely, 5! (factorial of 5) would be:
5! = 5 * 4 * 3 * 2 * 1 = 120
and a recursive function to calculate this in C++ could be:
Example #7-5
30
College of Engineering C++ &Numerical Lecturer
Civil Department Methods Muhammad I. Omer
Lecture #7
procedure (myarray);
Example #7-6: for using array as parameter (use function to print array):
// example7-6-arrays as parameters 5 10 15
#include <iostream> 2 4 6 8 10
using namespace std;
31