C++ Unit-Ii
C++ Unit-Ii
C++ Unit-Ii
UNIT II:
C++ Functions
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
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;
}
Output
Hello there!
Function Parameters
As mentioned above, a function can be declared with parameters (arguments). A parameter is
a value that is passed when declaring a function.
For example, let us consider the function below:
void printNum(int num)
{
cout << num;
}
Here, the int variable num is the function parameter.
#include <iostream>
using namespace std;
// display a number
void displayNum(int n1, float n2) {
cout << "The int number is " << n1;
cout << "The double number is " << n2;
}
int main()
{
int num1 = 5;
double num2 = 5.5;
// calling the function
displayNum(num1, num2);
return 0;
}
Output
The int number is 5
The double number is 5.5
In the above program, we have used a function that has one int parameter and
one double parameter.
We then pass num1 and num2 as arguments. These values are stored by the function
parameters n1 and n2 respectively.
Return Statement
In the above programs, we have used void in the function declaration. For example,
void displayNumber()
{
// code
}
This means the function is not returning any value.
[email protected] iamriteshkandari 9259069231
It's also possible to return a value from a function. For this, we need to specify
the returnType of the function during function declaration.
Then, the return statement can be used to return a value from a function.
For example,
int add (int a, int b) {
return (a + b);
}
Here, we have the data type int instead of void. This means that the function returns
an int value.
The code return (a + b); returns the sum of the two parameters as the function value.
The return statement denotes that the function has ended. Any code after return inside the
function is not executed.
Notice that sum is a variable of int type. This is because the return value of add() is of int type.
[email protected] iamriteshkandari 9259069231
Function Prototype
In C++, the code of function declaration should be before the function call. However, if we
want to define a function after the function call, we need to use the function prototype. For
example,
// function prototype
void add(int, int);
int main() {
// calling the function before declaration.
add(5, 3);
return 0;
}
// function definition
void add(int a, int b) {
cout << (a + b);
}
In the above code, the function prototype is:
void add(int, int);
This provides the compiler with information about the function name and its parameters. That's
why we can use the code to call a function before the function has been defined.
The syntax of a function prototype is:
returnType functionName(dataType1, dataType2, ...);
// function definition
[email protected] iamriteshkandari 9259069231
int add(int a, int b) {
return (a + b);
}
Output
100 + 78 = 178
In C++, we can declare a function as inline. This copies the function to the location of the
function call in compile-time and may make the program execution faster.
Inline Functions
To create an inline function, we use the inline keyword. For example,
inline returnType functionName(parameters) {
// code
}
Notice the use of keyword inline before the function definition.
Output
5
8
666
Here is how this program works:
[email protected] iamriteshkandari 9259069231
Here, we created an inline function named displayNum() that takes a single integer as a
parameter.
We then called the function 3 times in the main() function with different arguments. Each
time displayNum() is called, the compiler copies the code of the function to that call location.