⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chapter 10 functions -- valvano.htm

📁 介绍了在嵌入式系统中如何用c来设计嵌入式软件
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0058)http://www.ece.utexas.edu/~valvano/embed/chap10/chap10.htm -->
<HTML><HEAD><TITLE>Chapter 10: Functions -- Valvano</TITLE>
<META http-equiv=content-type content=text/html;charset=iso-8859-1>
<META content="MSHTML 5.50.3825.1300" name=GENERATOR>
<META 
content="StarMax HD:Microsoft Office 98:Templates:Web Pages:Blank Web Page" 
name=Template></HEAD>
<BODY vLink=#800080 link=#0000ff>
<P><!--Developing Embedded Software in C using ICC11/ICC12/Hiware by Jonathan W. Valvano--><B><FONT 
face=Helvetica,Arial size=4>Chapter 10: Functions</FONT></B></P>
<P><B><I><FONT face=Helvetica,Arial>What's in Chapter 10?</FONT></I></B></P>
<DIR>
<P><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap10/chap10.htm#DECLARATIONS">Function 
Declarations</A><FONT face=Monaco><BR></FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap10/chap10.htm#DEFINITIONS">Function 
Definitions</A><FONT face=Monaco><BR></FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap10/chap10.htm#CALLS">Function 
Calls</A><FONT face=Monaco><BR></FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap10/chap10.htm#ARGUMENTS">Parameter 
Passing</A><FONT face=Monaco><BR></FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap10/chap10.htm#PRIVATE">Making 
our C programs "look like" C++</A><FONT face=Monaco><BR></FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap10/chap10.htm#STACK">Stack 
frame created by ICC11 and ICC12</A><FONT face=Monaco><BR></FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap10/chap10.htm#ANIMATION">Animation 
of ICC12 function call</A><FONT face=Monaco><BR></FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap10/chap10.htm#FSM">Finite 
State Machine using Function Pointers</A><FONT face=Monaco><BR></FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap10/chap10.htm#INTERPRETER">Linked 
list interpreter</A></P></DIR>
<P><FONT face="Times New Roman,Times">We have been using functions throughout 
this document, but have put off formal presentation until now because of their 
immense importance. The key to effective software development is the appropriate 
division of a complex problem in modules. A module is a software task that takes 
inputs, operates in a well-defined way to create outputs. In C, functions are 
our way to create modules. A small module may be a single function. A 
medium-sized module may consist of a group of functions together with global 
data structures, collected in a single file. A large module may include multiple 
medium-sized modules. A hierarchical software system combines these software 
modules in either a top-down or bottom-up fashion. We can consider the following 
criteria when we decompose a software system into modules:</FONT></P>
<DIR>
<P><FONT face="Times New Roman,Times">1) We wish to make the overall software 
system easy to understand;<BR>2) We wish to minimize the coupling or 
interactions between modules;<BR>3) We wish to group together I/O port accesses 
to similar devices;<BR>4) We wish to minimize the size (maximize the number) of 
modules;<BR>5) Modules should be able to be tested independently;<BR>6) We 
should be able to replace/upgrade one module with effecting the others;<BR>7) We 
would like to reuse modules in other situations.</FONT></P></DIR>
<P><IMG height=199 src="Chapter 10 Functions -- Valvano.files/module.gif" 
width=431></P>
<ADDRESS>&nbsp;Figure 10-1: A module has inputs and outputs</ADDRESS>
<P>As a programmer we must take special case when dealing with global variables 
and I/O ports. In order to reduce the complexity of the software we will limit 
access to global variables and I/O ports. <FONT face="Times New Roman,Times">It 
is essential to divide a large software task into smaller, well-defined and easy 
to debug modules. For more information about modular programming see Chapter 2 
of the book <U>Embedded Microcomputer Systems: Real Time Interfacing</U> by 
Jonathan Valvano published by Brooks-Cole.</FONT></P>
<P><FONT face="Times New Roman,Times">The term <I>function</I> in C is based on 
the concept of mathematical functions. In particular, a mathematical function is 
a well-defined operation that translates a set of input values into a set of 
output values. In C, a function translates a set of input values into a single 
output value. We will develop ways for our C functions to return multiple output 
values and for a parameter to be both an input and an output parameter. As a 
simple example consider the function that converts temperature in degrees F into 
temperature in degrees C.</FONT></P>
<DIR>
<P><CODE>int FtoC(int TempF){&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int 
TempC;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TempC=(5*(TempF-32))/9;&nbsp;&nbsp;&nbsp;// 
conversion<BR>return TempC;}</CODE></P></DIR>
<P><FONT face="Times New Roman,Times">When the function's name is written in an 
expression, together with the values it needs, it represents the result that it 
produces. In other words, an operand in an expression may be written as a 
function name together with a set of values upon which the function operates. 
The resulting value, as determined by the function, replaces the function 
reference in the expression. For example, in the expression</FONT></P>
<DIR>
<P><CODE>FtoC(T+2)+4;&nbsp;&nbsp;&nbsp;&nbsp;// T+2 degrees Fahrenheit plus 4 
degrees Centigrade<BR></CODE></P></DIR>
<P><FONT face="Times New Roman,Times">the term<B> FtoC(T+2)</B> names the 
function <B>FtoC</B> and supplies the variable<B> T </B>and the constant <B>2 
</B>from which <B>FtoC</B> derives a value, which is then added to <B>4</B>. The 
expression effectively becomes</FONT></P>
<UL>
  <P><CODE>((5*((T+2)-32))/9)+4;&nbsp;&nbsp;&nbsp;</CODE></P></UL>
