Professional Documents
Culture Documents
: Programming Fundamentals
Email: 18042271r@connect.polyu.hk
Tutorial 3
Program Module.
Math Library.
Functions.
Function Definition
Function Prototype.
Function: Passing By Value Or By Reference.
Header Files.
Random Numbers.
Random Number Generation.
Program Module
For example:
printf(“%f”, exp(1))
Equivalent to e1 = 2.718.
sqrt(x) square root of x sqrt(900.0) is 30.0 floor(x) rounds x to the largest integer not floor(9.2) is 9.0
sqrt(9.0) is 3.0 greater floor(−9.8) is −10.0
than x
c brt(x) cube root of x (C99 and C11 only) cbrt(27.0) is 3.0
cbrt(−8.0) is −2.0
pow(x, y) x raised to power y Left parenthesis x to the power y right parenthesis. pow(2, 7) is 128.0
exp(x) exponential function E to the power x. exp(1.0) is 2.718282 pow(9, .5) is 3.0
exp(2.0) is 7.389056
fmod(x, y) remainder of as aStart fraction x over y end fraction. fmod(13.657, 2.333) is
log(x) natural logarithm of x (base e) log(2.718282) is 1.0 floating-point number 1.992
log(7.389056) is 2.0
log10(x) logarithm of x (base 10) log10(1.0) is 0.0 sin(x) trigonometric sine of x (x in sin(0.0) is 0.0
log10(10.0) is 1.0 radians)
log10(100.0) is 2.0
fabs(x) absolute value of x as a floating-point fabs(13.5) is 13.5 cos(x) trigonometric cosine of x (x cos(0.0) is 1.0
number fabs(0.0) is 0.0 in radians)
fabs(−13.5) is 13.5
tan(x) trigonometric tangent of x (x tan(0.0) is 0.0
c eil(x) rounds x to the smallest integer not ce il(9.2) is 10.0
less than x ce il(−9.8) is −9.0 in radians)
Functions
• Functions
• Modularize a program
• Software reusability
• Call function multiple times
• Local variables
• Known only in the function in which they are defined
• All variables declared in function definitions are local variables
• Parameters
• Local variables passed to function when called
• Provide outside information
Function Definition
Function prototype
Tells compiler argument type and return type of function
int square( int );
Function takes an int and returns an int
Explained in more detail later
Calling/invoking a function
square(x);
Parentheses an operator used to call function
Pass argument x
Function gets its own copy of arguments
After finished, passes back result
Function Definition
Example function
int square( int y )
{
return y * y;
}
return keyword
Returns data, and control goes to function’s caller
If no data to return, use return;
Function ends when reaches right brace
Control goes to caller
Functions cannot be defined inside other functions
Function Definition: Example
In many programming languages, there are two ways to pass arguments—pass-by-value and
pass-by-reference.
When arguments are passed by value, a copy of the argument’s value is made and passed to
the called function.
Changes to the copy do not affect an original variable’s value in the caller.
When an argument is passed by reference, the caller allows the called function to modify the
original variable’s value.
Pass-by-value should be used whenever the called function does not need to modify the value
of the caller’s original variable.
Header Files
(a) (b)
(c) (d)
2. First, write a function min that takes two inputs, i.e., integers (int), to find the minimum of two numbers. Next,
write the main() function that does the following:
Initialize two int variables.
Pass them to the min function.
Return the minimum of two numbers.
Print that number.
Prototype: int min(int, int);
3. To determine whether a year is a leap year or not is a bit tricky. We usually assume that if the year number is
evenly divisible by 4, it is a leap year. But it is not the only case. A year is a leap year if −
It is evenly divisible by 100
If it is divisible by 100, then it should also be divisible by 400.
Except this, all other years evenly divisible by 4 are leap years.
Write a function isLeapYear, pass any year to it, and return 1 (if leap year), otherwise, return 0(not leap year).
Prototype: int isLeapYear(int);
4. A proper factor of a number is any factor of the number except the number itself. For example, 6 has factors 1,
2, 3, and 6. The proper factors of 6 are 1, 2, and 3.
Write a function isProperFactor(), that determines if a given integer is a proper factor of another given integer:
For example, output to the screen if 3 is a proper factor of 6, using the following codes:
printf("%d a proper factor of %d? %d\n", 3, 6, isProperFactor(6, 3));
Prototype: int isProperFactor(int, int);