functions 01
functions 01
net/publication/360237425
C++ Functions
CITATIONS READS
0 8,697
1 author:
Farhadullah Shinwari
Ganpat University, U.V.Patel College of Engineering
3 PUBLICATIONS 0 CITATIONS
SEE PROFILE
All content following this page was uploaded by Farhadullah Shinwari on 28 April 2022.
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,
Note: We will learn about returnType and parameters later in this tutorial.
Calling a Function
int main() {
// calling a function
greet();
#include <iostream>
using namespace std;
// declaring a function
void greet() {
cout << "Hello there!";
}
int main() {
return 0;
}
Output
Hello there!
Function Parameters
int main() {
int n = 7;
return 0;
}
#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;
return 0;
}
Output
C++ function
with parameters
Note: The type of the arguments passed while calling the function must match
with the corresponding parameters defined in the function declaration.
Return Statement
In the above programs, we have used void in the function declaration. For
example,
void displayNumber() {
// code
}
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.
#include <iostream>
// declaring a function
int add(int a, int b) {
return (a + b);
}
int main() {
int sum;
// calling the function and storing
// the returned value in sum
sum = add(100, 78);
return 0;
}
Output
100 + 78 = 178
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.
Working of
C++ Function with return statement
Notice that sum is a variable of int type. This is because the return value
of add() is of int type.
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);
}
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.
#include <iostream>
// function prototype
int add(int, int);
int main() {
int sum;
return 0;
}
// function definition
int add(int a, int b) {
return (a + b);
}
Output
100 + 78 = 178
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.
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() ,
int main() {
double number, squareRoot;
number = 25.0;
cout << "Square root of " << number << " = " << squareRoot;
return 0;
}
Output
Square root of 25 = 5
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.
To learn more, visit C++ Standard Library functions.