Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Functions (covered by Chapter 5 in ABC). 2 Type function_name( parameter list ) { Declarations Statements } Function Definition Header body Partitioning."— Presentation transcript:

1 1 Functions (covered by Chapter 5 in ABC)

2 2 Type function_name( parameter list ) { Declarations Statements } Function Definition Header body Partitioning the code into functions makes it more readable and self-documenting

3 3 Runtime Stack return value actual parameters f2()control link activationsaved machine status recordlocal data temporaries return value actual parameters f1()control link activationsaved machine status recordlocal data temporaries return value actual parameters main()control link activationsaved machine status recordlocal data temporaries 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; }

4 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

5 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 returned return; return ++a; return (a * b);

6 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.

7 7 #include #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) Using the functions

8 8 Functions Example void 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; }

9 9 Functions Example void 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 ::::: 1111111 248163264128 3927812437292187 …..

10 10 #include 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? Call by Value Function declaration 3 is printed 6 is printed

11 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 n 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. local n and sum are changed

12 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.

13 13 #includes #defines list of prototypes #include “pgm.h” main.cfct.c prn.c pgm.h Developing large programs gcc -g -ansi -pedantic-errors main.c fct.c prn.c -o pgm

14 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” ); }

15 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; }

16 16 pgm.h #include #define N 3 void fct1(int k); void fct2(void); void prn_info(char *);

17 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” ); }

18 18 Assertions #include 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 conditions

19 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

20 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?

21 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 3 1 4 5.0 4 4 4 4 4 5.0 4 2 3


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

Similar presentations


Ads by Google