C++ Functions

A function in C++ is a block of code that performs a specific task (e.g. adding two numbers, computing area of a circle etc.).

It is a reusable block of code – define the function code once, and use it repeatedly.

A function is executed only when it gets called. 

Dividing a complex problem into a number of functions makes the code more readable.

Every C++ program that we develop has at least one function, which is main().

The syntax for defining a function is:

return_type function_name(para1, para2,..., paraN) { 
// function body
}

Here, para1, para2, etc. are the parameters passed into the function. The curly brackets (i.e. { } ) define the start and end of the function. The body of the function contains code statements.

The example below shows an example of a function definition. 

// function definition
void show() {
    cout << "Hello Friend!";
}

In the example above,

  • the name of the function is show()
  • the return type of the function is void
  • the empty parentheses mean no parameter
  • the function body is written inside {}

 

There are two types of function:

  1. Standard Library Functions: They are predefined in the C++ libraries.
  2. User-defined Functions: They are created by the programmers.
 
Scope and lifetime of variables within functions

When we declare a variable within a function, the variable can only be accessed within that function. When we exit the function, the variable is destroyed. These variables are called local variables. We can have local variables with the same name in different functions, because each is recognized only by the function in which it is declared.

However, If we declare a variable outside a function, all the functions on our program can access it. The lifetime of these variables starts when they are declared, and ends when the program is closed.

 

Function Examples

Following are the different types of examples of functions in C++.

 

1. Function with no parameter

In the example below, we define a function named show() without any parameter. 

To use the show() function, we need to call it.

Here’s how we can call the show() function.

#include <iostream>
using namespace std;

// function definition
void show() {
cout << "Hello Friend!";
}

int main() {
show(); // function call
return 0;
}

Output:

Hello Friend!
 
2. Function with parameters

The following example shows a function with parameters.

#include <iostream>
using namespace std;

// function definition
void add(int a, int b) {
int sum = a + b;
cout << "Sum: " << sum;
}

int main() {
add(10, 20); // function call
return 0;
}

Note: When parameters are passed to the function, it is called the arguments. So, from the example above: a are b parameters, while 10 and 20 are arguments.

Output:

Sum: 30
 
3. Function that returns a value

The example below shows a function that returns a value.

#include <iostream>
using namespace std;

// function definition
string myFunction()  {
   return "Hello, have a nice day!";
}

int main() {
   string str = myFunction(); // function call
   cout << str;
return 0;
}

Output:

Hello, have a nice day!
 
4. Function with parameters and that returns a value

The following example shows a function with parameters and that returns a value.

#include <iostream>
using namespace std;

// function definition
float sum(float a, float b, float c) {
return a + b + c;
}

int main() {
float result;
result = sum(10.25, 20.35, 30.15); // function call
cout << "Sum: " << result;
return 0;
}

Output:

Sum: 60.75