Chapter 4 Functions and Program Structure Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering.

Slides:



Advertisements
Similar presentations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Advertisements

Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control.
C Program Design Supplements 主講人:虞台文. Content Who takes the return value of main() ?
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.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
More about the C Preprocessor CS-2303, C-Term More about the C Preprocessor CS-2303 System Programming Concepts (Slides include materials from The.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
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.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 12 - The Preprocessor Directives (Macros)
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
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 Why we use functions C library functions Creating our own functions.
Chapter 13 C Preprocessor C How to Program, 8/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
C Program Design Supplements 主講人:虞台文. Content Who takes the return value of main() ? Using Command-Line arguments Variable-Length Argument Lists Program.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
Engineering Computing I Chapter 4 Functions and Program Structure.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 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.
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.
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.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
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.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
13 C Preprocessor.
What Is? function predefined, programmer-defined
RECURSION.
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
Preprocessor Directives
Functions Separate Compilation
Pre-processor Directives
6.11 Function Call Stack and Activation Records
Lesson #6 Modular Programming and 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).
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Programming in C Miscellaneous Topics.
Programming in C Miscellaneous Topics.
Functions and Program Structure
Lesson #6 Modular Programming and Functions.
Homework Finish up K&R Chapters 3 & 4
Functions and Program Structure
What Is? function predefined, programmer-defined
Presentation transcript:

Chapter 4 Functions and Program Structure Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

2Ku-Yaw ChangFunctions and Program Structure 4.1 Basics of Functions A program Print each line of its input that contains a particular “pattern” or string of characters Print each line of its input that contains a particular “pattern” or string of characters For example Search for the pattern of “ould” in the set of lines Search for the pattern of “ould” in the set of lines

3Ku-Yaw ChangFunctions and Program Structure 4.1 Basics of Functions 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! 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!

4Ku-Yaw ChangFunctions and Program Structure 4.1 Basics of Functions The jobs falls into three pieces: while (there’s another line) if (the line contains the pattern) print it

5Ku-Yaw ChangFunctions and Program Structure 4.1 Basics of Functions #include #include #define MAXLINE 1000 int getline(char line[], int max); int strindex(char source[], char searchfor[]); char pattern[] = “ould”; main() { char line[MAXLINE]; int found = 0; while (getline(line, MAXLINE) > 0) if (strindex(line, pattern) >= 0) { printf(“%s”, line); found ++; } return found; }

6Ku-Yaw ChangFunctions and Program Structure 4.1 Basics of Functions Each function has the form return-type function-name (argument declarations) { declarations and statements } Various parts may be absent A minimal function is dummy() {} A minimal function is dummy() {} If the return type is omitted, int is assumed. If the return type is omitted, int is assumed. The calling function is free to ignore the returned value.

7Ku-Yaw ChangFunctions and Program Structure Exercise 4-1 Write a function integerPower( base, exponent ) that returns the value of base exponent. For example, integerPower(3, 4) = 3 * 3 * 3 * 3. Assume that exponent is a positive, nonzero integer, and base is an integer. Function integerPower should use for to control the calculation. Do not use any math library functions.

8Ku-Yaw ChangFunctions and Program Structure Exercise 4-2 Write a function multiple that determines for a pair of integers whether the second integer is a multiple of the first. The function should take two integer arguments and return 1(true) if the second is a multiple of the first, and 0(false) otherwise. Use this function in a program that inputs a series of pairs of integers.

9Ku-Yaw ChangFunctions and Program Structure Exercise 4-3 Write the function strrindex(s, t), which returns the position of the rightmost occurrence of t in s, or -1 if there is none.

10Ku-Yaw ChangFunctions and Program Structure 4.2 Functions Returning Non-integers Many numerical functions return double sqrt sqrt sin sin cos cos Other specialized functions return other types A function takes no arguments Use void Use void double atof(void);

11Ku-Yaw ChangFunctions and Program Structure 4.5 Header Files A program is usually divided into several source files implementation file (*.c / *.cpp ) implementation file (*.c / *.cpp )codes header file (*.h / *.hpp) header file (*.h / *.hpp) Definitions and declarations

12Ku-Yaw ChangFunctions and Program Structure 4-10 Recursion Recursion A function may call itself either directly or indirectly A function may call itself either directly or indirectly Recursive function Base case Base case Simply return a result Recursive call or recursion step Recursive call or recursion step Call a fresh copy of itself to go to work on the smaller problem

13Ku-Yaw ChangFunctions and Program Structure 4-10 Recursion Example Factorial of a nonnegative integer n Factorial of a nonnegative integer n n ! = n * (n-1) * (n-2) * (n-3) * ….. * 1 Factorial(n) Factorial(n) Base case Factorial(0) = 1 Factorial(0) = 1 Factorial(1) = 1 Factorial(1) = 1 Recursion step Factorial(n) = n * Factorial(n-1) Factorial(n) = n * Factorial(n-1)

14Ku-Yaw ChangFunctions and Program Structure 4.11 The C Preprocessor Two most frequently used features #include #include To include the contents of a file during compilation #define #define To replace a token by an arbitrary sequence of characters

15Ku-Yaw ChangFunctions and Program Structure File Inclusion Make it easy to handle collections of #defines and declarations #include “filename” #include “filename” Searching for the file begins where the source program was found If not found there, searching follows an implementation- defined rule to find the file #include #include Searching for the file follows an implementation-defined rule

16Ku-Yaw ChangFunctions and Program Structure File Inclusion An include file may itself contain #include lines. Usually with *.h file extension Usually with *.h file extension#include Tie the declaration together for a large program Tie the declaration together for a large program The same definitions and variable declarations An include file is changed, all files that depend on it must be recompiled An include file is changed, all files that depend on it must be recompiled

17Ku-Yaw ChangFunctions and Program Structure Macro Substitution A macro substitution #define name replacement_text Subsequent occurrences of the token name will be replaced by the replacement_text Subsequent occurrences of the token name will be replaced by the replacement_textScope From its definition to the end of the source file being compiled From its definition to the end of the source file being compiled Only for tokens Not take place within quoted strings Not take place within quoted strings

18Ku-Yaw ChangFunctions and Program Structure Macro Substitution Example #define forever for (;;) /* infinite loop */ #define forever for (;;) /* infinite loop */ #define max(A, B) ((A) > (B) ? (A) : (B)) #define max(A, B) ((A) > (B) ? (A) : (B)) Look like a function call Expand into in-line code x = max(p+q, r+s) is replaced by the line x = ( (p+q) > (r+s) ? (p+q) : (r+s) ) Pitfall exists (optional assignment) #define square(x) x * x #define square(x) x * x Consider square(z+1)

19Ku-Yaw ChangFunctions and Program Structure Conditional Inclusion To make sure that the contents of a file hdr.h are included only once: #if !defined(HDR) or #ifndef HDR #define HDR /* contents of hdr.h go here */ #endif #pragma once

20Ku-Yaw ChangFunctions and Program Structure Conditional Inclusion To test the name SYSTEM to decide which version of a header to include: #if SYSTEM == SYSV #define HDR “sysv.h” #elif SYSTEM == BSD #define HDR “bsd.h” #elif SYSTEM == MSDOS #define HDR “msdos.h” #else #define HDR “default.h” #endif #include HDR

To be continued…