1 / 8

Chap. 4 Functions and Program Structure + make

Chap. 4 Functions and Program Structure + make. 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

mab
Download Presentation

Chap. 4 Functions and Program Structure + make

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chap. 4 Functions and Program Structure + make • 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 S5 - 25 April 2006 S6 - 2 May 2006 Imperative Programming, B. Hirsbrunner, diuf.unifr.ch/pai/ip

  2. Each Function has the form : • return-type function-name (argument declarations) • { • declarations and statements • } 4.1 Basics of Functions (1/5) • 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

  3. v1 v3 f (x, y, z) v2 g(x) Blackboard of public and private variables Toolbox of public and private functions 4.1 Basics of Functions (2/5) : Fundamental Paradigm The Blackboard-Toolbox paradigm

  4. i index i = 0, 1, … s j index j = i, i+1, … t k index k = 0, 1, … for (i = 0; s[i] != ‘\0’; i++) for (j = i, k = 0; t[k] != ‘\0’ && s[j] == t[k]; j++, k++) ; 4.1 Basics of Functions (3/5) : An Example 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!) • High level solution while (there’s another line) if (the line contains the pattern) print it <--- getline(line) <--- strindex(line,pattern) <--- printf(line) Graphical solution for strindex(s,t) and partial C code

  5. /* 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[]){...} 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); }

  6. p(2) p(2) i1=2; q(2) q(2) i2=2; r(3) r(3) i3=3; 6 18 return 2*3; 90 return 3*6; return 5*18; 4.1 Basics of Functions (5/5) : Execution Diagram A dummy program: #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); } Execution Diagram: main()

  7. 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 progr main.o sec.o main.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>] make (1/2) : Short Description + Example + Syntax Short Description The 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. Common Usage % make % make <target>

  8. 1 3 2 1 1 2 3 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-date else<do nothing> Similar to 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-date else<do nothing> make (2/2) : Execution Model Dependency checking (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 progr main.o sec.o Execution Tree 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.

More Related