chapter_9
chapter_9
chapter_9
List of parameters
(optional)
Type Function
(mandatory)
2
Its just the same as in math …
Result Argument(s)
Name
3
Function Definition
T y p e _ o f_ r e tu r n _ v a lu e F u n c t io n _ n a m e ( p a r a m e te r lis t )
{ // o p e n b lo c k ( b o d y ) : c r e a te s a lo c a l s c o p e fo r th e fu n c tio n
// im p le m e n ta tio n o f th e th e fu n c tio n
r e tu r n r e tu r n _ v a lu e ; // r e tu r n to c a llin g fu n c tio n
// th e ty p e o f th e r e tu r n v a lu e is e q u a l to th e ty p e o f th e
// fu n c tio n
} // c lo s e b lo c k
Example: Compute the Euclidean distance of two points with coordinates (x1,y1)
and (x2,y2).
double length(double x1, double y1, double x2, double y2)
{
double a;
a = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
return a;
}
Example: 6_d
4
Return value
In general, a function returns a value via the return-statement:
return return_value;
return_value can be:
a variable
double length(double x1, double y1, double x2, double y2)
{
double a;
a = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
return a;
}
an arithmetic expression
double length(double x1, double y1, double x2, double y2)
{
return sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}
a logical expression
6
A return value
7
Return value void
void is a special return type, where the return-statement does not send
a value back to the calling function.
Example:
8
Formal and actual parameters
Formal parameters are those given in the declaration (prototype) and definition
(implementation) of the function.
e.g. function length: x1, y1, x2, y2
• The sequence of parameters must be the same in the protoype and in the
calling of the function
9
Function prototypes
Variables: have to be declared to the compiler
Declaration
Type_of_return_value Function_name(parameterlist);
The semicolon at the end of the declaration is mandatory!
Definition
The body of the function that was declared (i.e. the implementation)
10
Code Modularization: the include concept
Our Code
External
Source File Header File
# include <A.h>
(Implementation) (Prototypes) # include <B.h>
use # include <C.h>
A.c A.h
use
B.c B.h
Compiled Content
(Libraries) use
C.lib B.h
Example: 6e
12
The include concept
• Using the #include <name> command the preprocessor inserts the file
<name> before compilation into the source file
Example:
# in c lu d e < s t d io .h >
...
p r in tf( “ % d “ , i) ;
...
13
Important header files
14
The Call Stack
calls returns
mySecondFunction() myRecursiveFunction()
calls returns
myFirstFunction() myRecursiveFunction()
calls returns
main() main()
calls returns
Operating System Operating System
15
6th Assignment: Trapezoidal rule
y
e.g. f ( x ) x
2
f (x)
f (xi )
f (xi+1 )
b
I f ( x ) dx
a
a xi xi+1 b x
1
Ai f ( x i 1 ) f ( x i ) ( x i 1 xi )
2
1 n 1
1
T h f ( x0 ) f ( x j ) f ( xn )
2
j 1 2
16
Input number of subintervals n
interval a,b
17
Variables, pointers and addresses
Motivation: Call by value
reason
18
Call by value
in t q u a d ( in t i)
{
i = i * i;
r e t u r n i;
}
v o id m a i n ( )
{
in t n , q ;
n = 3;
q = q u a d ( n ) ; / / n is c o p ie d t o a n e w v a r ia b le a n d g iv e n t o q u a d
/ / v a lu e o f n is s t ill 3
p r in t f ( “ b a s is % d , s q u a r e % d \ n “ , n , q ) ;
}
Example: 7_b
19
Call by value
v o id m a i n ( )
{ in t q u a d ( in t i)
in t n , q ; {
n = 3; i = i * i;
q = q u a d (n ); r e t u r n i;
p r in t f ( “ b a s is % d , s q u a r e % d \ n “ , n , q ) ; }
}
n Copy i
At the call: 3 3
Computation
In the Function: 9
Copy
n q
At the return: 3 9
20
Question: How can we get that a function returns more than one value?
Function Function
??
o.k.
Every variable has a unique identifier specifying its place in the memory:
the address!
22
Variables, pointers and addresses
• The data type defines, how many bits are reserved for the data and
how the sequence of bits has to be interpreted.
C++ (like C) can control all these aspects of a variable (within certain limitations).
23
Different operators are defined for accessing variables
Reference operator (&)
A reference operator can be used to obtain an address of a variable.
Int andy;
Int fred;
Int ted;
andy = 25;
fred = andy;
ted = &andy;
24
Different operators are defined for accessing variables
Dereference operator (*)
A dereferencing operator gets the value of a given address.
See the following example:
Int beth;
beth = *ted;
25
Definition of a new Datatype called „Pointer“
It is defined as follows:
<type>* <name>;
e.g.
int* points_to_integer;
With the help of pointers you can directly store addresses instead of
dereferencing variables to retrieve the address.
For more complex data tyes (like arrays) it is a lot easier to pass the starting
address than to copy each element into one function.
26
Summary and Examples
27
Exercise for practicing referencing and dereferencing operators
28
Concept of a reference
Example: 7_c
29
Transfer of parameters to functions
v o id m a in ( )
{
in t i ;
in t j ; v o id f ( in t v a l, in t& r e f)
i = 1; {
j = 2; v a l+ + ;
f ( i, j) ; r e f+ + ;
} }
ref
i j val
Memory at call: 1 2 1
void polar(double x1, double y1, double x2, double y2, double* l, double* w)
{
*l = length(x1, y1, x2, y2);
*w = angle(x1, y1, x2, y2);
return;
}
void main()
{
double l,w;
double u1, v1, u2, v2;
….
polar(u1, v1, u2, v2, &l, &w);
….
31
Summary – Different possibilities for function calls
32
Example of the three different ways
Call by value :
void polar_value(double x1, double y1, double x2, double y2, double l, double w)
{
l = length(x1, y1, x2, y2); Call to this function :
w = angle(x1, y1, x2, y2); polar(u1, v1, u2, v2, l, w);
return;
}
Call by reference :
void polar_ref_2(double x1, double y1, double x2, double y2, double& l, double& w)
{
l = length(x1, y1, x2, y2); Call to this function :
w = angle(x1, y1, x2, y2); polar(u1, v1, u2, v2, l, w);
return;
}