Functions C and Data Structures Baojian Hua

Slides:



Advertisements
Similar presentations
Data Structure & Abstract Data Type
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.
FUNCTIONS Or METHODS.
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
C Module System C and Data Structures Baojian Hua
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
C and Data Structures Baojian Hua
C Module System C and Data Structures Baojian Hua
C and Data Structures Baojian Hua
Functions and Program Structure Chapter 4. Introduction Functions break large computing tasks into smaller ones Appropriate functions hide details of.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Scope Rules and Storage Types CS-2303, C-Term Scope Rules and Storage Types CS-2303, System Programming Concepts (Slides include materials from The.
Recursion!. Can a method call another method? YES.
Guide To UNIX Using Linux Third Edition
Closure and Environment Compiler Baojian Hua
Storage & Linkage: Effects on Scope Rudra Dutta CSC Spring 2007, Section 001.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Abstract Data Types Using Classes Lecture-5. Abstract Data Types Using Classes Representing abstract data types using C++ We need the following C++ keywords.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Chapter 6: User-Defined Functions
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Engineering Computing I Chapter 4 Functions and Program Structure.
Mixing integer and floating point numbers in an arithmetic operation.
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.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
CSCI 171 Presentation 6 Functions and Variable Scope.
C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Week 11 Multi-file Programs and Scope of Variables.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
Lecture-3 Functions and Recursion. C Preprocessor Includes header files like stdio.h Expands macros defined Handles conditional compilations PreprocessorProcessor.
Functions Functions, locals, parameters, and separate compilation.
Functions What is a function –“sub” program, with an isolated set of statements –Accepts parameters, returns a result (perhaps) –Think of it as its own.
FUNCTIONS (CONT). Midterm questions (21-30) 21. The underscore can be used anywhere in an identifier. 22. The keyword void is a data type in C. 23. Floating.
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.
Functions Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization.
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.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Functions + Overloading + Scope
Chapter 9: Value-Returning Functions
Week 3-4 Control flow (review) Function definition Program Structures
Classes C++ representation of an object
Introduction to Programming
C Functions -Continue…-.
Functions, locals, parameters, and separate compilation
A Lecture for the c++ Course
CS1010 Programming Methodology
CS1010 Programming Methodology
Instructor: Ioannis A. Vetsikas
User-Defined Functions
Chapter 4 Procedural Methods.
Chapter 5 - Functions Outline 5.1 Introduction
Functions and Program Structure
6 Chapter Functions.
Functions and Modular Programming
Scope Rules and Storage Types
Chapter 7 Procedural Methods.
Function.
Classes C++ representation of an object
10/6: Lecture Topics C Brainteaser More on Procedure Call
Compiler vs linker The compiler translates one .c file into a .o file
Function.
Standard Version of Starting Out with C++, 4th Edition
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

Functions C and Data Structures Baojian Hua

Typical C Program Organization Program file1 function1functionm … … … filen function1functionn

Function Definition return-type function-name (argument declarations) { declarations and statements } // example: int sum (int a, int b) { int temp; temp = a + b; return temp; }

External Variables // Instead of function arguments and local // variables, we may also declare external // variables. External: not within any function. int i; int get () { return i; } void set (int a) { i = a; }

Scope Rules automatic (local) variable from the declaration point to block end extern variables; function names from the declaration point to file end “ extern ” defined somewhere, “ don ’ t care now ”

Scope Rule Example int a; // #2 extern int b; // #3 void foo () { int i; // #1 while (…) { int j; // #1 b = i+j; } extern void bar (); // #3 …; bar (); }

External Variables // file1.c int i; float j[10]; int f () { …; } // file2.c extern int i; extern float j[]; int g () { i = 9; …; } variable definitions variable declarations

Declarations and Definitions // file1.c int i; float j[10]; int f() { … } // file2.c extern int i; extern float j[]; … // file3.c extern int i; extern float j[]; …

External Variables Pros: An important way for data sharing The poor man ’ s method to build closures In future slides, we ’ ll discuss another one Also think objects in OO languages Cons: External variables blur the connections between functions and modules Involve the internal working of a linker

Static Variables By default, all external variables and functions (in all source files) are visible to all program code whether or not in same source file However, in some circumstance, we want to keep our data private Ex: visa number and passwd C provides the “ static ” mechanism

Static Variables // main.c extern int myDollar; extern void add (int a); int main () { // Ooooops! myDollar -= ; add (999999); …; } // visa.c int myDollar; int lookup () { return myDollar; } void add (int a) { myDollar += a; } // void sub (int a)

Static Variables // main.c // compiler complains… extern int myDollar; extern void add (int a); int main () { // Ooooops! myDollar -= ; add (999999); …; } // visa.c static int myDollar; int lookup () { return myDollar; } void add (int a) { myDollar += a; } // void sub (int a)

Static Variables “ Static ” can also applied to automatic variables to tie different function calls In a summary, the terminology “ static ” is a little misleading maybe “ private ” is more meaningful, just as that of C++ or Java

Initialization variable sortsautomatic init ’ user init ’ autoNOany expression extern variableYES. init ’ to 0constants “ extern ” NOForbidden

Header Files Problem with “ extern ” ? A header file group common declarations together could be included by other files typically named *.h Header file is C ’ s rudimentary module system Pros: Separate compilation Essential for linking user code with libraries Cons: flat name space

Header Files Example // area.h double area (int r); // area.c #include “area.h” double area (int r) { double pi = 3.14; double f = pi *r *r; return f; } // main.c #include “area.h” int main () { double f; f = area (5); return 0; }

Header Files Example // area.h double area (int r); // area.c #include “area.h” double area (int r) { double pi = 3.14; double f = pi *r *r; return f; } // main.c #include “area.h” int main () { double f; f = area (5); return 0; }

Conditional Inclusion // area.h #ifndef AREA_H #define AREA_H double area (int r); #endif // main.c #include “area.h” … Why these? We ’ d discuss it in future slides. // area.c #include “area.h” double area (int r) { double pi = 3.14; double f = pi *r *r; return f; }

Recursive Functions // Consider the fibnacci numbers: // { 0, if n == 0; // fib (n) = { 1, if n == 1; // { fib(n-1)+ fib(n-2), otherwise. // C code: int fib (int n) { if (0==n) return 0; else if (1==n) return 1; else return (fib(n-1) + fib(n-2)); }

Recursive Functions // Next, we crawl through this function to see // how fib(5) is computed: fib (5) = fib(4)+fib(3) = fib(3)+fib(2)+fib(3) = fib(2)+fib(1)+fib(2)+fib(3) = fib(1)+fib(0)+fib(1)+fib(2)+fib(3) = fib(1)+fib(2)+fib(3) = fib(2)+fib(3) = … = 5 // As we can see, it’s too inefficient as we are // too stupid doing much redundant computations. Exercise: design a more efficient version!