<P><FONT face="Times New Roman,Times">Although <B>FtoC(T+2)+4</B> returns the 
same result as <B>((5*((T+2)-32))/9)+4</B>, they are not identical. As will we 
see later in this chapter, the function call requires the parameter <B>(T+2)</B> 
to be passed on the stack and a subroutine call will be executed.</FONT></P>
<P><B><I><FONT face=Helvetica,Arial><A name=DECLARATIONS></A>Function 
Declarations</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">Similar to the approach with variables, C 
differentiates between a function declaration and a function definition. A 
declaration specifies the syntax (name and input/output parameters), whereas a 
function definition specifies the actual program to be executed when the 
function is called. Many C programmers refer to function declaration as a 
prototype. Since the C compiler is essential a one-pass process (not including 
the preprocessor), a function must be declared (or defined) before it can be 
called. A function declaration begins with the type (format) of the return 
parameter. If there is no return parameter, then the type can be either 
specified as <B>void</B> or left blank. Next comes the function name, followed 
by the parameter list. In a function declaration we do not have to specify names 
for the input parameters, just their types. If there are no input parameters, 
then the type can be either specified as <B>void</B> or left blank. The 
following examples illustrate that the function declaration specifies the name 
of the function and the types of the function parameters.</FONT></P>
<P><CODE>//&nbsp;&nbsp;declaration&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;input 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; output<BR>void 
Ritual(void);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
none&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; none&nbsp;<BR>char 
InChar(void);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
none&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;8-bit<BR>void 
OutChar(char);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
8-bit &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;none<BR>short 
InSDec(void);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
none&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; 16-bit<BR>void 
OutSDec(short);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
16-bit &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; none<BR>char 
Max(char,char);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
two 8-bit &nbsp; &nbsp; &nbsp; &nbsp;8-bit<BR>int 
EMax(int,int);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
two 16-bit &nbsp; &nbsp; &nbsp; 16-bit<BR>void 
OutString(char*);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
pointer to 8-bit none<BR>char 
*alloc(int);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
16-bit &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pointer to 8-bit<BR>int 
Exec(void(*fnctPt)(void));&nbsp;&nbsp;// function pointer 
16-bit<BR><BR></CODE></P>
<P><FONT face="Times New Roman,Times">Normally we place function declarations in 
the header file. We should add comments that explain what the function 
does.</FONT></P>
<DIR>
<P><CODE>void InitSCI(void);&nbsp;&nbsp;&nbsp;// Initialize 38400 
bits/sec<BR>char InChar(void);&nbsp;&nbsp;&nbsp;&nbsp;// Reads in a character, 
gadfly <BR>void OutChar(char);&nbsp;&nbsp;&nbsp;// Output a character, 
gadfly<BR>char UpCase(char);&nbsp;&nbsp;&nbsp;&nbsp;// Converts lower case 
character to upper case<BR>void InString(char *, unsigned int); // Reads in a 
String of max length</CODE></P></DIR>
<P><FONT face="Times New Roman,Times">To illustrate some options when declaring 
functions, we give alternative declarations of these same five 
functions:</FONT></P>
<DIR>
<P><CODE>InitSCI();<BR>char InChar(); <BR>void OutChar(char letter);<BR>char 
UpCase(char letter);<BR>InString(char *pt, unsigned int MaxSize); 
</CODE></P></DIR>
<P><FONT face="Times New Roman,Times">Sometimes we wish to call a function that 
will be defined in another module. If we define a function as external, software 
in this file can call the function (because the compiler knows everything about 
the function except where it is), and the linker will resolve the unknown 
address later when the object codes are linked.</FONT></P>
<DIR>
<P><CODE>extern void InitSCI(void);&nbsp;<BR>extern char InChar(void);&nbsp; 
<BR>extern void OutChar(char);&nbsp;<BR>extern char 
UpCase(char);&nbsp;&nbsp;<BR>extern void InString(char *, unsigned int); 
</CODE></P></DIR>
<P><FONT face="Times New Roman,Times">One of the power features of C is to 
define pointers to functions. A simple example follows:</FONT></P>
<DIR>
<P><CODE>int (*fp)(int);&nbsp;&nbsp;// pointer to a function with input and 
output<BR>int fun1(int 
input){<BR>&nbsp;&nbsp;&nbsp;return(input+1);&nbsp;&nbsp;&nbsp;&nbsp;// this 
adds 1<BR>};<BR>int fun2(int 
input){<BR>&nbsp;&nbsp;&nbsp;return(input+2);&nbsp;&nbsp;&nbsp;&nbsp;// this 
adds 2<BR>};<BR>void Setp(void){ int 
data;<BR>&nbsp;&nbsp;&nbsp;fp=&amp;fun1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
fp points to fun1<BR>&nbsp;&nbsp;&nbsp;data=(*fp)(5);&nbsp;// 
data=fun1(5);<BR>&nbsp;&nbsp;&nbsp;fp=&amp;fun2;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
fp points to fun2<BR>&nbsp;&nbsp;&nbsp;data=(*fp)(5);&nbsp;// 
data=fun2(5);<BR>};</CODE></P></DIR>
<ADDRESS>Listing 10-1: Example of a function pointer</ADDRESS>
<P><FONT face="Times New Roman,Times">The declaration of <B>fp</B> looks a bit 
complicated because it has two sets of parentheses and an asterisk. In fact, it 
declares <B>fp</B> to be a pointer to any function that returns integers. In 
other words, the line <B>int (*fp)(int);</B> doesn't define the function. As in 
other declarations, the asterisk identifies the following name as a pointer. 
Therefore, this declaration reads "<B>fp</B> is a pointer to a function with a 
16-bit signed input parameter that returns a 16-bit signed output parameter." 
Using the term <I>object</I> loosely, the asterisk may be read in its usual way 
as "object at." Thus we could also read this declaration as "the object at 
<B>fp</B> is a function with an <B>int</B> input that returns an 
<B>int</B>."</FONT></P>
<P><FONT face="Times New Roman,Times">So why the first set of parentheses? By 
now you have noticed that in C declarations follow the same syntax as references 
to the declared objects. And, since the asterisk and parentheses (after the 
name) are expression operators, an evaluation precedence is associated with 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -