In
the CSCI1110, chapter 7 –function was taught from week 7 to week 8. This is one of the key topics in the curriculum of CSCI1110. Now let me summarize what I have learnt from chapter 7 –function.
Part 1
1.1 Contents of the lectures
In chapter 7, I have learnt some fundamental
concepts about C functions. What are C functions? Modules in C are called functions. C programs are typically written by
combining new functions you write with prepackaged functions available in the C
standard library. The C standard library provides a rich collection of
functions for performing common mathematical calculations, string
manipulations, character manipulations, inputs/outputs, and many other useful
operations. This makes the design of the program easier since these functions
provide many of the capabilities programmer needs. Therefore, it is very
important for a good programmer to acquire a good skill of using C functions. In chapter 7, the knowledge of C functions I had learnt include
(1) function definitions, (2) function
prototypes, (3) pre-defined function.
(1) Function definitions :
- The general format for a function definition is
Return-value-type function-name(
parameter-list)
{
Definitions
Statements
}
- The return-value-type states the type of the value returned to the calling function. If a function does not return a value, the return-value-type is declared as void.
- The function-name is any valid identifier.
- The parameter-list is a comma-separated list containing the definitions of the variables that will be passed to the function. If a function dose not receive any value, parameter-list is declared as void.
- The function body is the set of definitions and statements that constitute the function.
- The arguments passed to a function should match in number, type and order with the parameters in the function definition.
- When a program encounter a function call, control is transferred from the point of invocation to the called function, the statements of the called function are executed and control returns to the caller.
- A called function can return control to the caller in one of three ways. If the function does not return a value, control is returned when the function-ending right brace is reached, or by executing the statement return ;
If the function does return a value, the
statement return
expression; returns
the value of expression.
(2) Function
Prototypes :
- A function prototype declares the function’s return type and declares the number, types, and order of the parameters the function expects to receive.
- Function prototype enables the compiler to verify the functions are called correctly.
- The compiler ignores variable names mentioned in the function prototype.
- Argument in a mixed-type expression are converted to the same type via the C standard’s usual arithmetic conversion rules.
(3) Pre-defined function :
- C provides many built-in functions. To call them, we have to know the following info (which can be found from manuals): name, functionality, parameters, return value
- You also need to know which header file(s) to include. E.g.
#include <stdio.h>
-To use math functions, you have to include "math.h" as
#include <math.h>
More about C function :
Website :
http://www2.its.strath.ac.uk/courses/c/section3_9.html
Video:
1.2 Learning Process:
In order to understand the principle of C
functions and acquire the techniques
of using different functions for performing useful operations, I use different methods to let
myself to understand the principle and uses of the techniques during the
learning process.
(1)
To understand the principle of C functions in the view of
worker-function
relationship
The principle of C functions is that, functions are invoked by a function call, which specifies the
function name and provides information (as arguments) that the function needs
to perform its designated task.
However,
this concept is quite abstract and difficult to understand.
In order
to understand the principle of C functions clearly, I regard the
relationship between the caller and the called function as a worker-function
relationship :
A boss
(the caller) asks a worker (the called function) to perform a task and report
back when the task is done. For example, a function needing to display
information on the screen calls the worker function printf to perform that
task, then printf display information and reports back – or returns – to the
caller when its task is completed. The boss function does not know how the
worker functions, and the boss will be unware of this.
Figure
showing a boss function communicating with several worker functions in a
hierarchical manner.
(2) Record the common programming error
in using C functions.
Since the use of C functions is complex, we may make some programming
errors easily in using C function. In acquire the techniques of using different
functions for performing useful operations accurately without mistakes or errors, I recorded the common programming error in using C functions and
some error-prevention skills during the learning process.
- Specifying function parameters of the same type as double x,y instead of double x, double y results in a compilation error.
- Placing a semicolon after the right parenthesis enclosing the parameter list of a function definition is a syntax error.
- Defining a parameter again as a local variable in a function is a compilation error.
- Defining a function inside another function is a syntax error.
- Forgetting the semicolon at the end of a function prototype is a syntax error.
- Converting from a higher data type in the promotion hierarchy to a lower type can change the data value. Many compilers issue warnings in such cases.
Part 2
2.1 A interesting topic in
the lesson – math library function in C program
A brief description about the math library function in C program :
C mathematical operations are a group of functions in the standard library of the C programming language implementing basic mathematical functions.
Math library functions allow programmer to perform certain common mathematical calculations. It is normally used in a program by writing the name of the function followed by left parenthesis followed by the argument of the function followed by a right parenthesis.
Most of the mathematical functions are placed in
math.h
header (cmath
header in C++). The functions that operate on integers, such as
abs
, labs
, div
, and ldiv
, are instead specified in the stdlib.h
header (cstdlib
header in C++).Any functions that operate on angles use radians as the unit of angle.
More about C math library :
2.2 My opinion, commends, ideas, and further information about math library function in C program
I think Math library function in C program is a very
useful tool for solving mathematics or engineering problem in real world. By
calling the Math library function in C program, we can perform some complex
mathematics calculation or solve some difficult mathematics problem in an
easier way.
In the
below, there are some advanced examples about use of Math library function that
are not included in the lecture.
For example,
we can use log2( ) function to convert numbers in the base 10 to the base 2
easily.
#include <math.h>
#include <stdio.h>
int main(void)
{
double val = 1.0; do {
printf("%f %f\n", val, log2(val));
val++;
} while (val<11.0); return 0;
}
#include <stdio.h>
int main(void)
{
double val = 1.0; do {
printf("%f %f\n", val, log2(val));
val++;
} while (val<11.0); return 0;
}
For the
above C program using log2( ) function in the Math library, the following result
can be obtained.
Apart from it, the Math library function in C program can
also perform more complex mathematics calculation. For example, we may use it
to create a matrix, which is commonly used in solving engineering problems.
To create a sparse matrix froma
standard numeric array, we may use the mlfSparse()
routine.
mlfSparse() converts the numeric array into sparse storage format.
#include <math.h>
#include <stdio.h>
int main(void)
{
#include <stdio.h>
int main(void)
{
static
double row_subscripts[] = { 3, 4, 5, 4, 5, 6 };
static
double col_subscripts[] = { 4, 3, 3, 5, 5, 4 };
mxArray *i =
NULL;
mxArray *j =
NULL;
mxArray *S =
NULL;
mlfAssign(&i,
mlfDoubleMatrix(1, 6, row_subscripts, NULL));
mlfAssign(&j,
mlfDoubleMatrix(1, 6, col_subscripts, NULL));
mlfAssign(&S,
mlfSparse(i, /* Row subscripts */
j, /* Column
subscripts */
mlfScalar(9),
/* Data */
mlfScalar(8),
mlfScalar(7),
NULL));
mlfPrintMatrix(S);
mlfPrintMatrix(mlfFull(S));
mxDestroyArray(i);
mxDestroyArray(j);
mxDestroyArray(S);
return 0;
}
}
For the
above C program using mlfSparse(C math library reference), the
following result can be obtained.
In fact,
the C Math Library is commonly used
as a component in different mathematics or engineering softwares.Fr example, the
C Math Library is a component of the MATLAB Compiler. The MATLAB Compiler
includes the Compiler, the C Math Library and the Graphics Library. The Math
library provides the core math and data analysis routines from MATLAB in object
library form. We can use the Math library with the MATLAB Compiler to create
math functions based on MATLAB in your external applications ,or embed the Math
library in standalone applications.
From these examples, we can see
that Math library
function is a very useful function in C program. It helps engineer to solve difficult
and complex engineering mathematics problem in the real applications. As a
engineering student, I must acquire the techniques of using this function for solving
engineering problems.
More about C mathematical function :
http://en.wikipedia.org/wiki/C_mathematical_functions
More about C mathematical function :
http://en.wikipedia.org/wiki/C_mathematical_functions
In the coming lecture, I will learn more advanced
knowledge about C program. I will continue to make effort on writing C program
and hope that I can acquire a good skill of writing C
program at the end of this semester.
沒有留言:
張貼留言