C++ Functions
In this tutorial, we will learn about the C++ function and function expressions with the help of examples.
A function is a block of code that performs a specific task.
Suppose we need to create a program to create a circle and color it. We can create two functions to solve this problem:
a function to draw the circle
a function to color the circle
Dividing a complex problem into smaller chunks makes our program easy to understand and reusable.
There are two types of function:
Standard Library Functions: Predefined in C++
User-defined Function: Created by users
In this tutorial, we will focus mostly on user-defined functions.
C++ User-defined Function
C++ allows the programmer to define their own function.
A user-defined function groups code to perform a specific task and that group of code is given a name (identifier).
When the function is invoked from any part of the program, it all executes the codes defined in the body of the function.
C++ Function Declaration
The syntax to declare a function is:
returnType functionName (parameter1, parameter2,...) {
// function body
}
Here's an example of a function declaration.
// function declaration
void greet() {
cout (( "Hello World";
}
Here,
the name of the function is greet()
the return type of the function is void
the empty parentheses mean it doesn't have any parameters
the function body is written inside {}
Note: We will learn about returnType and parameters later in this tutorial.
Calling a Function
In the above program, we have declared a function named greet(). To use the greet() function, we need to call it.
Here's how we can call the above greet() function.
int main() {
// calling a function
greet();
}
There are two most popular ways to pass parameters:
Pass by Value: In this parameter passing method, values of actual parameters are copied to the function’s formal parameters and the two types of parameters are stored in different memory locations. So any changes made inside functions are not reflected in the actual parameters of the caller.
Pass by Reference: Both actual and formal parameters refer to the same locations, so any changes made inside the function are actually reflected in the actual parameters of the caller.
Points to Remember About Functions in C++
1. Most C++ program has a function called main() that is called by the operating system when a user runs the program.
2. Every function has a return type. If a function doesn’t return any value, then void is used as a return type. Moreover, if the return type of the function is void, we still can use the return statement in the body of the function definition by not specifying any constant, variable, etc. with it, by only mentioning the ‘return;’ statement which would symbolize the termination of the function
C++ Recursion
When function is called within the same function, it is known as recursion in C++. The function which calls the same function, is known as recursive function.
A function that calls itself, and doesn’t perform any task after function call, is known as tail recursion. In tail recursion, we generally call the same function with return statement.
C++ Passing Array to Function
In C++, to reuse the array logic, we can create function. To pass array to function in C++, we need to provide only array name.
functionname(arrayname); //passing array to function
C++ Overloading (Function)
If we create two or more members having the same name but different in number or type of parameter, it is known as C++ overloading. In C++, we can overload:
methods,
constructors and
indexed properties
It is because these members have parameters only.
Types of overloading in C++ are:
Function overloading
Operator overloading
C++ Function Overloading
Function Overloading is defined as the process of having two or more function with the same name, but different in parameters is known as function overloading in C++. In function overloading, the function is redefined by using either different types of arguments or a different number of arguments. It is only through these differences compiler can differentiate between the functions.
The advantage of Function overloading is that it increases the readability of the program because you don’t need to use different names for the same action.
Watch video Parameters and Arguments in Functions || C++ Complete Course || Beginners to Advance| Urdu/Hindi|BJM online without registration, duration hours minute second in high quality. This video was added by user BJM Technologies 03 December 2022, don't forget to share it with your friends and acquaintances, it has been viewed on our site 7 once and liked it people.