G3-1 University of Washington Computer Programming I Structuring Program Files © 2000 UW CSE.

Slides:



Advertisements
Similar presentations
Spring Semester 2013 Lecture 5
Advertisements

1 ICS103 Programming in C Lecture 5: Introduction to Functions.
1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014.
Structure of a C program
B-1 Lecture 2: Problems, Algorithms, and Programs © 2000 UW CSE University of Washington Computer Programming I.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Chapter 6: User-Defined Functions I
Guide To UNIX Using Linux Third Edition
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
V-1 University of Washington Computer Programming I File Input/Output © 2000 UW CSE.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
B. RAMAMURTHY CH.4 IN KERNIGHAN AND RITCHIE C TEXTBOOK C Language: Functions 5/11/2013 Amrita-UB-MSES
Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
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.
Chapter 6: User-Defined Functions
Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.
EPSII 59:006 Spring Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.
Sharda University P. K. Mishra (Asst.Prof) Department of Computer Science & Technology Subject Name: Programming Using C Sub Code: CSE-106 Programming.
Computer Programming I Hour 2 - Writing Your First C Program.
C Tutorial - Program Organization CS Introduction to Operating Systems.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
142 F -1 Functions chapter 3 of the text int main(void) { double x,y,z; … x = cube(y/3.0); … printf(“%f cubed is %f”,x,cube(x)); … return 0; } double cube(double.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
CPS120: Introduction to Computer Science Functions.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
Functions. Let’s look at the 2 programs of evaluating: y=2^3+2^5+2^6; #include using namespace std; int main() { int y=0; int partResult=1; for (int i=1;
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
1 CHAPTER 3 MODULAR PROGRAMMING. 2 Introduction  A library in C is a collection of general purpose and related functions.  2 types of libraries: Standard.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
9/29/99B-1 CSE / ENGR 142 Programming I Variables, Values, and Types © 1998 UW CSE.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Function PrototypetMyn1 Function Prototype We can declare a function before we use or define it by means of a function prototype. A function prototype.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
Basics of Computer A Computer is Electronic device that can
User-Written Functions
Chapter 6: User-Defined Functions I
C Functions Pepper.
University of Washington Computer Programming I
What's a Computer? Monitor Disk Main mouse Memory Keyboard Network
Introduction to C Programming Language
Functions Inputs Output
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
Functions I Creating a programming with small logical units of code.
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
Functions Chapter 3 of the text Motivation:
Functions.
Chapter 6: User-Defined Functions I
Functions, Part 1 of 3 Topics Using Predefined Functions
Compiler vs linker The compiler translates one .c file into a .o file
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions I Creating a programming with small logical units of code.
CPS125.
Scope Rules.
Presentation transcript:

G3-1 University of Washington Computer Programming I Structuring Program Files © 2000 UW CSE

G3-2 Structuring Programs The function is the basic unit of a C program Programs often use many functions Some are defined within the program Some are in libraries Organizing and ordering the functions and other parts within the.c file is important

G3-3 Order in the Program General principle: identifiers (names of things) must be declared before they are used. Variables: place them first within each function #define constants: placed at the top of the.c file What about functions?

G3-4 Order for Functions in the.c File Function names are identifiers, so… they too must be declared before they are used: #include voidfun2(void){... } voidfun1(void){...; fun2();... } intmain(void){...; fun1();... return 0; } fun1 calls fun2, so fun2 is defined before fun1, etc.

G3-5 Function Prototypes Insisting that all the code of each function precede all calls to that function is sometimes: Impossible: function A calls B, and B calls A Inconvenient: printf() is a function, but we don’t want its code in our program But the ordering rule requires that the function names be declared before they can be used (in a call). Is there any solution?

G3-6 Solution: Function Prototypes Function prototypes allow us to define the name, so that it can be used, without giving the code for the function. The prototype gives the function name, return type, and the types of all the parameters but no code. In place of the { } code block, there is a semicolon.

G3-7 Example Function Prototypes void Useless(void); void PrintInteger(int value); double CalculateTax (double amount, double rate);

G3-8 Using Prototypes Write prototypes for your functions near the top of the program Can use the function anywhere thereafter Fully define the function later, wherever convenient Highly recommended to aid program organization

G3-9 Library Functions What about library functions, like printf? You must also tell the compiler that you are going to use the library which contains printf This is the purpose of the #include directive The linker knows where the libraries are

G3-10 #include The “#include” means “go get the file stdio.h and insert what’s in it right here (as if it had been typed here)” stdio.h contains function prototypes for scanf and printf and the other functions in the standard I/O library The actual code for them is NOT there, just prototypes. The (result of compiling) the code is in a library that is combined with your code by the linker

G3-11 Compilers, Linkers, etc. library (ANSI) header (stdio.h) executable program compilercompiler linkerlinker source code object code. c file

G3-12 Putting it All Together #include directives … #define constants … Function prototypes … Full function definitions …

G3-13 Logical Order vs. Control Flow With prototypes, the functions can be placed in any physical order Order within the source file has no influence on control flow Programs always start at the function main So there should always be a main No function is executed until it is called by some other function Only exception: main

G3-14 Summary Organizing the parts of a.c file is important General principle: identifiers must be declared before they are used For functions, a prototype can be declared near the beginning of the program Function can be defined in detail later For libraries, mention the library name in a #include directive Source order and control flow are different concepts