Ch6C++ Functions
Ch6C++ Functions
LANGUAGE
C++ Functions
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:
Dividing a complex problem into smaller chunks makes our program easy to understand
and reusable.
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.
// function declaration
void greet() {
cout << "Hello World";
}
Here,
In the above program, we have declared a function named greet(). To use the
greet() function, we need to call it.
int main() {
// calling a function
greet();
EXAMPLE 1: DISPLAY A TEXT
#include <iostream>
// declaring a function
void greet() {
int main() {
return 0;
}
FUNCTION PARAMETERS
int main() {
#include <iostream>
using namespace std; int num1 = 5;
double num2 = 5.5;
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.
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,
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.
EXAMPLE 3: ADD TWO NUMBERS
#include <iostream>
int sum;
using namespace std;
// calling the function and storing
// the returned value in sum
sum = add(100, 78);
// declaring a function
int add(int a, int b) { cout << "100 + 78 = " << sum << endl;
int main() {
In the above program, the add() function is used to find the sum of two
numbers.
We pass two int literals 100 and 78 while calling the function.
We store the returned value of the function in the variable sum, and then we
print it.
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:
int sum;
#include <iostream>
// calling the function and storing
// the returned value in sum
using namespace std; sum = add(100, 78);
// function definition
int main() {
int add(int a, int b) {
return (a + b);
}
BENEFITS OF USING USER-DEFINED FUNCTIONS
Functions make the code reusable. We can declare them once and use
them multiple times.
Functions make the program easier as each small task is divided into a
function.
Functions increase readability.
C++ Library Functions
Library functions are the built-in functions in C++ programming.
Programmers can use library functions by invoking the functions directly; they don't need to
write the functions themselves.
Some common library functions in C++ are sqrt(), abs(), isdigit(), etc.
In order to use library functions, we usually need to include the header file in which these
library functions are defined.
For instance, in order to use mathematical functions such as sqrt() and abs(), we need to
include the header file cmath.
EXAMPLE 5: C++ PROGRAM TO FIND THE SQUARE ROOT OF A NUMBER
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double number, squareRoot;
number = 25.0;
cout << "Square root of " << number << " = " << squareRoot;
return 0;
}
In this program, the sqrt() library function is used to calculate the square
root of a number.
The function declaration of sqrt() is defined in the cmath header file. That's
why we need to use the code #include <cmath> to use the sqrt() function.
TYPES OF USER-DEFINED FUNCTIONS
IN C++
USER-DEFINED FUNCTIONS CAN BE
CATEGORISED AS:
For better understanding of arguments and return in functions,
Function with no argument and no return value
Function with no argument but return value
Function with argument but no return value
Function with argument and return value
Consider a situation in which you have to check prime number. This
problem is solved below by making user-defined function in 4 different
ways as mentioned above.
EXAMPLE 1: NO ARGUMENTS PASSED AND NO RETURN VALUE
# include
<iostream> // Return type of function is void
because value is not returned.
using namespace std;
void prime()
{ if (flag == 1)
{
void prime(); int num, i, flag = 0; cout << num << " is not a
prime number.";
cout << "Enter a positive }
int main() integer enter to check: "; else
{ cin >> num; {
cout << num << " is a
// No argument is for(i = 2; i <= num/2; ++i) prime number.";
passed to prime() { }
if(num % i == 0) }
prime(); {
return 0; flag = 1;
break;
}
}
}
In the above program, prime() is called from the main() with no arguments.
prime() takes the positive number from the user and checks whether the
number is a prime number or not.
Since, return type of prime() is void, no value is returned from the function.
EXAMPLE 2: NO ARGUMENTS PASSED BUT A RETURN VALUE
#include <iostream>
if (flag == 1) / Return type of function is int
using namespace std;
{ int prime()
cout<<num<<" is {
int prime();
not a prime number."; int n;
}
int main() else printf("Enter a positive integer to
{ { check: ");
int num, i, flag = 0; cout<<num<<" is cin >> n;
a prime number.";
// No argument is
passed to prime()
} return n;
num = prime();
return 0; }
for (i = 2; i <= num/2;
}
++i)
{
if (num%i == 0)
{
flag = 1;
break;
}
}
In the above program, prime() function is called from the main() with no
arguments.
prime() takes a positive integer from the user. Since, return type of the
function is an int, it returns the inputted number from the user back to the
calling main() function.
Then, whether the number is prime or not is checked in the main() itself
and printed onto the screen.
EXAMPLE 3: ARGUMENTS PASSED BUT NO RETURN VALUE
#include <iostream>
Then, num is passed to the prime() function where, whether the number is
prime or not is checked and printed.
Since, the return type of prime() is a void, no value is returned from the
function.
EXAMPLE 4: ARGUMENTS PASSED AND A RETURN VALUE .
#include <iostream>
using namespace std; /* This function returns integer value. */
int prime(int n)
int prime(int n); {
int i;
int main()
for(i = 2; i <= n/2; ++i)
{
{
int num, flag = 0;
cout << "Enter positive integer
if(n % i == 0)
to check: "; return 1;
cin >> num; }
// Argument num is passed to
check() function return 0;
flag = prime(num); }
if(flag == 1)
cout << num << " is not a
prime number.";
else
cout<< num << " is a prime
number.";
return 0;
}
In the above program, a positive integer is asked from the user and stored
in the variable num.
Then, num is passed to the function prime() where, whether the number is
prime or not is checked.