Why static function?

Posted on 2010-01-06
Last Modified: 2012-05-08
What is purpose of a staic function?  Exactly how does static function differ with regular function?  In what situation(s) static function should be defined?

We write C, C++, Assembly for Freescale ColdFire Microcontroller MCF5485.
Question by:naseeam
    LVL 32

    Assisted Solution

    by:Daniel Wilson
    In an object-oriented language, a static function may be called without instantiating the class that defines it.

    This permits it to be called with lower overhead than a non-static method of the class.  Of course, it also means that it has no access to instance data.

    Author Comment

    Thank you for a great answer but I should have done better job of asking the question.

    I meant why static functions in a .c file.  There are some static functions defined in a .c file.  Why would you define them static?  These static functions are not defined in a class, that's C++.  I am talking about 'C' Language static functions.,
    LVL 53

    Accepted Solution

    What DanielWilson said, was about static member functions.

    A regular static (non-member) function is a function that has internal linkage - ie. it is only visible in the same compilation unit where it is defined. It cannot be called from another compilation unit (as opposed to functions with external linkage).

    Author Comment

    What is a compliation unit?  Is it a file?
    LVL 53

    Expert Comment

    >> What is a compliation unit?  Is it a file?

    It roughly corresponds to a .c file (with all the headers included in it, directly or indirectly).
    It is the code that a compiler compiles into an object file. Each compilation unit results in an object file. The linker then links all these object files into the final binary (executable, or library, or ...)

    Author Closing Comment

    Great Super fast answers.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now