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.

Slides:



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

CSE 303 Lecture 16 Multi-file (larger) programs
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control.
Chapter 7: User-Defined Functions II
1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014.
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
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.
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.
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.
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.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Week 3 Part I Kyle Dewey. Overview Odds & Ends Constants Errors Functions Expressions versus statements.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Chapter 4 Functions and Program Structure Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering.
Copyright 2001 Oxford Consulting, Ltd1 January Storage Classes, Scope and Linkage Overview Focus is on the structure of a C++ program with –Multiple.
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
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
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.
Functions Kernighan/Ritchie: Kelley/Pohl: Chapter 4 Chapter 5.
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++)
18. DECLARATIONS.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Engineering Computing I Chapter 4 Functions and Program Structure.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
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.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
Exam / Homework Exam 1 Starting K&R chapter 4 tonight
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
FUNCTIONS. Funtions  The heart of effective problem solving is problem decomposition.  breaking a problem into small, manageable pieces  In C, the.
Introduction to FunctionsCIS 1057 Fall Introduction to Functions CIS 1057 Computer Programming in C Fall 2013 (Acknowledgement: Many slides based.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Functions. Motivation What is a function? A function is a self-contained unit of program code designed to accomplish a particular task. We already used.
Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd.
First Compilation Rudra Dutta CSC Spring 2007, Section 001.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
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.
Functions and Program Structure Chapter 4. Introduction Functions break large computing tasks into smaller ones Appropriate functions hide details of.
Functions and Program Structure CSE 2031 Fall June 2016.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 9: Value-Returning Functions
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
Computer Science 210 Computer Organization
Chapter 7: User-Defined Functions II
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Programmazione I a.a. 2017/2018.
User-Defined Functions
Computer Science 210 Computer Organization
Scope, Parameter Passing, Storage Specifiers
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,
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Functions and Program Structure
Homework Finish up K&R Chapters 3 & 4
Functions and Program Structure
Compiler vs linker The compiler translates one .c file into a .o file
INTRODUCTION TO C.
Presentation transcript:

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 Scope Rules 4.5 Header Files 4.6 Static Variables + Scope and Lifetime of Variables and Functions 4.7 Register Variables 4.8 Block Structure 4.9 Initialization 4.10 Recursion + Function Call Diagram (revisited) + System Calls 4.11 The C Preprocessor : C language facilities cpp, file inclusion (#include), macro (#define), conditional (#if, #ifdef, #ifndef) Chap. 4 Functions and Program Structure Imperative and System Programming, B. Hirsbrunner, diuf.unifr.ch/pai/ip, Lecture 4 – 10 October 2012 KR4 : ~70’; Tutorials: Mem_Diagr II: ~20', Make: 30', Macro: ~15’; Exercises: ~45’

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

3 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)

4 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 C code excerpt 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++) ;

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

6 4.1 Basics of Functions (5/5) : Function Call 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: Control Flow Diagram of a Function Call: return 2*3;

7 4.2 Functions Returning Non-integers The return statement is the mechanism for returning a value : return expression; Remark : The expression will be converted to the return type of the function if necessary.

8 Function Prototypes 1. Full function prototype (recommended !) return-type function-name (argument declarations) 2. Shortest recommended function prototype (ok in some special cases) return-type function-name (type declarations) 3. Older function declaration (never recommended !, only ok for main()) return-type function-name () 4. Shortest function declaration (never recommended !) function-name () Remark. (3) and (4) are accepted in the ANSI standard, but are only intended to permit older C programs to compile with new compilers. It’s a very bad idea to use it with new programs. Example 1. int toto (char c) 2. int toto (char) 3. int toto () 4. toto ()

9 4.3 External Variables A program is just a set of external objects, which are either variables or functions Functions are always external C does not allow functions to be defined inside other functions External variables are defined outside any function Internal variables to a function come into existence when the function is entered, and disappear when it is left

Scope Rules Definition The scope of a name is the part of the program within which the name can be used. Scope of an external variable or function lasts from the point at which it is declared to the end of the file being compiled Scope of an automatic variable (i.e. local variable of a function) is the block in which the name is declared Remarks about the declaration and definition of an external variable A declaration announces the properties of a variable (primary its type and name), whereas a definition also causes storage to be aside : extern double val[]; /* declaration of an external variable */ double val[MAXVAL]; /* definition of an external variable */ Usage: if an external variable is to be referred before it is defined, or if it is defined in a different file, then the extern declaration is mandatory

Header Files : Hello World (revisited) Header file.h dependency graph hello.c tellMe.c tellMe.h #include "tellMe.h" /* for tellMe() */ main() { tellMe("Hello World"); } hello.c : #include /* for printf() */ #include "tellMe.h" /* for tellMe() */ void tellMe(char s[]) { printf(”tellMe> %s\n", s); } tellMe.c : void tellMe(char s[]); tellMe.h : Header file.h : specification Source file.c : implementation Makefile : hello : hello.o tellMe.o gcc -o hello hello.o tellMe.o hello.o : hello.c tellMe.h gcc -c hello.c tellMe.o : tellMe.c tellMe.h gcc -c tellMe.c make.c dependency graph hello hello.otellMe.o hello.ctellMe.c

