C++ Function is a piece of code that performs specific tasks. Functions can run multiple times by calling them in code. They may or may not have a return value. Any C++ code must have at least one function. In C++ programming language functions provide code reusability.
There are two types of functions in C++ programming language:
- User defined C++ functions
- Predefined C++ functions
User Defined Functions
The functions that user creates by defining and declaring are user defined functions. Function has a function name that acts as an identifier.
C++ Function Declaration
A programmer can create his own functions by function declaration. There are 3 parts of function declaration.
- Function return type
- Function name
- Function parameters
Programmer can declare a function by writing function’s return type followed by function name followed by parenthesis. The syntax for declaring a function is:
C++ Function Definition
A Function’s definition is present inside the curly braces {}. It is the function body. The curly braces {} contain the code body that will execute upon calling the function. The block of code present inside curly braces specifies the actions that a function will perform. The syntax for defining a function is:
In the above example, // piece of code is the definition of the function funcName().
C++ Function Call
The user defined function funcName is called in the pre defined function int main(). The syntax for calling a function is:
For example, the below code shows how to declare, define and call a function. This is a simple function that prints a text “This is my first function”.
#include <iostream> #include <string> using namespace std; void myFunc() { cout << "This is my first function\n"; } int main() { myFunc(); return 0; }
The output of the above code after compilation is:
The best practice is to declare user defined function before the main function and define that function after the main function. For example
#include <iostream> #include <string> using namespace std; // Function declaration void myFunc(); int main() { myFunc(); // Function call return 0; } // Function definition void myFunc() { cout << "This is my first function\n"; }
The output of above code after compilation is:
In C++ programming language, the compiler compiles the code from top to bottom. So, programmer should not define or declare the function after the main function as in this case error will arise.
C++ Function Name
While declaring a function, the function name and parenthesis () identify the function. Function is later called by its specified name through out the program. For example, in the above code, funcName() is the name of function. Here void is the type of function. A function with type void has no return value. A function can have different return types.
C++ Function Return Type
A function may or may or may not return a value before exiting the function body. The function that returns nothing has return type void. Other types of function must include return statement in the function’s body. Functions may return variables or constants.
For instance a function having return type int has the following syntax:
The function funcName returns an integer value 0. Other return types are float, double, long, etc.
C++ Function Parameters
C++ functions may or may not have input variables. These input variables are called function’s parameters. A function can have as many parameters as the programmer wants, separated by a comma. Programmer can also write void in function’s parenthesis to specify that function has no parameters. Parameters are a functions arguments. The syntax for writing function with arguments is:
While defining a function with parameters, programmer must specify the type of arguments. For example, the below code shows how to create a function that returns the sum of two integer values. The function addition takes two arguments as inputs. These arguments are defined in function definition. The type of arguments is int. While calling the addition function, user can input integer values as function’s parameters.
#include <iostream> using namespace std; int addition (int x, int y) { int sum; sum = x + y; return sum; } int main () { int z; z = addition (7,2); cout << "The result of addition is: " << z << '\n'; return 0; }
The output of above code after compilation is:
Passing Arguments to C++ Functions
While defining a function, C++ programming language allows user to pass the parameters in the form of various data types. C++ functions can take parameters in the form of:
- Constants
- Strings
- Arrays
- Pointers
For example, the below code shows a function that takes an array and array size as input parameters.
#include <iostream> using namespace std; void printArray(int a[], int array_size) { cout << "Elements of array are: " << endl; for (int i = 0; i < array_size; ++i) { cout << a[i] << endl; } } int main() { int array1[] = {10, 22, 21, 11, 9}; printArray(array1, 5); return 0; }
The output of above code after compilation is:
Here is another example that shows how to define a function that takes string as input.
// This code is written by www.tutorialsart.com #include <iostream> #include <string> using namespace std; void printText(string text) { cout << text << endl; } int main() { printText("This function prints your entered text"); return 0; }
The output of above code after compilation is:
C++ Predefined Functions
These are standard library functions. C++ has many built in functions. User does not need to define or declare system defined functions. These are already defined in C++ standard libraries. For example, the pre defined function int main() is most commonly used in programs. This function executes the code inside its body.
C++ functions provide simplification in programming by avoiding code repetition. Input output functions are present in iostream library. User can include the header file iostream to invoke I/O functions. Other examples of pre defined functions are pow(), sqrt(), floor(), abs(), that perform mathematical operations. These functions are present in the cmath header file. User can invoke these by including header file and calling function name in the code.
For example the code below takes a number and displays its square root using the built in function sqrt() present in cmath header file. The function sqrt() takes one parameter.
#include <iostream> #include <string> #include <cmath> using namespace std; int main() { int x = 9; double sq = sqrt(x); cout << "The square root of " << x << " is: " << sq << endl; return 0; }
The output of above code after compilation is:
Comments are closed.