DEV Community

Cover image for Part 6: Functions
Ayesha Sahar
Ayesha Sahar

Posted on • Updated on • Originally published at ayeshasahar.hashnode.dev

Part 6: Functions

Hey folks, welcome to the 6th part of my series, C++: Explain like I’m five. In the last part, we discussed the different types of loop control statements used in C++. As you guys know quite a lot now, I feel that is time to move on to functions.

Functions

It is a block of code that only runs when we call it. Data that is known as "parameters" can be passed into a function. They only perform a certain defined action. Functions are important for reusing code as after defining it once, you can use it many times. For example, you want to display numbers from 1 to 100. You write its code again and again in the program when you want to display its values. But with a function, you only have to write the code once and call it wherever you want to display the values.

There are 2 types of functions:

  1. Standard Library Functions (built-in functions)
  2. User-defined Functions

Before creating our own functions, let's see what are the Standard Library Functions in C++.

1. Standard Library Functions

They are also known as built-in functions. We do not need to declare or define these functions as they are already written in the C++ libraries like iostream, cmath, etc. We can use them whenever we need them.

Example:

Let's use the built-in function pow(x,y), which is x to the power y. This function is declared in cmath header file. In order to use it, we will include the header file in our program using #include.

#include <iostream>
#include <cmath>
using namespace std;
int main(){
    cout<<pow(3,6);
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output:

729
Enter fullscreen mode Exit fullscreen mode

2. User-defined Functions

The functions that we declare and define in our programs are called user-defined functions. To understand them, let's take a look at an example.

Example:

#include <iostream>

using namespace std;

void display(void);  //function declaration

int main() 
{
    display();   //function call
} 

void display()  //function definition
{
    cout<< "My first cpp program using functions!"<<endl;
}
Enter fullscreen mode Exit fullscreen mode

Output:

My first cpp program using functions!
Enter fullscreen mode Exit fullscreen mode

Now you guys might be thinking about the terms that I used in the program comments. I used them in order to specify the syntax of a user-defined function. Those are the 3 components that build up a function.

1. Function Declaration:

It is made by declaring the return type, name and data types of parameters of a function and is terminated by a semi-colon.

Syntax:

return_type function_name(parameters list);
Enter fullscreen mode Exit fullscreen mode

The return type specifies what type of data does a function return. We could leave a parameter list empty just as I did in the example. But if you want to pass a parameter, then the parameter list should contain both data type and name of the variable.

For example:

int factorial(int n, float j);
Enter fullscreen mode Exit fullscreen mode

2. Function Call

In the example, we declared the name of the function "display()". So in order to use it, we need to call it. We will do so by writing "display();" wherever we need to use it.

3. Function Definition

A function definition is the part of a function where we define what exactly would the function do. In the example, I defined that whenever display() would be called, print the line "My first cpp program using functions!" and that's what it did.

Another important concept to know about is function overloading.

Function Overloading

A function is said to be overloaded when its name is given to another function. Even if the 2 functions have the same name, they should differ in at least one of these aspects;

  1. Number of parameters
  2. Data type of parameters
  3. Order of appearance

Example:

#include <iostream>
using namespace std;

int plusInt(int x, int y) {
  return x + y;
}

double plusDouble(double x, double y) {
  return x + y;
}

int main() {
  int Num1 = plusInt(8, 5);
  double Num2 = plusDouble(4.3, 6.26);
  cout << "Int: " << Num1 << "\n";
  cout << "Double: " << Num2;
  return 0;
}

Enter fullscreen mode Exit fullscreen mode

Output:

Int: 13
Double: 10.56
Enter fullscreen mode Exit fullscreen mode

That's all about functions. All I would say is try to use functions to solve real-world problems. It could be anything. You can even make a function for multiplying or subtracting numbers etc. Just remain consistent and practice. In the next part, we will discuss arrays so stay tuned :)

Note: To see the examples of some basic C++ programs, take a look at my github repo here. If you have any questions, comment down below or ping me on Twitter ^_^

Let's connect!

Twitter

Github

Top comments (0)