Code Examples
  Home arrow Code Examples arrow Page 7 - Programming in C
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
CODE EXAMPLES

Programming in C
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 37
    2005-03-02

    Table of Contents:
  • Programming in C
  • Linking
  • Try It Out: An Example C Program
  • Dealing with Errors
  • Keywords
  • Developing Programs in C
  • Functions and Modular Programming
  • Try It Out: Exercising What You Know
  • Common Mistakes

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Programming in C - Functions and Modular Programming


    (Page 7 of 9 )

    The word “function” has appeared a few times so far in this chapter with reference to main(), printf(), function body, and so on. Let’s explore in a little more depth what functions are and why they’re important.

    Most programming languages, including C, provide a way of breaking up a program into segments, each of which can be written more or less independently of the others. In C these segments are called functions. The program code in the body of one function is completely insulated from that of other functions. A function will have a specific interface to the outside world in terms of how information is transferred to it and how results generated by the function are transmitted back from it. This interface is specified in the first line of the function, where the function name appears.

    Figure 1-3 shows a simple example of a program to analyze baseball scores that is composed of four modules.


    Figure 1-3.  Modular programming

    Each of the four modules does a specific, well-defined job. Overall control of the sequence of operations in the program is managed by one module, main(). There is a module to read and check the input data and another module to do the analysis. Once the data has been read in and analyzed, a fourth module has the task of outputting the team and player rankings.

    Segmenting a program into manageable chunks is a very important aspect to programming, so let’s go over the reasons for doing this:

    • Dividing the program into a number of separate functions allows each function to be written and tested separately. This greatly simplifies the process of getting the total program to work.

    • Several separate functions are easier to handle and understand than one huge function.

    • Libraries are just sets of functions that people tend to use all the time. Because they’ve been prewritten and pretested, you know they’ll work, so you can use them without worrying about their code details. This will accelerate your program development, by allowing you to concentrate on your own code, and it’s a fundamental part of the philosophy of C. The richness of the libraries greatly amplifies the power of the language.

    • You can accumulate your own libraries of functions that are applicable to the sorts of programs that you’re interested in. If you find yourself writing a particular function frequently, you can write a generalized version of it to suit your

    18 needs and build this into your own library. Then, whenever you need to use that particular function, you can simply use your library version.

    •In the development of very large programs, which can vary from a few thousand to millions of lines of code, development can be undertaken by teams of programmers, with each team working with a defined subgroup of the functions that make up the whole program.

    You’ll learn about C functions in greater detail in Chapter 8. Because the structure of a C program is inherently functional, you’ve already encountered one of the standard library functions in one of this chapter’s earliest examples: the function printf(). 

    This article is excerpted from Beginning C by Ivor Horton (Apress, 2004; ISBN 1590592530). Check it out at your favorite bookstore today. Buy this book now.

    More Code Examples Articles
    More By Apress Publishing


       · Has anyone looked over this article before posting? Both command line examples and...
       · The code was correct, the CMS's editor converted the <stdio.h> into HTML and erased...
     

    CODE EXAMPLES ARTICLES

    - Bipartite Graphs
    - Connectivity in Graphs
    - The Ford-Fulkerson Algorithm
    - Critical Paths
    - The Bellman-Ford and Roy-Floyd Algorithms
    - Shortest Path Algorithms in Graphs
    - Minimum Spanning Tree
    - Articulation Edges and Vertexes
    - Circles and Connectivity in Graphs
    - Depth-First Search in Graphs
    - Breadth-First Search in Graphs
    - The Prufer Code and the Floyd-Warshall Algor...
    - An Insight into Graphs
    - Coding a Custom Object with WSC
    - Creating a Custom Object with WSC





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek