Table of Contents
Every programming language allows functions to be created and used. A function is a group of statements that can be reused. It allows us to write code that is clean and manageable. A function can be either user-defined or built-in (predefined).
User-defined functions are those that are written by programmers, not but the language creators. On the other hand, Built-in functions are predefined functions of a language.
Benefits of Using Functions
- Stores a block of code and makes it reusable across one or multiple programs.
- Eliminates redundant code by storing it in one single place.
- Saves time and cost by making the code easier to debug, maintain and update.
Defining Functions
To use functions in your program, you must first define them. Defining a function allows us to specify the behavior and code for the function.
Function declaration Syntax
The syntax for defining a function in C++ is:
return_type function_name() {
// function body...
}
Function Parts
A function definition has different parts as explained below:
1. Return Type
All functions can optionally return some value on successful execution. The data type of value that the function returns must match the return type of the function.
A function that does not return a value is declared as void function.
2. Function Name
The name of the function must be a valid identifier. It follows the same rules for naming variables in C++.
3. Function Body
The function body contains the code that we want to execute when the function is used.
Example of a function that reads and prints out a number:
void print() {
int num;
cout << " Enter a number: ";
cin >> num;
cin << " The entered number is: " << num;
}
Declaring Functions
In C++, declaring a function is pretty similar to declaring a variable which informs the proper way to the compiler on how to call it.
A function can be defined in two locations:
1. Before main()
If we define a function before main(), it acts both as a declaration and definition.
return_type function_name() {
// function body
}
void main() {
// use the function
function_name()
}
2. After the main() function.
If the function is defined after main(), we must declare it otherwise the compiler will throw an error. Declaring a function specifies the return type and the function name.
// declaration just specifies the function.
return_type function_name();
void main() {
// use the function
function_name()
}
// definition contains the function body
return_type function_name() {
// function body
}
Using Functions
To use user-defined functions, we need to invoke/call them. In programming, executing a function is known as invoking or calling it.
We can invoke a function by specifying its name and putting a pair of parenthesis:
function_name();
Invoking a function that is not defined will throw an error.
Example:
/* define the function */
void greet() {
cout << "Welcome, user! ";
cout << "It's great to see you here. ";
}
int main() {
/* invoke method in your code where you want */
greet();
cout << "From main.";
/* other code */
}
The code will execute sequentially as the output below:
Welcome, user!
It’s great to see you here.
From main.
Scope in functions
Scope in programming defines the accessibility of variables and other data in a program. Any variable defined inside a function has a functional scope. It can be accessed and used only within the function where it is defined.
Moreover, every time a function is called, a new space in memory is allocated for its variables.
Points to Remember:
- Functions help us to group up code and reuse it later.
- Functions help to reduce the redundant code and reuse them later.
- A Function must be defined or declared before it can be used. A function definition contains three parts – return type, function name, and the function body.
- Variables declared inside a function, known as local variables, can only be accessed in that function due to scope rules.
