What is function?
A function is a block of code that performs some operation. A function
can optionally define input parameters that enable callers to pass
arguments into the function. A function can optionally return a value as
output.
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:
1. Standard Library Functions: Predefined in C++
2. User-defined Function: Created by users
Built-in Functions/ Standard Library Function:
These are functions that are already present in C++; their definitions are already
provided in the header files. The compiler picks the definition from header files and uses
them in the program.
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";
}
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 {}
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();
Example 1: Display a Text
#include <iostream>
using namespace std;
// declaring a function
void greet() {
cout << "Hello there!";
}
int main() {
// calling the function
greet();
return 0;
}
Why Use Functions in C++?
Functions are used to minimize the repetition of code, as a function allows you to write
the code inside the block. And you can call that block whenever you need that code
segment, rather than writing the code repeatedly. It also helps in dividing the program
into well-organized segments.
---------.-------------------------------.---------------------------------------------------.--------------.
Return Values
The void keyword, used in the previous examples, indicates that the function
should not return a value. If you want the function to return a value, you can use
a data type (such as int, string, etc.) instead of void, and use
the return keyword inside the function:
Example
int myFunction(int x) {
return 5 + x;
}
int main() {
cout << myFunction(3);
return 0;
}
// Outputs 8 (5 + 3)
FUNCTION RETURN VALUES:
A function that returns a value is called a value-returning function. A
function is value-returning if the return type is anything other than
void . A value-returning function must return a value of that type (using
a return statement), otherwise undefined behavior will result.