Functions in C++
Learn the subtle details about functions using C++ as a case-study.
Hello and welcome to this article about Functions in C++.
Functions in C++ is similar to every other programming language, but if you are new to programming it is good to have a view of how functions work in low-level languages like C++.
An important thing to note about function in almost all programming language is that a function can exist as a function or a method. I said almost because some programming languages do not have method, just functions.
What is a Function?
A function is a block of code that encapsulate a specific logic that can be scoped within that block of code. Scoped in the sense that the block of logic can start in the function and end in that same function.
To define this simply,
a function is a block of code that encapsulate logic that starts and ends in that same function.
I’ll demonstrate a function in three different languages. They all do the same thing.
def multiply(a: int, b: int) -> int:
return a * b
function multiply(a, b) {
return a * b;
}
int Multiply (int a, int b) {
return a * b;
}
Running this code in any of the three language returns the same result.
What is a Method?
A method is a function that is defined inside a class. Let me demonstrate an example.
class Calculator:
...
def multiply(self, a: int, b: int) -> int:
return a * b
class Calculator {
multiply(a, b) {
return a * b;
}
}
class Calculator {
public:
int multiply(int a, int b) {
return a * b;
}
};
These three methods are similar. They are defined within a class which makes them a class method (a function defined within a class).
It is important to note the subtle difference between a function and a method, which is: A function encapsulate a block of logic, while a method is a function that is defined within a class.
But aside from knowing the difference between a function and a method, it is good to know what happens when you use functions in your code. To best understand this, let’s see what happens when you call a function.
What happens when you call a function?
Every time you call a function from your code, the compiler generates a call instruction for that function. Meaning; in a running program, in order to call a function, the compiler needs to create an entire stack frame for the function being called. i.e., the compiler has to push this function information onto the stack:
the function parameters (if the function / method contains parameters),
the return address in memory.
Then the compiler jumps to a different part of our binary code in order to start executing the instructions from the functions pushed to the stack. Then the return of the function has to be gotten and “returned” to the line of the code before the function is being called. There is a lot of back and forth in memory in order to execute function instructions during function calls, and this back and forth takes time for the compiler to perform. This back and forth can cause our programs to be slow if we have many deeply nested functions, or if we write code with functions that can be written without a function.
One takeaway from this is that writing functions in our code requires thinking of how to optimize our code by having functions only when needed.
What to remember is that the best time to create a function is when you write repetitive code block logic multiple times in your code.
👉🏽 Remember, the primary point of functions is to prevent code duplication.
Function Declaration and Function Definition
A very simple way to understand this is that function declaration is a function written without the body of the function, while a function definition is a function written with the block of logic for that function.
In C++, functions can be declared or defined. Function declarations are usually stored in header files i.e., .h
files but can be stored in other .cpp
files, while Function definitions are written in .cpp
files. I wrote more about Function Definition and Function Declaration in another article. Link to that at the end of the article.
Let’s see an example demonstrating both.
#include <iostream>
// definition of Log in an external file
void Log(const char* message)
{
std::cout << message << std::endl;
std::cin.get();
}
#include <iostream>
void Log(const char* message); // declaration of Log from Log.cpp
int main() {
return 0;
}
From the code snippets above the function declaration is the declaration of the function Log
without the function body, which makes the Log function in the main.cpp
file a function declaration. While in the log.cpp
file, the function is defined which makes it a function definition.
Syntax
#include <iostream>
void Log(const char* message)
{
std::cout << message << std::endl;
std::cin.get();
}
int main() {
return 0;
}
There are two functions in the code snippet above with similar syntax. The syntax for functions in C++ is as follows:
The function specifier (optional) e.g., static, inline etc.
The data type. (required) e.g., void, int.
The name of the function.
The parameters for the function (optional).
Conclusion
That’s it for functions in C++. I hope you have learnt something from this article.
Thanks for reading. 🙂