Cpp reference main function

C++ Functions

A function is a block of code which only runs when it is called.

You can pass data, known as parameters, into a function.

Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times.

Create a Function

C++ provides some pre-defined functions, such as main() , which is used to execute code. But you can also create your own functions to perform certain actions.

To create (often referred to as declare) a function, specify the name of the function, followed by parentheses ():

Syntax

Example Explained

  • myFunction() is the name of the function
  • void means that the function does not have a return value. You will learn more about return values later in the next chapter
  • inside the function (the body), add code that defines what the function should do

Call a Function

Declared functions are not executed immediately. They are «saved for later use», and will be executed later, when they are called.

To call a function, write the function’s name followed by two parentheses () and a semicolon ;

In the following example, myFunction() is used to print a text (the action), when it is called:

Example

Inside main , call myFunction() :

int main() myFunction(); // call the function
return 0;
>

// Outputs «I just got executed!»

A function can be called multiple times:

Example

int main() myFunction();
myFunction();
myFunction();
return 0;
>

// I just got executed!
// I just got executed!
// I just got executed!

Function Declaration and Definition

A C++ function consist of two parts:

  • Declaration: the return type, the name of the function, and parameters (if any)
  • Definition: the body of the function (code to be executed)

Note: If a user-defined function, such as myFunction() is declared after the main() function, an error will occur:

Example

However, it is possible to separate the declaration and the definition of the function — for code optimization.

You will often see C++ programs that have function declaration above main() , and function definition below main() . This will make the code better organized and easier to read:

Example

// Function declaration
void myFunction();

// The main method
int main() myFunction(); // call the function
return 0;
>

// Function definition
void myFunction() cout >

Источник

main function and program execution

Every C program has a primary function that must be named main . The main function serves as the starting point for program execution. It usually controls program execution by directing the calls to other functions in the program.

Several restrictions apply to the main function that don’t apply to any other C functions. The main function:

  • Can’t be declared as inline .
  • Can’t be declared as static .
  • Can’t have its address taken.
  • Can’t be called from your program.

The main function signature

The main function doesn’t have a declaration, because it’s built into the language. If it did, the declaration syntax for main would look like this:

int main( void ); int main( int argc, char *argv[ ] ); int main( int argc, char *argv[ ], char *envp[ ] ); 

The main function is declared implicitly by using one of these signatures. You may use any of these signatures when you define your main function. The Microsoft compiler also allows main to have a return type of void when no value is returned. The argv and envp parameters to wmain can also be defined as type char** . For more information about the arguments, see Argument description.

Remarks

Functions within the source program perform one or more specific tasks. The main function can call these functions to perform their respective tasks. When main calls another function, it passes execution control to the function, so that execution begins at the first statement in the function. A function returns control to main when a return statement is executed or when the end of the function is reached.

You can declare any function, including main , to have parameters. The term «parameter» or «formal parameter» refers to the identifier that receives a value passed to a function. See Parameters for information on passing arguments to parameters. When one function calls another, the called function receives values for its parameters from the calling function. These values are called arguments. You can declare formal parameters to main so that it can receive arguments from the command line using the format shown in the function signature.

When you want to pass information to the main function, the parameters are traditionally named argc and argv , although the C compiler doesn’t require these names. Traditionally, if a third parameter is passed to main , that parameter is named envp . The types for argc , argv , and envp are defined by the C language. You can also declare argv as char** argv and envp as char** envp . Examples later in this section show how to use these three parameters to access command-line arguments. The following sections explain these parameters.

If your code adheres to the Unicode programming model, you can use the Microsoft-specific wide-character version of main , wmain , as your program’s entry point. For more information about this wide-character version of main , see Using wmain .

main termination

A program usually stops executing when it returns from or reaches the end of main , although it can terminate at other points in the program for various reasons. For example, you may want to force the termination of your program when some error condition is detected. To do so, you can use the exit function. For more information on exit and an example of usage, see exit .

Источник

main function and program execution

Every C program has a primary function that must be named main . The main function serves as the starting point for program execution. It usually controls program execution by directing the calls to other functions in the program.

Several restrictions apply to the main function that don’t apply to any other C functions. The main function:

  • Can’t be declared as inline .
  • Can’t be declared as static .
  • Can’t have its address taken.
  • Can’t be called from your program.

The main function signature

The main function doesn’t have a declaration, because it’s built into the language. If it did, the declaration syntax for main would look like this:

int main( void ); int main( int argc, char *argv[ ] ); int main( int argc, char *argv[ ], char *envp[ ] ); 

The main function is declared implicitly by using one of these signatures. You may use any of these signatures when you define your main function. The Microsoft compiler also allows main to have a return type of void when no value is returned. The argv and envp parameters to wmain can also be defined as type char** . For more information about the arguments, see Argument description.

Remarks

Functions within the source program perform one or more specific tasks. The main function can call these functions to perform their respective tasks. When main calls another function, it passes execution control to the function, so that execution begins at the first statement in the function. A function returns control to main when a return statement is executed or when the end of the function is reached.

You can declare any function, including main , to have parameters. The term «parameter» or «formal parameter» refers to the identifier that receives a value passed to a function. See Parameters for information on passing arguments to parameters. When one function calls another, the called function receives values for its parameters from the calling function. These values are called arguments. You can declare formal parameters to main so that it can receive arguments from the command line using the format shown in the function signature.

When you want to pass information to the main function, the parameters are traditionally named argc and argv , although the C compiler doesn’t require these names. Traditionally, if a third parameter is passed to main , that parameter is named envp . The types for argc , argv , and envp are defined by the C language. You can also declare argv as char** argv and envp as char** envp . Examples later in this section show how to use these three parameters to access command-line arguments. The following sections explain these parameters.

If your code adheres to the Unicode programming model, you can use the Microsoft-specific wide-character version of main , wmain , as your program’s entry point. For more information about this wide-character version of main , see Using wmain .

main termination

A program usually stops executing when it returns from or reaches the end of main , although it can terminate at other points in the program for various reasons. For example, you may want to force the termination of your program when some error condition is detected. To do so, you can use the exit function. For more information on exit and an example of usage, see exit .

Источник

Читайте также:  Css background repeating linear gradient
Оцените статью