0 4.1 Basics of Functions + Execution Diagram + make 4.2 Functions Returning Non-integers +...

8
1 4.1 Basics of Functions + Execution Diagram + make 4.2 Functions Returning Non-integers + Prototype Function 4.3 External Variables 4.4 Scope Rules 4.5 Header Files + make (revisited) 4.6 Static Variables 4.7 Register Variables 4.8 Block Structure 4.9 Initialization 4.10 Recursion + Execution Diagram (revisited) + System Calls 4.11 The C Preprocessor 4.11.1 File Inclusion 4.11.2 Macro Substitution 4.11.3 Conditional Inclusion Chap. 4 Functions and Program Structure + make ogramming, B. Hirsbrunner, diuf.unifr.ch/pai/ip S5 - 17 April 2006 S6 - 24 April 2006

Transcript of 0 4.1 Basics of Functions + Execution Diagram + make 4.2 Functions Returning Non-integers +...

Page 1: 0 4.1 Basics of Functions + Execution Diagram + make 4.2 Functions Returning Non-integers + Prototype Function 4.3 External Variables 4.4 Scope Rules 4.5.

1

4.1 Basics of Functions + Execution Diagram + make

4.2 Functions Returning Non-integers + Prototype Function

4.3 External Variables

4.4 Scope Rules

4.5 Header Files + make (revisited)

4.6 Static Variables

4.7 Register Variables

4.8 Block Structure

4.9 Initialization

4.10 Recursion + Execution Diagram (revisited) + System Calls

4.11 The C Preprocessor4.11.1 File Inclusion4.11.2 Macro Substitution4.11.3 Conditional Inclusion

Chap. 4 Functions and Program Structure + make

Imperative Programming, B. Hirsbrunner, diuf.unifr.ch/pai/ip

S5 - 17 April 2006

S6 - 24 April 2006

Page 2: 0 4.1 Basics of Functions + Execution Diagram + make 4.2 Functions Returning Non-integers + Prototype Function 4.3 External Variables 4.4 Scope Rules 4.5.

2

4.1 Basics of Functions (1/5)

• Each Function has the form :

return-type function-name (argument declarations){

declarations and statements}

• Various part may be absent; a minimal function is : dummy () {}

• A program is just a set of definitions of variables and functions

• Communication between functions : – by arguments– value returned by a function– external variables

Page 3: 0 4.1 Basics of Functions + Execution Diagram + make 4.2 Functions Returning Non-integers + Prototype Function 4.3 External Variables 4.4 Scope Rules 4.5.

3

4.1 Basics of Functions (2/5) : Fundamental Paradigm

The Blackboard-Toolbox paradigm

v1

v2

v3

Blackboard of public and private variables

Toolboxof public and private functions

f (x, y, z)

g(x)

Page 4: 0 4.1 Basics of Functions + Execution Diagram + make 4.2 Functions Returning Non-integers + Prototype Function 4.3 External Variables 4.4 Scope Rules 4.5.

4

4.1 Basics of Functions (3/5) : An Example

High level solutionwhile (there’s another line)

if (the line contains the pattern)print it

<--- getline(line)

<--- printf(line)<--- strindex(line,pattern)

Graphical solution for strindex(s,t) and partial C codeindex i = 0, 1, …

s

t

i

index j = i, i+1, …

index k = 0, 1, …k

j

Example: Write a program to print each line of its input that contains a particular “pattern”, i.e. a string of characters (this is a special case of the famous UNIX grep program!)

for (i = 0; s[i] != ‘\0’; i++)

for (j = i, k = 0; t[k] != ‘\0’ && s[j] == t[k]; j++, k++) ;

Page 5: 0 4.1 Basics of Functions + Execution Diagram + make 4.2 Functions Returning Non-integers + Prototype Function 4.3 External Variables 4.4 Scope Rules 4.5.

5

4.1 Basics of Functions (4/5) : A Skeleton Program

#include <stdio.h> /* for printf() */int getline(char line[]);int strindex(char source[], char searchfor[]);char pattern[] = "ould"; /* pattern to search for */

/* find all lines matching pattern -------- KR p.69 */main(){ char line[]; while (getline(line) > 0) if (strindex(line, pattern) >= 0) printf("%s", line);}

/* getline: get line into s, return length */int getline(char s[]) {...}

/* strindex: return index of t in s, -1 if none */int strindex(char s[], char t[]) {...}

Page 6: 0 4.1 Basics of Functions + Execution Diagram + make 4.2 Functions Returning Non-integers + Prototype Function 4.3 External Variables 4.4 Scope Rules 4.5.

6

4.1 Basics of Functions (5/5) : Execution Diagram

#include <stdio.h>int r(int i) {return 2*i;}int q(int i) {return 3*r(i+1);}int p(int i) {return 5*q(i);}main() { p(2); }

main()

p(2)p(2)

q(2)

i1=2;

q(2)

r(3)

i2=2;

r(3)i3=3;

6 return 3*6; 18

return 5*18; 90

A dummy program:

Execution Diagram:

return 2*3;

Page 7: 0 4.1 Basics of Functions + Execution Diagram + make 4.2 Functions Returning Non-integers + Prototype Function 4.3 External Variables 4.4 Scope Rules 4.5.

7

make (1/2) : Short Description + Example + Syntax

Short DescriptionThe purpose of the make utility is to determine automatically which pieces of a large program need to be recompiled, and issue the commands to recompile them.

Example of a Makefile(1) progr : main.o sec.o(2) gcc -o progr main.o sec.o

(3) main.o : main.c(4) gcc -c main.c

(5) sec.o : sec.c(6) gcc -c sec.c

Syntax of a Makefile Rule <target> : [<target's prerequisites>]<newline> # Dependency line

<tab><command line><newline> # Show how to build the target out of their prerequisites

[<tab><command line><newline>]

Common Usage% make% make <target>

progr

main.o sec.o

main.c sec.c

Page 8: 0 4.1 Basics of Functions + Execution Diagram + make 4.2 Functions Returning Non-integers + Prototype Function 4.3 External Variables 4.4 Scope Rules 4.5.

8

make (2/2) : Execution Model

Dependency checking

Remark 1. 'make' echoes each command to the standard output before executing it.Remark 2. If <target's prerequisites> is absent the associated command lines are executed.

if (main.o don't exist) then <execute cmd (4)>else if ( tmain.o < tmain.c) then < execute cmd (4)> // main.o is out-of-dateelse <do nothing>

1

3progr

main.o sec.o1 2

3 if (progr don't exist) then <execute cmd (2)>else if ((tprogr < tmain.o) or (tprogr < tsec.o)) then < execute cmd (2)> // progr is out-of-dateelse <do nothing>

Execution Tree

2 Similar to 1

(1) progr : main.o sec.o(2) gcc -o progr main.o sec.o(3) main.o : main.c(4) gcc -c main.c(5) sec.o : sec.c(6) gcc -c sec.c