What is the scope of a function?

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

What is the scope of a function?

2,136 次查看
bigmac
Specialist III

Hello all,

 

I am using CW6.3 for HCS08 devices.

 

My project utilises the following two files (in addition to some other files):

 

// File:  SCI1.c#include "SCI.h"byte Rx_get_fifo( void);  // Fetch byte from receive buffer

 

// File:  SCI2.c#include "SCI.h"byte Rx_get_fifo( void);  // Fetch byte from receive buffer

 

The function Rx_get_fifo() is utilised from within each respective file only.  There is no declaration for Rx_get_fifo() within the header file.

 

My incorrect assumption was that the scope of the function would be limited to its own file.  However, the linker was unable to resolve, and indicated a naming conflict.

 

Solution 1:  Obviously, to use different function names.

 

Solution 2:  To declare each function as "static".  This seems unorthodox usage of the static keyword.  Most C textbooks refer this as a modifier for variables, but do not mention anything with respect to functions.

 

Perhaps someone will be able to explain why the function name did actually extend beyond file scope, without any extern declarations.  Additionally, are there any other side effects with using a "static" function?

 

Regards,

Mac

标签 (1)
标记 (1)
0 项奖励
回复
7 回复数

1,538 次查看
Lundin
Senior Contributor IV

> Most C textbooks refer this as a modifier for variables, but do not mention anything with respect to functions.

 

Sadly, most C textbooks are utter crap. Including K&R which only mentions static functions very briefly.

 

 

Using the static keyword for private functions is considered very good programming practice. Static functions are used in C to achieve the OO program design concept called "private encapsulation!", which is a 100% good thing. There are no "side-effects" of static functions, they are perfectly safe. (As opposed to the static keyword for variables, which makes functions non-reentrant.)

 

The widely-recognized industry standard MISRA-C does not only encourage the use of static but also enforces it (MISRA-C:2004 rules 8.10 and 8.11).

 

I would also recommend reading embedded guru Nigel Jones' blog:

http://embeddedgurus.com/stack-overflow/2008/12/efficient-c-tips-5-make-local-functions-static/

0 项奖励
回复

1,538 次查看
CompilerGuru
NXP Employee
NXP Employee
Remember than C #includes are mostly just doing textual include.
So for the compiler the source looks the same as if the "byte Rx_get_fifo( void);" would have been placed in a header file included by both C files.
And about the "unorthodox usage of the static keyword" part, I disagree. I would instead go for the opposite.
In C I make every function static except if there is an intention to share it. And then I expect a declaration of that function to exist in a shared header file. Any non static function declarations in a C file is "unothodox". Either make the functon static or if the function is intended to be external, move the declaration into a header file and include that header for the function definition and for every function usage.

The ColdFire compiler supports a useful switch (out of memory called something similar to "require function prototype") so it warns for every non static function definition which was not prior declared.
This with no external function declarations in C files (just in headers) allows to catch missing statics in function definitions. Too bad the S08/S12 front end does not provide that check :smileysad:.
In my point of view, "Solution 1" is not a solution at all, it just works around the missing static. The real solution is the "Solution 2" and optionally you may also rename the function, but not for the compiler,  for programmers grepping over the code :smileywink:.
Daniel
0 项奖励
回复

1,538 次查看
kef
Specialist I

Indeed CW S08/S12 compilers do allow turning any warning message, like missing prototype, into error.

0 项奖励
回复

1,538 次查看
kef
Specialist I

I would use 2nd solution and I don't think it is unorthodox. Functions are extern and global scope by default. static makes them limited to file scope. No other sideeffects AFAIK.

0 项奖励
回复

1,538 次查看
bigmac
Specialist III

Hello Kef and Rocco,

 

Thank you for your response.  Presumably it is the function name only that defaults to global scope.  Is it then true that the extern function prototype declaration is required only to identify the parameter list and return type of the particular function name?

 

I had previously assumed that the declaration was also required to "globalise" the function name, as well.

 

Regards,

Mac

 

0 项奖励
回复

1,538 次查看
kef
Specialist I

Unlike when used with variables, extern does nothing to function prototypes and function definitions. 

Yes, function prototype declares types of all function arguments and type of returned value.

0 项奖励
回复

1,538 次查看
rocco
Senior Contributor II

Hi Mac,

 

My understanding is the same as Kef's. The "static" keyword, when applied to functions, is similar to the "private" keyword in other languages. Its purpose is to limit the scope.

0 项奖励
回复