Operating System Concepts

Download Report

Transcript Operating System Concepts

Chapter 4
Functions and Program Structure
Ku-Yaw Chang
[email protected]
Assistant Professor, Department of
Computer Science and Information Engineering
Da-Yeh University
4.1 Basics of Functions
A program

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
Ku-Yaw Chang
Functions and Program Structure
2
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!
Ku-Yaw Chang
Functions and Program Structure
3
4.1 Basics of Functions
The jobs falls into three pieces:
while (there’s another line)
if (the line contains the pattern)
print it
Ku-Yaw Chang
Functions and Program Structure
4
4.1 Basics of Functions
#include <stdio.h>
#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;
}
Ku-Yaw Chang
Functions and Program Structure
5
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() {}
If the return type is omitted, int is assumed.
The calling function is free to ignore the returned value.
Ku-Yaw Chang
Functions and Program Structure
6
Exercise 4-1
Write a function integerPower( base, exponent ) that
returns the value of baseexponent.
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.
Ku-Yaw Chang
Functions and Program Structure
7
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.
Ku-Yaw Chang
Functions and Program Structure
8
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.
Ku-Yaw Chang
Functions and Program Structure
9
4.2 Functions Returning
Non-integers
Many numerical functions return double



sqrt
sin
cos
Other specialized functions return other types
A function takes no arguments

Use void
double atof(void);
Ku-Yaw Chang
Functions and Program Structure
10
4.5 Header Files
A program is usually divided into several source
files

implementation file (*.c / *.cpp )
codes

header file (*.h / *.hpp)
Definitions and declarations
Ku-Yaw Chang
Functions and Program Structure
11
4-10 Recursion
Recursion

A function may call itself either directly or indirectly
Recursive function

Base case
Simply return a result

Recursive call or recursion step
Call a fresh copy of itself to go to work on the smaller
problem
Ku-Yaw Chang
Functions and Program Structure
12
4-10 Recursion
Example

Factorial of a nonnegative integer n
n ! = n * (n-1) * (n-2) * (n-3) * ….. * 1

Factorial(n)
Base case


Factorial(0) = 1
Factorial(1) = 1
Recursion step

Ku-Yaw Chang
Factorial(n) = n * Factorial(n-1)
Functions and Program Structure
13
4.11 The C Preprocessor
Two most frequently used features

#include
To include the contents of a file during compilation

#define
To replace a token by an arbitrary sequence of
characters
Ku-Yaw Chang
Functions and Program Structure
14
4.11.1 File Inclusion
Make it easy to handle collections of #defines
and declarations

#include “filename”
Searching for the file begins where the source program was
found
If not found there, searching follows an implementationdefined rule to find the file

#include <filename>
Searching for the file follows an implementation-defined rule
Ku-Yaw Chang
Functions and Program Structure
15
4.11.1 File Inclusion
An include file may itself contain #include lines.

Usually with *.h file extension
#include

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
Ku-Yaw Chang
Functions and Program Structure
16
4.11.2 Macro Substitution
A macro substitution
#define name replacement_text
 Subsequent occurrences of the token name will be
replaced by the replacement_text
Scope

From its definition to the end of the source file being
compiled
Only for tokens

Not take place within quoted strings
Ku-Yaw Chang
Functions and Program Structure
17
4.11.2 Macro Substitution
Example


#define forever for (;;) /* infinite loop */
#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
Consider square(z+1)
Ku-Yaw Chang
Functions and Program Structure
18
4.11.3 Conditional Inclusion
To make sure that the contents of a file hdr.h are
included only once:
#if !defined(HDR)
#define HDR
or
#ifndef HDR
/* contents of hdr.h go here */
#endif
#pragma once
Ku-Yaw Chang
Functions and Program Structure
19
4.11.3 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
Ku-Yaw Chang
Functions and Program Structure
20
To be continued…