<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>CodeWarrior for MCU中的主题 Re: What is the scope of a function?</title>
    <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/What-is-the-scope-of-a-function/m-p/204481#M7832</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Unlike when used with variables, extern does nothing to function prototypes&amp;nbsp;and function definitions.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes, function prototype declares types of all function arguments and type of returned value.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Feb 2012 05:36:49 GMT</pubDate>
    <dc:creator>kef</dc:creator>
    <dc:date>2012-02-11T05:36:49Z</dc:date>
    <item>
      <title>What is the scope of a function?</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/What-is-the-scope-of-a-function/m-p/204477#M7828</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using CW6.3 for HCS08 devices.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My project utilises the following two files (in addition to some other files):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;// File:&amp;nbsp; SCI1.c#include "SCI.h"byte Rx_get_fifo( void);&amp;nbsp; // Fetch byte from receive buffer&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;// File:&amp;nbsp; SCI2.c#include "SCI.h"byte Rx_get_fifo( void);&amp;nbsp; // Fetch byte from receive buffer&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The function Rx_get_fifo() is utilised from within each respective file only.&amp;nbsp; There is no declaration for Rx_get_fifo() within the header file.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My incorrect assumption was that the scope of the function would be limited&amp;nbsp;to its own file.&amp;nbsp; However, the linker was unable to resolve, and indicated a naming conflict.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Solution 1:&amp;nbsp; Obviously, to use different function names.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Solution 2:&amp;nbsp; To declare each function as "static".&amp;nbsp; This seems unorthodox usage of the static keyword.&amp;nbsp; Most C textbooks refer this as a modifier for variables, but do not mention anything with respect to functions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Perhaps someone will be able to explain why the function name did&amp;nbsp;actually extend beyond file scope, without any extern declarations.&amp;nbsp; Additionally, are there any other side effects with using&amp;nbsp;a "static" function?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Mac&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 08 Feb 2012 21:51:36 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/What-is-the-scope-of-a-function/m-p/204477#M7828</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2012-02-08T21:51:36Z</dc:date>
    </item>
    <item>
      <title>Re: What is the scope of a function?</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/What-is-the-scope-of-a-function/m-p/204478#M7829</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I would use 2nd solution and I don't think it is unorthodox. Functions are extern and global scope by default. static makes them&amp;nbsp;limited to file scope.&amp;nbsp;No other sideeffects AFAIK.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 08 Feb 2012 23:01:40 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/What-is-the-scope-of-a-function/m-p/204478#M7829</guid>
      <dc:creator>kef</dc:creator>
      <dc:date>2012-02-08T23:01:40Z</dc:date>
    </item>
    <item>
      <title>Re: What is the scope of a function?</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/What-is-the-scope-of-a-function/m-p/204479#M7830</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Mac,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 09 Feb 2012 04:44:49 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/What-is-the-scope-of-a-function/m-p/204479#M7830</guid>
      <dc:creator>rocco</dc:creator>
      <dc:date>2012-02-09T04:44:49Z</dc:date>
    </item>
    <item>
      <title>Re: What is the scope of a function?</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/What-is-the-scope-of-a-function/m-p/204480#M7831</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Kef and Rocco,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your response.&amp;nbsp; Presumably it is the function name only that defaults to global scope.&amp;nbsp;&amp;nbsp;Is it then&amp;nbsp;true that the extern function prototype declaration is required only to identify the parameter list and&amp;nbsp;return type of the particular function name?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I had previously assumed that the declaration was also required to "globalise" the function name, as well.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Mac&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Feb 2012 01:48:02 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/What-is-the-scope-of-a-function/m-p/204480#M7831</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2012-02-11T01:48:02Z</dc:date>
    </item>
    <item>
      <title>Re: What is the scope of a function?</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/What-is-the-scope-of-a-function/m-p/204481#M7832</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Unlike when used with variables, extern does nothing to function prototypes&amp;nbsp;and function definitions.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes, function prototype declares types of all function arguments and type of returned value.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Feb 2012 05:36:49 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/What-is-the-scope-of-a-function/m-p/204481#M7832</guid>
      <dc:creator>kef</dc:creator>
      <dc:date>2012-02-11T05:36:49Z</dc:date>
    </item>
    <item>
      <title>Re: What is the scope of a function?</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/What-is-the-scope-of-a-function/m-p/204482#M7833</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;Remember than C #includes are mostly just doing textual include.&lt;/DIV&gt;&lt;DIV&gt;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.&lt;/DIV&gt;&lt;DIV&gt;And about the "&lt;SPAN&gt;unorthodox usage of the static keyword" part, I disagree. I would instead go for the opposite.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN style="line-height: 12px;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;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:.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;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, &amp;nbsp;for programmers grepping over the code :smileywink:.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;Daniel&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Feb 2012 03:31:51 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/What-is-the-scope-of-a-function/m-p/204482#M7833</guid>
      <dc:creator>CompilerGuru</dc:creator>
      <dc:date>2012-02-12T03:31:51Z</dc:date>
    </item>
    <item>
      <title>Re: What is the scope of a function?</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/What-is-the-scope-of-a-function/m-p/204483#M7834</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Indeed CW S08/S12 compilers do allow&amp;nbsp;turning any warning message, like missing prototype, into error.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Feb 2012 07:01:09 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/What-is-the-scope-of-a-function/m-p/204483#M7834</guid>
      <dc:creator>kef</dc:creator>
      <dc:date>2012-02-12T07:01:09Z</dc:date>
    </item>
    <item>
      <title>Re: What is the scope of a function?</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/What-is-the-scope-of-a-function/m-p/204484#M7835</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&amp;gt; Most C textbooks refer this as a modifier for variables, but do not mention anything with respect to functions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sadly, most C textbooks are utter crap. Including K&amp;amp;R which only mentions static functions very briefly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Using the static keyword for private functions is considered &lt;EM&gt;very good&lt;/EM&gt; 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.)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would also recommend reading embedded guru Nigel Jones' blog:&lt;/P&gt;&lt;P&gt;&lt;A href="http://embeddedgurus.com/stack-overflow/2008/12/efficient-c-tips-5-make-local-functions-static/" rel="nofollow" target="_blank"&gt;http://embeddedgurus.com/stack-overflow/2008/12/efficient-c-tips-5-make-local-functions-static/&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 28 Feb 2012 00:07:17 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/What-is-the-scope-of-a-function/m-p/204484#M7835</guid>
      <dc:creator>Lundin</dc:creator>
      <dc:date>2012-02-28T00:07:17Z</dc:date>
    </item>
  </channel>
</rss>

