About these ads

C++ Programming Tutorials: Function Libraries and the Main Function

There are certain required elements for every C++ program. If you want to use certain inbuilt functions you have to include their libraries. Every program needs a function called ‘main’. This post will look at these two of the requirements of C++. These are all fairly simple, but are often the cause for errors.

Including Function Libraries

To use any inbuilt C++ functions, like input and output, you have to include a statement that lets the compiler know to include their definitions in your program. Its best to put this type of statement at the start of your program. The code you need to use is:

#include <iostream>
using namespace std;
  • The #include is the same for every library. Its the keyword that lets the compiler know what is going on.
  • The name of the library has to be inside the <> which happens to be iostream for the input/output functions library.
  • using namespace is about letting the compiler know which function names to use. std stands for standard. You only need one of these statements in your program even if you include multiple libraries.
  • Every executable statement in C++ needs a ; at the end. If you don’t put them in the compiler will return an error.

The Main Function

Every C++ program needs a main function. This is where execution starts. In it you can write your code and call other functions. There are certain requirements for functions in C++. Lets examine a simple main function:

int main() {
    return 0;
}
  • The int is the return type of the function. This isn’t necessary for the main function but is good practice to include.
  • main() is the function name. main functions don’t usually take an input, so nothing is placed inside the brackets. You can write void inside the brackets which is another way of saying ‘no input’. You have to spell main exactly as shown, C++ is case sensitive.
  • If you have defined a return type you need to terminate the function with a return statement. After it you have to have a value or variable of the type you specified in the function definition.
  • The {} around the body of the main function are necessary because they let the compiler know where the main function starts and finishes. The compiler will return an error if you don’t put them in.

Other Posts in this Series

1. Introduction to C++ and Series
2. Function Libraries and the Main Function
3. Declaring Variables
4. Basic Input and Output Functions
5. C++ Mathematics and Logic Operations
6. If Statements (Conditional Execution)
7. Switch Statements (More Flow Control)
7.1. Example Program using Switch Statement

Listen to this Post

This post is part of the C++ Programming Tutorials series in which I will write about basic techniques and requirements of C++ programming. I will publish them every Friday. You can find the other posts in this series here.

I put every effort into bringing you reliable information and keeping it up to date, but some information found on this website might not be accurate, complete or up to date. Please don’t rely 100% on the information I provide.

Please read the About page for more information.

About these ads

Tags: , , , , , , , , , , ,

Leave a comment on this post (no links allowed):

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 83 other followers

%d bloggers like this: