C and Data Structures Baojian Hua

Slides:



Advertisements
Similar presentations
CSE 303 Lecture 16 Multi-file (larger) programs
Advertisements

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
/* program to find area of a ring */ #include int main() { float a1,a2,a,r1,r2; printf("Enter the radius : "); scanf("%f",&r1); a1 = 3.14*r1*r1; printf("Enter.
FUNCTIONS Or METHODS.
C Module System C and Data Structures Baojian Hua
Functions 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 Module System C and Data Structures Baojian Hua
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Recursion!. Can a method call another method? YES.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
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.
Learners Support Publications Classes and Objects.
C Hints and Tips The preprocessor and other fun toys.
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.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Session 7 Methods Strings Constructors this Inheritance.
Lecture 05 Functions II, Storage Class, Scope, rand() METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
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.
THE PREPROCESSOR
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
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.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Functions and Program Structure CSE 2031 Fall June 2016.
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
Chapter 7: User-Defined Functions II
C Functions -Continue…-.
INC 161 , CPE 100 Computer Programming
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
Functions, locals, parameters, and separate compilation
Functions Separate Compilation
Instructor: Ioannis A. Vetsikas
Chapter 5 - Functions Outline 5.1 Introduction
6 Chapter Functions.
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
Classes and Objects.
Scope Rules and Storage Types
Function.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Classes C++ representation of an object
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:

C and Data Structures Baojian Hua bjhua@ustc.edu.cn Functions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Typical C Program Organization … file1 filen … … function1 functionm function1 functionn

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 dec (int a) i = a;

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

External Variables Pros: Cons: An important way for data sharing The poorest way 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 So, it should be used rarely, if at all

Scope Rules automatic (local) variable from the declaration point to block end extern variables; function names from the declaration point to file end “extern” extern variables in other files

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

Static Variables By default, all extern variables and functions (in all 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 visa number and passwd C provides the “static” mechanism

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

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

Static Variables “Static” can also applied to automatic variables to tie different function calls Overall, the terminology “static” is a little misleading maybe “private” is more meaningful

Initialization variable sorts automatic init’ user init’ auto NO any expression extern variable YES. init’ to 0 constants “extern” Forbidden

Header Files 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 loading libraries Cons: flat name space

Header Files Example // area.h // area.c #include “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; }

Or Extern Variable // pi.c double pi=3.14; // area.h double area (int r); // area.c #include “area.h” extern double pi; double area (int r){ double f = pi *r *r; return f; } // main.c #include “area.h” int main (){ double f; f = area (5); return 0; }

Problems // area.h #include “pi.h” double area (int r); // pi.h double pi; // main.c #include “area.h” #include “pi.h” int main (){ double f; f = area (5); return 0; } There will be errors, if some declaration appear more than once (say, structure declarations).

Conditional Inclusion // area.h #ifndef AREA_H #define AREA_H #include “pi.h” double area (int r); #endif // pi.h #ifndef PI_H #define PI_H double pi; #endif // main.c #include “area.h” #include “pi.h”

Recursive Functions // Consider the fibnacci numbers: // { 0, if n = 0; // fib n = { 1, if n = 1; // { fib(n-1)+ fib(n-2), otherwise. // The C code is: 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) // = 1 + 0 + fib(1)+fib(2)+fib(3) // = 1 + 1 + fib(2)+fib(3) // = … = 5 // As we can see, it’s too inefficient as we are // too stupid doing much redundant computations. // Exercise: is there a more efficient version?

Some More Macros