Static Variables Definition 1.The static keyword, applied to an external variable or function, limits the scope of that object to the rest of the source file. 2.The static keyword, applied to an automatic variable (i.e. a variable defined locally to a function), promote that variable to a permanent variable. Usage static char buf[BUFSIZE]; /* buffer for ungetch */ static int bufp = 0 /* next free position in buf */ Remark 1.A static external variable is a "glocal variable with permanent storage" (local relative to the file in which it is declared, and global relative all functions defined to the rest of the file; glocal is a contraction of global and local) 2.A static automatic variable is a "local variable with permanent storage" (local relative to the function in which it is declared)

13 Scope and Lifetime of Variables and Functions Characterization of scope (visibility) Global (G) the scope is the whole program (more precisely from the point on where the var/fct has been declared to the end of the source file) Semi-Global (SG) scope is in between global and local, e.g. the file where the var/fct has been declared and defined Local (L) scope is the enclosed braces {} Characterization of lifetime Permanent (P) lifetime is the same as the one of the program Semi-Permanent (SP) lifetime is in between permanent and volatile Volatile (V) lifetime is the same as the one of the enclosed braces {}, or of the associated function

Register Variables Definition A register declaration advises the compiler that the variable in question will be heavily used. Usage void f(register unsigned m, register long n) { register int i;... } Remarks 1.The idea is that register variables are to be placed in machine registers (i.e. a small very rapid but very expensive memory). 1.The compilers are free to ignore the advice. 2.The register declaration can only be applied to automatic variables and to the formal parameters of a function.

Block Structure Variable can be defined in a block-structured fashion !!! Declarations of variables (including initializations) may follow the left brace that introduces any coumpound statement, not just the one that begins a function. Function cannot be defined in a block-strutured fashion !!! Functions may not be defined within other functions. Example if (n>0) { int i; /* declare a new integer i */ for (i=0; i<n; i++)... } The scope of the variable i is the "true" branch of the if; this i is unrelated to any i outside the block. Remark. Let av and sv be an automatic resp. static variable declared and initialized in a block. Then av is initialized each time the block is entered and sv only the first time. (Hint: av is a local variable without permanent storage and sv is a local variable with permanent storage!)

Initialization Fundamentals External and static variables : initialization at startup –The initialization is done once, conceptually before the program begins execution –The initializer must be a constant expression –The value is initialized to zero in the absence of explicit initialization Automatic and register variables : initialization at run time –The initialization is done during the execution of the program, that is each time the function or block is entered –The initializer can be any expression –The value is undefined (i.e. garbage) in the absence of explicit initialization Usage int x = 1; char squote = '\''; long day = 1000L * 60L * 60L * 24L; /* milliseconds/day */ int binsearch(int x, int v[], int n) { int low=0, high=n-1, mid;... }

Recursion + Function Call Diagram #include int f(int n) {if (n>1) return n*f(n-1); else return 1;} /* f(n)=n! */ main () {printf("%d\n", f(getchar()-'0')); return 0;} fac.c % gcc -o fac fac.c %./fac In Bash (i.e. terminal) main() f(3) f(2) n1=3; f(2) f(1) n2=2; f(1) return 1; n3=1; 1 return 2*1; 2 return 3*2; 6 getchar(); printf("%d\n", 6); Bash (terminal)./fac Assume the user hit the key '3' See next slide ! return 0 0 Control Flow Diagram of a Function Call (for %./fac) %./fac ;./fac %./fac &&./fac %./fac ||./fac %./fac |./fac Try (hit 3 when needed) (factorial test program)

18 UNIX Shell and System Calls 1 System call 2 OS kernel is in charge to (1) read the next character from the standard input file, i.e. from the keyboard (assume the user hit the key '3'), (2) display it on the terminal window and (3) return it to getchar() 3 OS kernel is in charge to write the received value to the standard output file, i.e. the terminal window main() UNIX Shell (terminal) fac getchar() '3' f(3) OS kernel readWithEcho(...) 1 2 'read c' and display it getchar() printf(...) printf() write(...) 1 3 'write' return 0 0 printf()

The C Preprocessor : C language facilities (1/2) For a multifile program, the first three operations are performed on each individual source code file, creating an object file for each source code file. The final linking operation combines all object codes. When you invoke gcc without options, it initiates a sequence of four basic operations: (1) Preprocessing, (2) Compilation, (3) Assembly, (4) Linking. The cpp preprocessor allow you, among other things, to display the result of a preprocessing operation. Example. Consider we have the following text in the 'macro.txt' file: #define square(X) (X)*(X) square(2) square(x+1) and then invoke the cpp preprocessor: % cpp macro.txt (2)*(2) (x+1)*(x+1) % Note that 'macro.txt' is not a correct C source code file; but it is fine for preprocessing.

The C Preprocessor : C language facilities (2/2) Macro. A macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of the macro. Usage: #define name replacement-text Example: #define square(x) (x)*(x) #define square(x) x*x // right syntax, but wrong semantics! Conditional. A conditional is a directive that instructs the preprocessor to select whether or not to include a chunk of code in the final token stream passed to the compiler. For example, a program may need to use different code depending on the machine or operating system it is to run on. "Usage": There are 3 conditional directives: #if, #ifdef or #ifndef File inclusion. The #include directive works by directing the C preprocessor to scan the specified file as input before continuing with the rest of the current file. Usage: #include // for system header files #include "file" // for header files of your own program Example: #include