<NOSCRIPT><A href="http://about.com/" target=_blank><IMG src="C++ Tutorial - Lesson 14 Functions, Basics_files/0.gif" border=0></A></NOSCRIPT>   About > Computing & Technology > C/C++ 
Search     
 Hi, I'm John Kopp, your guide to C, C++ and C# programming. This site provides what you need to know to learn C, C++ or C# programming. Browse the subjects on the left, try one of the tutorials or use the search box to get started.

C/C++

with John Kopp
Your Guide to one of hundreds of sites
 Home · Articles · Forums · Chat · Newsletters · Help    
Subjects

  ESSENTIALS
· C Tutorial
· C++ Tutorial
· C/C++ Glossary
· Tips
· More Tutorials
  BUYER'S GUIDE

Product Reviews
Top Picks
C Tutorials
C++ Tutorials
C#
Advanced C
Advanced C++
Beginning C
Beginning C++
Books
C++ Builder
Careers
CGI
CM
Dictionaries
CompilingDebugging
Freeware/Shareware
Humor
Magazines
OOAD
Polls
SoftwareEngineer'n
Style
STL
UNIX/GNU
Visual C++

Subject Library

All articles on this topic

 

Stay up-to-date!
Subscribe to our newsletter.

Web Hosting
Global Servers

 
 
 
Advertisement
> Free Credit Report
 
C++ Tutorial - Lesson 14: Functions, Basics
 More of this Feature
• Declaring and Defining Functions
• Using Functions
• Returning Multiple Values From Functions
• Returning Multiple Values From Functions, Continued
• Passing Arrays to Functions
• Default Parameters
 
 Related Resources
• C++ Tutorial - Introduction to Pointers
• C++ Tutorial - References
• C++ Tutorial - Arrays and Vectors
• Advanced C++ Tutorials
• Simple Solutions in C++
• C++ Programming Tips
• All C++ Lessons
 

by John Kopp

Welcome to About.com's free tutorial on C++ programming. This lesson covers functions. Functions are used to encapsulate a set of operations and return information to the main program or calling routine. Encapsulation is data, information or detail hiding. Once a function is written, we need only be concerned with what the function does. That is, what data it requires and what outputs it produces. The details, "how" the function works, need not be known. We have also seen encapsulation with objects. Objects in C++, which encapsulate both data and methods, will be discussed more in later lessons.

The use of functions provides several benefits. First, it makes programs significantly easier to understand and maintain. The main program can consist of a series of function calls rather than countless lines of code. A second benefit is that well written functions may be reused in multiple programs. The C standard library is an example of the reuse of functions. A third benefit of using functions is that different programmers working on one large project can divide the workload by writing different functions.

Defining and Declaring Functions
A function is declared with a prototype. A function prototype consists of the return type, a function name and a parameter list. The function prototype is also called the function declaration. Here are some examples of prototypes.

return_type function_name(list of parameters);

int max(int n1, int n2);    
int printResults(string buffer, int status);    

The function definition consist of the prototype and a function body, which is a block of code enclosed in parenthesis. A declaration or prototype is a way of telling the compiler the data types of the any return value and of any parameters, so it can perform error checking. The definition creates the actual function in memory. Here are some examples of functions.

int FindMax(int n1, int n2)
{
    if (n1 > n2)
    {
        return n1;
    }
    else
    {
        return n2;
    }
}

void PrintMax(int someNumber)
{
    cout << "The max is " << someNumber << endl;
}

void PrintHW()
{
    cout << "Hello World" << endl;
}

float FtoC(float faren)
{
    float factor = 5./9.;
    float freezing = 32.0;
    float celsius;

    celsius = factor * (faren - freezing);

    return celsius;
}

There are a few significant things to notice in these examples. The parameter list of a function may have parameters of any data type and may have from no to many parameters. The return statement can be used to return a single value from a function. The return statement is optional. For instance, the function PrintHW does not return a value. Techniques for returning multiple values from a function will be covered later in the lesson. Finally, observe that variables can be declared within a function. These variables are local variables and can only be used within the function. They have local scope. Scope refers to the section of code where a variable name is valid and may be used. We'll see more on scope in a later lesson.

Next page > Using Functions > Page 1, 2, 3, 4, 5, 6


Subscribe to the C/C++ Newsletter
Name
Email






Email this page | Sign up for a Newsletter |
Explore More
Most Popular Articles
• C++ Programming Tutorial
• C Programming Tutorial
• C++ Tutorial - Lesson 12: File Input and Output
• C++ Tutorial - Lesson 4: Input and Output - cin, cout, iostr...
• C++ Tutorial - Lesson 10: Arrays and Vectors
• C++ Tutorial - Lesson 1: Writing and Compiling A First Progr...
• Tutorials
What's Hot Now
• Teach Yourself C++ in 21 Days by Jesse Liberty
• C Programming Tutorial
• C++ Tutorial - Lesson 25: Operator Overloading
• The C Programming Language, 2nd Edition by Kernighan and Rit...
• Topics in C++: Function Template Specialization
• C Programming Tips: Using Long Constants
• C Tutorial - Lesson 5: Conditional Processing, Part 1 - Rela...
About Us | Advertise on This Site | User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2004 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.