Functions in C++
Requirements:
Linux Distribution
g++
any text editor
My Setup:
Debian 10
g++ version 6.3.0
pluma
Suppose you want to write a program that requires a lot of steps to perform
the required action you wish to write. Best practices says to not write
everything in your main function. Instead, break up your program into what could
be considered as sub-procedures. Today we will be talking about functions, what
they are and how they work.
What exactly is a function? A Function is a named section of a program that
performs a specific task. In this sense, a function is a type of procedure or
routine. Some programming languages make a distinction between a function,
which returns a value, and a procedure, which performs some operation but does
not return a value. Most programming languages come with a pre written set of
functions that are kept in a library. You can also write your own functions to
perform specialized tasks. Lets write a small program without any functions and
see if we can break it down further.
#include <iostream> int main(int argc, char *argv[]){ int x; int y; std::cout << "Enter first number:"; std::cin >> x; std::cout << "Enter second number:"; std::cin >> y; std::cout << "Sum of numbers : " << x + y << std::endl; std::cout << "Difference of numbers : " << x - y << std::endl; std::cout << "Product of numbers : " << x * y << std::endl; return 0; }
As you can see there are alot of statements in our main function. There are a
couple things we could do here. We could write functions to return the results
of each such as :
#include <iostream> int add(int a, int b){ return a+ b; } int subtract(int a, int b){ return a - b; } int multiply(int a, int b){ return a * b; } int main(int argc, char *argv[]){ int x; int y; std::cout << "Enter first number:"; std::cin >> x; std::cout << "Enter second number:"; std::cin >> y; std::cout << "Sum of numbers : " << add(x,y) << std::endl; std::cout << "Difference of numbers : " << subtract(x,y) << std::endl; std::cout << "Product of numbers : " << multiply(x,y) << std::endl; return 0; }
Or what we could do is just print the results in another function as such:
#include <iostream> void printResults(int a, int b){ std::cout << "Sum of the numbers: " << a + b << std::endl; std::cout << "Difference of numbers:" << a -b << std::endl; std::cout << "Product of numbers: " << a * b << std::endl; } int main(int argc, char *argv[]){ int x; int y; std::cout << "Enter first number:"; std::cin >> x; std::cout << "Enter second number:"; std::cin >> y; printResults(x,y); return 0; }
Now, these may seem like a bit of overkill at the moment, however, if your
planning on doing something more then just one time, it maybe easier to write a
function to do it, and then call the function instead of repeating the same code.
Remember “Dont Repeat Yourself”. So lets go over the syntax of writing a
function :
return_type functionName(parameter_Type parameter_name){ //do code here return whatever_variable_returns; }
So the return_type can be anything. You can return any type of data. If you
are not returning any data, you can call this void. Then you can omit the return
keyword at the end of the function. Functions can take in data as well. From a
function-side of the code, the data that is taken in are parameters . From an
end-user side of code, the data you pass into function is considered arguments.
In C++, you can pass variables in arguments by:
1. Value: the variables you pass in are copied into temporary values and the
variables are never modified
2. Reference: the variables you pass in are used as is and can be modified
unless you specify const (in which that variable is read-only)
If you havent already, i suggest checking out my C tutorial on “Variables,
Functions, and Pointers ” to get a better example on function calls by value
compared to references. In C , the only way to pass by reference, is using
pointers, however C++ has the references feature built in, so passing by pointer
or by reference is okay. The only difference is that a reference is always
referring to another variable, where a pointer can change what its pointing to
through out its lifetime.