Download presentation
Presentation is loading. Please wait.
Published byBuck Simon Modified over 9 years ago
1
Chapter 4 Functions and Program Structure Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University
2
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
3
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!
4
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
5
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; }
6
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.
7
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.
8
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.
9
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.
10
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);
11
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
12
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
13
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)
14
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
15
15Ku-Yaw ChangFunctions and Program Structure 4.11.1 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
16
16Ku-Yaw ChangFunctions and Program Structure 4.11.1 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
17
17Ku-Yaw ChangFunctions and Program Structure 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 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
18
18Ku-Yaw ChangFunctions and Program Structure 4.11.2 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)
19
19Ku-Yaw ChangFunctions and Program Structure 4.11.3 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
20
20Ku-Yaw ChangFunctions and Program Structure 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
21
To be continued…
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.