A function consists of two parts, the declaration and the definition.
The declaration consists of the function's name, return type, and parameters,
while the definition is the code block inside the function.
The function's return type is the same as the data type
(int, float, etc).
But, a function could also not return anything.
This type of function will use the return type of void.
Syntax:
return_type function_name (data_type parameter_name){ //declaration //line of codes to be executed or definition }
Example:
#include<iostream> total=10; void store (int a){ total+=a; } int main() { int goods=10; store(goods); return 0; }
The function above will not return any value, but it will change the variable total.
Example:
#include<iostream> int store (int a){ int total=10; total+=a; return total; } int main() { int goods=10; int result; result = store (goods); return 0; }
Notice that the function above has the return statement.
The return statement is used to return value to other functions.
And, because the function has a return value, we need to prepare a variable
to store the return value (in this case,
the variable name is result).
Declared functions are not executed when we don't call them. So, to execute them, we need to call them. To call a function, we merely need to write the function name with parentheses and semicolons. If the function needs parameters, we need to also include parameters in the parentheses.
Syntax:
function_name(parameters);
Example:
#include<iostream> total=10; void store (int a){ total+=a; } int main() { int goods=10; store(goods); //calling the function return 0; }
Information can be passed to functions as a parameter. Parameters are variables inside a function that are useful for operations inside the function. There is also another type of parameter called arguments. The argument is a parameter that has been passed to a function, or we can say that the argument is the input from other functions to a particular function.
Example:
#include <iostream> void displayInfo(int age, int phone){ std::cout << "This is your age, " << age; std::cout << "\nThis is your phone number, " << phone; } int main() { displayInfo(10, 9912); return 0; }
In this example, 10 and 9912 are the arguments,
while age and phone are the parameters.
We can also set a default parameter value for a function. So, if you call a function but do not provide an argument to it, it will automatically use the default value. A parameter with a default value is called an optional parameter.
Example:
#include <iostream> void display(int age=20){ std::cout << age << std::endl; } int main() { display(10); //the output is 10 display(); //the output is 20 return 0; }
C++ also enables users to pass the parameter using references. In the previous examples, we pass the normal variable as a parameter. But, we actually can pass references as a parameter. This is called pass by reference.
Example:
#include <iostream> void swap(int &x, int &y) { int temp = x; x = y; y = temp; } int main() { int firstNum = 10; int secondNum = 20; std::cout << "Before swap: " << "\n"; std::cout << firstNum << secondNum << "\n"; swap(firstNum, secondNum); std::cout << "After swap: " << "\n"; std::cout << firstNum << secondNum << "\n"; return 0; }
C++ also enables function overloading. Function overloading means multiple functions having the same name with different parameters.
Example:
#include <iostream> int adding(int x, int y) { return x+y; } float adding (float x, float y){ return x+y; } double adding (double x, double y){ return x+y; } int main() { int first = adding (20 + 10); float second = adding (9.1 + 1.2); double third = adding (9.23444 + 12.901); return 0; }
Lambda is an expression that allows us to define function objects which can be
used inline. So, in other words, we do not need to declare a function outside the
main().
Syntax:
data_type function_name = [] () { //lines of code };
Explanation:
[] is the lambda introducer which denotes the start of
lambda expression() is the parameter listSyntax:
#include <iostream> int main() { auto display = [](int age=20){ std::cout << age << std::endl; }; display(10); //the output is 10 display(); //the output is 20 return 0; }