Engineering Computing I Chapter 4 Functions and Program Structure.

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.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
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.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 10: Appendices.
Functions and Program Structure Chapter 4. Introduction Functions break large computing tasks into smaller ones Appropriate functions hide details of.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
Guide To UNIX Using Linux Third Edition
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 2007 Pearson Education, Inc. All rights reserved C Preprocessor.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 19 - The Preprocessor Outline 19.1 Introduction 19.2 The #include Preprocessor Directive 19.3.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.
C++ for Engineers and Scientists Third Edition
Chapter 4 Functions and Program Structure Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering.
chap13 Chapter 13 Programming in the Large.
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: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
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.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
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.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
CPS120: Introduction to Computer Science Functions.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
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;
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.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Programming Language  C Functions and Program Structure 主講人:虞台文.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
© Oxford University Press All rights reserved. CHAPTER 10 THE PREPROCESSOR DIRECTIVE.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
C PREPROCESSOR. Introduction  It is a program that processes our source program before it is passed to the compiler.  Preprocessor commands (often known.
C language + The Preprocessor. + Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It.
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.
A First Book of ANSI C Fourth Edition
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
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.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Week 3-4 Control flow (review) Function definition Program Structures
Introduction to the C Language
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
Pre-processor Directives
Introduction to the C Language
Functions and Program Structure
Chapter 4 void 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,
C Preprocessor(CPP).
Functions and Modular Programming
Functions and Program Structure
Homework Finish up K&R Chapters 3 & 4
C Preprocessor Seema Chandak.
Functions and Program Structure
Presentation transcript:

Engineering Computing I Chapter 4 Functions and Program Structure

Functions Spring 20112Chapter 4  Functions break large computing tasks into smaller ones  Functions enable programmers to build on what others have done instead of starting over from scratch.  Appropriate functions hide details of operation from parts of the program that don’t need to know about them thus clarifying the whole, and easing the pain of making changes.  C has been designed to make functions efficient and easy to use  C programs generally consist of many small functions rather than a few big ones.

Basics of Functions Spring 2011Chapter 43 To begin with, let us design and write a program to print each line of its input that contains a particular ‘‘pattern’’ or string of characters. (This is a special case of the UNIX program grep.) For example, searching for the pattern of letters ‘‘ould’’ in the set of lines Ah Love! could you and I with Fate conspire To grasp this sorry Scheme of Things entire, Would not we shatter it to bits -- and then Re-mould it nearer to the Heart’s Desire! will produce the output Ah Love! could you and I with Fate conspire Would not we shatter it to bits -- and then Re-mould it nearer to the Heart’s Desire!

Basics of Functions Spring 2011Chapter 44

Basics of Functions Spring 2011Chapter 45

Basics of Functions Spring 2011Chapter 46

Function Definition Spring 2011Chapter 47 Minimal Function

Functions Returning Non-integers Spring 2011Chapter 48

External Variables Spring 2011Chapter 49  A C program consists of a set of external objects, which are either variables or functions.  The adjective ‘‘external’’ is used in contrast to ‘‘internal’’, which describes the arguments and variables defined inside functions.  External variables are defined outside of any function, and are thus potentially available to many functions.  Functions themselves are always external, because C does not allow functions to be defined inside other functions.  Because external variables are globally accessible, they provide an alternative to function arguments and return values for communicating data between functions.

Example of External Functions Spring 2011Chapter 410

Example of External Variables Spring 2011Chapter 411

Scope Rules Spring 2011Chapter 412 The scope of a name is the part of the program within which the name can be used Func1(int x, int y) { int a; … { for(int j=0; j<10; j++) … a = 1; } … j = 0; } Local or automatic variables: X, y, a and j Scope of local variabes x, y, and a Scope of local variabe j

Multiple Source Files Spring 2011Chapter 413 extern int d, e f; int a; void func1(args1); void func2(args2) ; extern void func3(args1); extern void func4(args2) ; main(…) { …} int b; extern int c; func1(args1) {…} int c; func2(args1) {…} int d; extern void func1(args1); extern void func2(args2) ; int e; extern int f; func3(args1) {…} int f; func4(args1) {…}

Header Files Spring 2011Chapter 414

Header Files Spring 2011Chapter 415 main.c #include #include “calc.h” #define MAXDP 100 main() { … } stack.c #include #include “calc.h” #define MAXVAL 100 Int sp = 0; Double val[MAXVAL] void push(double) … }

Register Variable Spring 2011Chapter 416 A register declaration  advises the compiler that the variable in question will be heavily used.  The idea is that register variables are to be placed in machine registers, which may result in smaller and faster programs A register declaration  advises the compiler that the variable in question will be heavily used.  The idea is that register variables are to be placed in machine registers, which may result in smaller and faster programs

Register Variable Spring 2011Chapter 417 ALU Register A Register B Register C Register D Register E Register F Memory A = A+B

Block Structure Spring 2011Chapter 418

Initialization Spring 2011Chapter 419

Initialization Spring 2011Chapter 420 Size is omitted

Initialization Spring 2011Chapter 421 Equivalent

The C Preprocessor  #include  #define  Conditional Compilation  Macros with Arguments Spring 2011Chapter 422 C provides certain language facilities by means of a preprocessor, which is conceptionally a separate first step in compilation:

File Inclusion Spring 2011Chapter 423  They include common #define statements and extern declarations and function prototypes  There are often several #include lines at the beginning of a source file  #include is the preferred way to tie the declarations together for a large program  It guarantees that all the source files will be supplied with the same definitions and variable declarations  when an included file is changed, all files that depend on it must be recompiled

Macro Substitution Spring 2011Chapter 424 #define name replacement text name = replacement text

Conditional Inclusion Spring 2011Chapter 425  It is possible to control preprocessing itself with conditional statements.  This provides a way to include code selectively, depending on the value of conditions evaluated during compilation.  The #if line evaluates a constant integer expression.  If the expression is non-zero, subsequent lines until an #endif or #elif or #else are included.

Conditional Inclusion Spring 2011Chapter 426

Conditional Inclusion Spring 2011Chapter 427 The #ifdef and #ifndef lines are specialized forms that test whether a name is defined. The first example of #if above could have been written