Functions in C++ | Declaring, Defining and Calling

Functions in C++ are a group of program statements with a unique name that perform a specific task. Functions are used to provide modularity to a program. By default every C++ program execution starts from the main() function.

Depending on whether a function is predefined or created by user; there are two types of function:

  • Library / Predefined Function
  • User-defined Function

A function declaration tells the compiler about a function’s name, return type, and parameters. A function definition provides the actual body of the function.

functions in c++

Defining Functions in C++
A C++ function definition consists of 2 parts:

  • Function header / signature
  • Function body

Following are the components of a C++ function:

  • Return Type: A function may return a value. The return_type is the data type of the value the function returns. Some functions perform operations without returning a value. In this case, the return_type is the keyword void.
  • Function Name: This is the actual name of the function. The function name and the parameter list together constitute the function signature. To call a function, its name is used.
  • Parameters: A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.
  • Function Body: The function body contains a collection of statements that define what the function does.
Example of Function Definition
Declaring a function in C++

Function declaration tell compiler about that there exists a function with the given return type, name and set of parameters. Function declaration does not include the body of the function. The function’s body can be defined separately
A function declaration has the following parts:

Example of function declaration for the above max function:

Parameter names are not important in function declaration only their type is required, hence the above declaration can also be written as:

Function declaration is required when you define a function in one source file and you call that function in another file. In such case, you should declare the function at the top of the file calling the function.

Calling a Function in C++

After declaring and defining a function, to use it we have to perform a function call. Functions can be called by simply using their function name with the necessary arguments passed inside the round brackets.
When a program calls a function, program control is transferred to the called function. A called function performs defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns program control back to the main program.

Example program for function declaration, definition and calling

Run Online

Max value is : 200
Default Values for Parameters in Functions

When passing argument values for functions, we can specify default values when no value is passed. This default value will be used when no value is passed.
This is done by using the assignment operator and assigning values for the arguments in the function definition.
Default arguments can be assigned from right to left only. that is if there are 2 arguments to be passed and you want to make one as a default argument, only the right most parameter can be made as a default parameter and not the left one.

Default Argument program example in C++

Run Online

Total value is :300
Total value is :120
Watch it on YouTube

2 thoughts on “Functions in C++ | Declaring, Defining and Calling

  • May 4, 2018 at 9:40 pm

    hi! I am Rohini.s.kulkarni from Hubli . I want you to give me ticks and tricks to grasps stuff fast… specially theory .

    • May 6, 2018 at 4:38 pm

      Hello Rohini, In programming related subjects, the only way you can learn things fasters is by practicing code examples. On our website we have program examples with every topic. Try to type the code yourself because thats the best way to go about programming. Develop small programs and applications and then you will get better at this subject. All the best 🙂
      Also checkout this article for 5 ways to learn programming faster –


Leave a Reply

Your email address will not be published. Required fields are marked *