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.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Introduction to C Programming
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control.
Week 5 - Wednesday.  What did we talk about last time?  Arrays.
C Program Design Supplements 主講人:虞台文. Content Who takes the return value of main() ?
1 Homework Assignments Turn in HW1 (If not done yet, catch up!) Questions about HW1? Anyone still stuck on apply / UNIX account? Everyone have the books?
COMP1180 Review Date: 4 March, 2009 Time: 10:30am - 12:20pm Venue: –CS students -- FSC801C and FSC801D –IS and other students -- OEE1017 Remarks: – 1)
Introduction to FunctionsCS-2301 D-term Introduction to Functions CS-2301 System Programming C-term 2009 (Slides include materials from The C Programming.
1 CSSE 332 Standard Library, Storage classes, and Make.
0 Chap. 4 Functions and Program Structure 4.1 Basics of Functions 4.2 Functions Returning Non-integers 4.3 External Variables 4.4 Scope Rules 4.5 Header.
Imperative Programming Prof. Béat Hirsbrunner Amine Tafat, PhD Student Matthias Buchs and Raphaël Lesceux, Graduate Students Department of Informatics.
0 Arrays (1/2) #include /* count digits, white space, others */ main() { int c, i, nwhite, nother; int ndigit[10]; nwhite = nother = 0; for (i = 0; i
Functions and Program Structure Chapter 4. Introduction Functions break large computing tasks into smaller ones Appropriate functions hide details of.
0 4.2 Functions Returning Non-integers The return statement is the mechanism for returning a value : return expression; Remark : The expression will be.
Introduction to FunctionsCS-2301 B-term Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.
Introduction to Make Updated by Prasad Spring 2000.
0 make (1/3) : Short Description + Example + Syntax Short Description The purpose of the make utility is to determine automatically which pieces of a large.
Guide To UNIX Using Linux Third Edition
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Computer Science 210 Computer Organization Introduction to C.
1 4.1 Basics of Functions + Control Flow Diagram of a Function Call 4.2 Functions Returning Non-integers + Prototype Function 4.3 External Variables 4.4.
Chapter 4 Functions and Program Structure Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
C Programming language
1 4.1 Basics of Functions 4.2 Functions Returning Non-integers + Function Prototype 4.3 External Variables 4.4 Scope Rules 4.6 Static Variables + Scope.
1 Homework / Exam Finish up K&R Chapters 3 & 4 Starting K&R Chapter 5 Next Class HW4 due next class Go over HW3 solutions.
Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
0 Chap. 5 Pointers and Arrays 5.3Pointers and Arrays 5.4Address Arithmetic 5.5Character Pointers and Functions 5.6Pointer Arrays; Pointers to Pointers.
Homework K&R chapter 4. HW3: ASCII Hex to Integer int axtoi (char s[ ]) { int i, n, flag; n = 0; flag = 1; for ( i = 0; flag; i++) /* for (i = 0; ; i++)
Engineering Computing I Chapter 4 Functions and Program Structure.
Week 2-3 Control flow (review) Conditional statements If, else, else if, switch-case, break Loop constructs for, while, do-while, break, continue, label--go;
Makefiles. Multiple Source Files (1) u Obviously, large programs are not going to be contained within single files. u C provides several techniques to.
Exam / Homework Exam 1 Starting K&R chapter 4 tonight
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
Introduction to FunctionsCIS 1057 Fall Introduction to Functions CIS 1057 Computer Programming in C Fall 2013 (Acknowledgement: Many slides based.
Programming Language  C Functions and Program Structure 主講人:虞台文.
CS 11 C track: lecture 2 Last week: basics of C programming compilation data types ( int, float, double, char, etc.) operators ( + - * / = == += etc.)
Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd.
Discussion Week 1 TA: Kyle Dewey. Project 0 Walkthrough.
CS 261 C Basics Page 2 1/14/2016 CS 261, WSU Vancouver Primitive Types Notes: 4 A is a constant; B is a variable.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
0 4.1 Basics of Functions + Function Call Diagram + make 4.2 Functions Returning Non-integers + Prototype Function 4.3 External Variables 4.4 Scope Rules.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Revisiting building. Preprocessing + Compiling 2 Creates an object file for each code file (.c ->.o) Each.o file contains code of the functions and structs.
Functions and Program Structure Chapter 4. Introduction Functions break large computing tasks into smaller ones Appropriate functions hide details of.
Makefiles Manolis Koubarakis Data Structures and Programming Techniques 1.
Functions and Program Structure CSE 2031 Fall June 2016.
Lecture 3 Translation.
Week 3-4 Control flow (review) Function definition Program Structures
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Fall 2015
Function: Declaration
Computer Science 210 Computer Organization
C Language By Sra Sontisirikit
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Functions and Program Structure
Register Variables Declaring a variable as a "register" variable is an advisory to the compiler to keep the normal location of the variable in a register,
Functions and Modular Programming
Your questions from last session
For regex_compile function in grep.c
Functions, Part 1 of 3 Topics Using Predefined Functions
Homework K&R chapter 4.
Compiler vs linker The compiler translates one .c file into a .o file
Functions, Part 1 of 3 Topics Using Predefined Functions
CPS125.
SPL – PS1 Introduction to C++.
INTRODUCTION TO C.
Preprocessor Directives and Macros Chapter 21
Week 5 - Wednesday CS222.
Presentation transcript:

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 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 File Inclusion Macro Substitution Conditional Inclusion Chap. 4 Functions and Program Structure + make Imperative Programming, B. Hirsbrunner, diuf.unifr.ch/pai/ip S April 2006 S April 2006

1 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

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

3 4.1 Basics of Functions (3/5) : An Example High level solution while (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 code index 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++) ;

4 4.1 Basics of Functions (4/5) : A Skeleton Program #include /* 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[]) {...}

5 4.1 Basics of Functions (5/5) : Execution Diagram #include 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) 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;

6 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. 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 : [ ] # Dependency line # Show how to build the target out of their prerequisites [ ] Common Usage % make progr main.osec.o main.csec.c

7 make (2/2) : Execution Model Dependency checking Remark 1. 'make' echoes each command to the standard output before executing it. Remark 2. If is absent the associated command lines are executed. if (main.o don't exist) then else if ( t main.o // main.o is out-of-date else 1 3 progr main.osec.o 12 3 if (progr don't exist) then else if ((t progr // progr is out-of-date else 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