Functions and parameters in C++

In this post, I will talk about functions and how to use functions in C++. The function may or may not have parameters. I will also cover topics about functions with parameters passed as values and through a reference.

Functions

A function is a block that contains lines of code that can be called whenever needed. It avoids duplication of the code. The ones that change are the parameters and return values. It is usually a good practice to keep the function with approximately 15 lines of code. This is a standard practice that is recommended. When the function grows longer and longer it becomes less readable and it can become hard to write unit test cases.

I have an example to explain more about functions.

Functions without parameters

A standard syntax for a function without parameters is as follows:

data-type FunctioName(parameter1, parameter2,…..);

A datatype can be of any type like a built-in type or a user-defined type. The function name is a name given that is usually a combination of one or two words that best describes what the block performs.

#include <iostream>
using namespace std; 

int Addition()
{
	int a = 10; 
	int b = 9; 
	int result = a + b; 
	return result; 
}
int main()
{
	cout<<"Result : "<<Addition()<<endl;
	return 0; 
}
/***********OUTPUT**************/
Result : 19

I have created a function to add 2 numbers called Addition. It does not contain any parameters. I have declared and initialized 2 numbers by name ‘a’ and ‘b’. This is a simple example of a function.

Functions with passing values as parameters

From the previous example, we see that if we pass the values as parameters the function can be called anytime with different values. So I extended the function to take in parameters which will give more flexibility.

#include <iostream>
using namespace std; 

int Add(int first, int second)
{
	int result = first + second; 
	if (result > 0)
	{
		return result; 
	}
	return -1; 
}
int main()
{
	Add(8,19);
	return 0; 
}
Functions with parameters passed as reference

We can pass the parameters by reference. This means that we can send the address location of a variable and then the value can be accessed. This is a helpful feature because when a function is called the system stores the temporary variables and much more information in the stack data structure. It makes another copy of the variables and lets the function work on the copied values. The main difference between passing by value and passing by reference is:

  • In passing by value is when a value is passed to the function and the function makes changes to that variable. These changes stay inside the scope of the function.
  • When we pass by reference, the values which are modified inside the function will also show outside the scope of the function. i.e to the caller function as well.
void passbyRef(int &a, int & b)
{
	a = 20;
	return; 
}
int main()
{
	int x = 90, y = 200;
	passbyRef(x, y);
	cout << "X:" << x << endl;
	cout << "Y:" << y << endl;
	return 0; 
}

/****************OUTPUT********************
X:20
Y:200

In the example, I passed 90 and 200 for x and y. This is taken in as reference because it uses & for referring to the address location. The function changes the value at ‘x’ which is ‘a’ to 20. These changes will be reflected after we come out of the function.

Conclusion

In this post, We learned about functions. Firstly, we spoke about functions. Secondly, Functions with and without parameters. Thirdly, What is passing by reference and the difference between passing by value and reference. Next tutorial I will talk more about File stream I/O operations.

Prev : Arrays

Next : File streams