1 functions (covered by chapter 5 in abc). 2 type function_name( parameter list ) { declarations...

21
1 Functions (covered by Chapter 5 in ABC)

Post on 20-Dec-2015

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Functions (covered by Chapter 5 in ABC). 2 Type function_name( parameter list ) { Declarations Statements } Function Definition Header body Partitioning

1

Functions

(covered by Chapter 5 in ABC)

Page 2: 1 Functions (covered by Chapter 5 in ABC). 2 Type function_name( parameter list ) { Declarations Statements } Function Definition Header body Partitioning

2

Type function_name( parameter list )

{

Declarations

Statements

}

Function Definition

HeaderHeader

bodybody

Partitioning the code into functions makes it more readable and self-documenting

Page 3: 1 Functions (covered by Chapter 5 in ABC). 2 Type function_name( parameter list ) { Declarations Statements } Function Definition Header body Partitioning

3

Runtime Stackreturn value

actual parameters

f2() control link

activation saved machine status

record local data

temporaries

return value

actual parameters

f1() control link

activation saved machine status

record local data

temporaries

return value

actual parameters

main() control link

activation saved machine status

record local data

temporaries

Activation records / stack frames are created and stored in an area of memory termed the runtime stack.

Activation records / stack frames are created and stored in an area of memory termed the runtime stack.

void f2(char c, int i){...

}void f1(int i){

...f2('a', i);...

}int main(void){

f1(1);return 0;

}

Page 4: 1 Functions (covered by Chapter 5 in ABC). 2 Type function_name( parameter list ) { Declarations Statements } Function Definition Header body Partitioning

4

Storage Organization• The program stack grows each

time a function call is made.

• Infinite recursion results in a collision between the runtime stack and the heap termed a run-time stack overflow error.

• Illegal pointer de-references (garbage, dangling-references) often result in memory references outside the operating system allocated partition, (segment) for the C program resulting in a segmentation error (GPF - access violation) and core dump.

System Privileges

(not accessible to the C program)

Binary Code

Static Data

Runtime Stack

Function activation

record management

Dynamic memory

structure management

Heap

Typical C program execution memory model Typical C program execution memory model

Page 5: 1 Functions (covered by Chapter 5 in ABC). 2 Type function_name( parameter list ) { Declarations Statements } Function Definition Header body Partitioning

5

double abs_value( double x )

{

if ( x >= 0.0 )

return x;

else

return -x;

}

•Conversions

•No use of return value

•The stack mechanism

The return statement

The evaluated value is returnedThe evaluated value is returned

return;return ++a;return (a * b);

Page 6: 1 Functions (covered by Chapter 5 in ABC). 2 Type function_name( parameter list ) { Declarations Statements } Function Definition Header body Partitioning

6

double sqrt( double );

void f(char c, int i);

is equivalent to:

void f(char, int);

Function Prototypes

When declaring a function, the names of the input parameters do not matter, only their type and order.

When declaring a function, the names of the input parameters do not matter, only their type and order.

Page 7: 1 Functions (covered by Chapter 5 in ABC). 2 Type function_name( parameter list ) { Declarations Statements } Function Definition Header body Partitioning

7

#include <stdio.h>

#define N 7

long power( int, int );

void prn_heading( void );

void prn_tbl_of_powers( int );

int main(void)

{

prn_heading();

prn_tbl_of_powers( N );

return 0;

}

Functions Example

Function declaration (Prototypes)

Function declaration (Prototypes)

Using the functionsUsing the functions

Page 8: 1 Functions (covered by Chapter 5 in ABC). 2 Type function_name( parameter list ) { Declarations Statements } Function Definition Header body Partitioning

8

Functions Examplevoid prn_heading( void )

{

printf( “\n::::: A TABLE OF POWERS :::::\n\n” );

}

long power( int m, int n)

{

int i = 0;

long product = 1;

for ( i = 1; i <= n; ++i )

product *= m;

return product;

}

Page 9: 1 Functions (covered by Chapter 5 in ABC). 2 Type function_name( parameter list ) { Declarations Statements } Function Definition Header body Partitioning

9

Functions Examplevoid prn_tbl_of_powers( int n )

{

int i = 0, j = 0;

for ( i = 1; i <= n; ++i )

{

for ( j = 1; j <= n; ++j )

{

if ( j == 1 )

printf( “%ld”, power( i, j ) );

else

printf( “%9ld”, power( i, j ) );

}

putchar( ‘\n’ );

}

}

::::: A TABLE OF POWERS :::::

1 1 1 1 1 1 1

2 4 8 16 32 64 128

3 9 27 81 243 729 2187

…..

Page 10: 1 Functions (covered by Chapter 5 in ABC). 2 Type function_name( parameter list ) { Declarations Statements } Function Definition Header body Partitioning

10

#include <stdio.h>

int main( void )

{

int n = 3, sum = 0, compute_sum( int );

printf( “%d\n”, n );

sum = compute_sum( n );

printf( “%d\n”, n );

printf( “%d\n”, sum );

return 0;

}

what happens to n and sum here?what happens to n and sum here?

Call by Value

Function declarationFunction declaration

3 is printed3 is printed

3 is printed3 is printed

6 is printed6 is printed

Page 11: 1 Functions (covered by Chapter 5 in ABC). 2 Type function_name( parameter list ) { Declarations Statements } Function Definition Header body Partitioning

11

Call by Value

int compute_sum( int n ) {

int sum = 0;

for ( ; n > 0; --n ) sum += n;return sum;

}

sum the integers from 1 to nsum the integers from 1 to n

This n is local. It gets the value of the of the varuable that was sent to the function.

This n is local. It gets the value of the of the varuable that was sent to the function.

sum is local to the function and.sum is local to the function and.

local n and sum are changedlocal n and sum are changed

Page 12: 1 Functions (covered by Chapter 5 in ABC). 2 Type function_name( parameter list ) { Declarations Statements } Function Definition Header body Partitioning

12

Function invocation - summary

1. Each expression in the argument list is evaluated.

2. The value of the expression is converted, if necessary, to the type of the formal parameter, and is assigned to its corresponding formal parameter ("call by value") at the beginning of the function's body.

3. The body of the function is executed.

4. If a 'return' statement is executed, then control is passed back to the calling environment.

5. If the 'return' statement includes an expression then the value of the expression is converted, if necessary, to the type given by the type specifier of the function, and that value is passed to the calling environment as well.

6. If the 'return' statement does not include an expression, no useful value is returned.

7. If no 'return' statement is present, control is passed back to the calling environment at the end of the function's body. No useful value is returned.

Page 13: 1 Functions (covered by Chapter 5 in ABC). 2 Type function_name( parameter list ) { Declarations Statements } Function Definition Header body Partitioning

13

#includes#defineslist of prototypes

#include “pgm.h” #include “pgm.h” #include “pgm.h”

main.c fct.c prn.c

pgm.h

Developing large programs

gcc -g -ansi -pedantic-errors main.c fct.c prn.c -o pgmgcc -g -ansi -pedantic-errors main.c fct.c prn.c -o pgm

Page 14: 1 Functions (covered by Chapter 5 in ABC). 2 Type function_name( parameter list ) { Declarations Statements } Function Definition Header body Partitioning

14

fct.c#include "pgm.h”

void fct1( int n )

{

int i = 0;

printf( “Hello from fct1()\n” );

for ( i = 0; i < n; ++i )

fct2();

}

void fct2( void )

{

printf( “Hello from fct2()\n” );

}

Page 15: 1 Functions (covered by Chapter 5 in ABC). 2 Type function_name( parameter list ) { Declarations Statements } Function Definition Header body Partitioning

15

main.c#include "pgm.h"

int main( void )

{

char ans = 0;

int i = 0, n = N;

printf( “%s”, “This program does not do very much.\n”

“Do you want more information? ”);

scanf( “ %c”, &ans );

if ( ans == ‘y’ || ans == ‘Y’ )

prn_info( “pgm” );

for ( i = 0; i < n; ++i )

fct1( i );

printf( “Bye!\n” );

return 0;

}

Page 16: 1 Functions (covered by Chapter 5 in ABC). 2 Type function_name( parameter list ) { Declarations Statements } Function Definition Header body Partitioning

16

pgm.h

#include <stdio.h>

#include <stdlib.h>

#define N 3

void fct1(int k);

void fct2(void);

void prn_info(char *);

Page 17: 1 Functions (covered by Chapter 5 in ABC). 2 Type function_name( parameter list ) { Declarations Statements } Function Definition Header body Partitioning

17

prn.c#include "pgm.h"

void prn_info( char *pgm_name )

{

printf( “Usage: %s\n\n”, pgm_name );

printf( “%s\n”, “This program illustrates how one can write a program\n”

“in more than one file. In this example, we have a\n”

“single .h file that gets included at the top of our\n”

“three .c files. Thus the .h file acts as the \“glue\”\n”

“that binds the program together.\n\n”

“Note that the functions fct1() and fct2() when called\n”

“only say \“hello.\” When writing a serious program, the\n”

“programmer sometimes does this in a first working\n”

“version of the code.\n” );

}

Page 18: 1 Functions (covered by Chapter 5 in ABC). 2 Type function_name( parameter list ) { Declarations Statements } Function Definition Header body Partitioning

18

Assertions#include <assert.h>

#include <stdio.h>

int f( int a, int b );

int g( int c );

int main( void )

{

int a = 0, b = 0, c = 0;

....

scanf( “%d%d”, &a, &b );

.....

c = f( a,b );

assert( c > 0 );

.....

}

an assertion: used to enforce certain logical conditionsan assertion: used to enforce certain logical conditions

Page 19: 1 Functions (covered by Chapter 5 in ABC). 2 Type function_name( parameter list ) { Declarations Statements } Function Definition Header body Partitioning

19

Assertions

int f( int a, int b )

{

.....

assert( a == 1 || a == -1 );

assert( b >= 7 && b <== 11 );

......

}

The assertion makes sure that the statement evaluates to true

The assertion makes sure that the statement evaluates to true

Page 20: 1 Functions (covered by Chapter 5 in ABC). 2 Type function_name( parameter list ) { Declarations Statements } Function Definition Header body Partitioning

20

Scope Rules{

int a = 1, b = 2, c = 3;

printf( “%3d%3d%3d\n”, a, b, c );

{

int b = 4;

float c = 5.0;

printf( “%3d%3d%5.1f\n”, a, b, c );

a = b;

{

int c;

c = b;

printf( “%3d%3d%3d\n”, a, b, c );

}

printf( “%3d%3d%5.1f\n”, a, b, c );

}

printf( “%3d%3d%3d\n”, a, b, c );

}

What is the output of this code?

Page 21: 1 Functions (covered by Chapter 5 in ABC). 2 Type function_name( parameter list ) { Declarations Statements } Function Definition Header body Partitioning

21

Scope Rules{

int a = 1, b = 2, c = 3;

printf( “%3d%3d%3d\n”, a, b, c );

{

int b = 4;

float c = 5.0;

printf( “%3d%3d%5.1f\n”, a, b, c );

a = b;

{

int c;

c = b;

printf( “%3d%3d%3d\n”, a, b, c );

}

printf( “%3d%3d%5.1f\n”, a, b, c );

}

printf( “%3d%3d%3d\n”, a, b, c );

}

1 2 31 2 3

1 4 5.01 4 5.0

4 4 44 4 4

4 4 5.04 4 5.0

4 2 34 2 3