DEV Community

Cover image for C++: A concise introduction to functions
Isaac Smith
Isaac Smith

Posted on • Updated on

C++: A concise introduction to functions

A function is a piece of code that performs a specific task and gives a program modularity. Multiple functions can be included in a program, making complex programs easier to manage and understand. I am aware of two types of functions: standard library functions and user-defined functions. The functions that come prepackaged with the programming language are known as standard library functions, whereas user-defined functions are functions that are created by the programmer. When a function is called, the code defined in its body is executed. The following is a general definition of a function

returnType functionName (parameter0, parameter1) {
    // Function body

    // return statement
}
Enter fullscreen mode Exit fullscreen mode

It is possible to declare a function with or without parameters. A parameter is a value passed into a function when it is declared. When calling a function, the type of arguments passed into it should match the corresponding parameters defined in the function declaration. This is a function that calculates velocity given distance and time.

// Function declaration
int velocity(int distance, int time);

// Function definition 
int velocity(int distance, int time) {

    return distance / time;
}
Enter fullscreen mode Exit fullscreen mode

Functions can have or not have a return statement. When returning a value, the return keyword is used. To call a function, we write the function name at the point where we want to call it and, if necessary, pass arguments to it. The name of the function is specified in the function declaration, and the body of the function is specified in the function definition.

functionName(argument0, argument1);
Enter fullscreen mode Exit fullscreen mode

Functions can have default parameters; if a function with default
parameters is called without passing arguments to it, it will use the default parameters. This function can add two or three numbers.

// Function declaration
int add(int i, int j, int = 0);

// Function definition
int add(int i, int j, int k) {

    return i + j + k;
}
Enter fullscreen mode Exit fullscreen mode

The default parameters of a function can be overwritten when the function is called. In C++, we can call functions either by value
or by reference.

Advantages of using Functions

Using functions cuts down on coding time. Because all the subtasks are divided down into their own functions, functions make building huge programs easier to write and debug. The same functions that a programmer defines in different portions of a program can be reused. This cuts down on the amount of code a programmer has to write while simultaneously shortening development time.

Because there is a separation of concerns in the software, using functions makes testing easy. Because separate functions perform different jobs, a programmer can test for problems because the desired result is obvious. Because there is a single point of failure and difficulty reasoning around it, testing a monolithic program may fail. Functions are a convenient approach to create well-structured, testable applications.

Maintainability is improved by using functions. If a program is split down into smaller bits, a programmer can simply adjust it. A program that has been broken down into functions is easier to navigate and allows the programmer to avoid scanning the entire program for a single line of code that has to be changed.

The use of functions improves the reliability of a program. This is due to the code's modularity, which allows for easy testing and debugging, as well as the code's maintainability, which allows the programmer to alter parts of the code without breaking something on another line or affecting the program's structure. It's usually best to divide diverse activities and processes into their own functions when designing sophisticated software.

Happy coding.

Discussion (2)

Collapse
elvisoric profile image
Elvis Oric

Nice post! You just need to pay attention to the terminology.
Comments in code snippets are a bit misleading.
// Define function is actually function declaration and
// Create function is function definition.

Collapse
isaack_bsmith profile image
Isaac Smith Author

Thank you for your feedback. I have fixed the error.