Â
Definition of functions:-
A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. It is a unit of code that is often defined by its role within a greater code structure. Specifically, a function contains a unit of code that works on various inputs, many of which are variables, and produces concrete results involving changes to variable values or actual operations based on the inputs.
*Note:- Functions are also called as Methods, so don't get confused when methods are used instead of functions.
Need of functions in Programming:-
1. To improve the readability of code.
2. Improves the reusability of the code, same function can be used in any program rather than writing the same code from scratch.
3. Debugging of the code would be easier if you use functions, as errors are easy to be traced.
4. Reduces the size of the code, duplicate set of statements are replaced by function calls.
5. We can divide a large program into multiple small modules.
6. Function Hide the complexities.
Types of Functions in C programming language:-
1. Pre-defined Functions
2. User-defined Functions
Pre-defined Functions:-
These are the functions that are built into Language to perform operations & are stored in the Standard Function Library.
Some examples of pre-defined functions in C++ are cin, cout, sqrt, cbrt, strcmp, etc.
User-defined Functions:-
User-defined functions are defined by the user to perform specific tasks. There are four different patterns to define a function −
-Functions with no argument and no return value.
-Functions with no argument but a return value.
-Functions with argument but no return value.
-Functions with argument and a return value.
Syntax of declaring functions/methods in C++:
1. Functions with no argument and no return value.
datatype function_name()
{
// body of function
}
2. Functions with no argument but a return value.
data_type function_name()
{
// body of function
return variable_name;
}
3. Functions with argument but no return value.
data_type function_name(data_type argument_name)
{
// body of function
}
4. Functions with argument and a return value.
data_type function_name(data_type argument_name)
{
// body of function
return variable_name1;
}
Syntax of Calling functions/methods in C++:
1. Functions with no argument and no return value.
int main()
{
function_name();
}
2. Functions with no argument but a return value.
int main()
{
printf(function_name());
}
// OR
int main()
{
variable_name = function_name();
printf(variable_name);
}
3. Functions with argument but no return value.
int main()
{
function_name(argument_name);
// Passing the value of variable_name to function_name
}
4. Functions with argument and a return value.
int main()
{
printf(function_name(argument_name));
}
Examples for functions in C++:
In all the below examples we are going to see how to write a program to find sum of 1 to n natural numbers using the above four patterns of functions.
1. Functions with no argument and no return value.
// 1. 'no argument' and 'no return '
#include <iostream>
using namespace std;
void show_sum()
{
int n;
cout << "Enter the number: ";
cin >> n;
int sum = 0;
for (int i = 1; i <= n; i++)
{
sum += i;
}
cout << "Sum of natural numbers from 1 to " << n << " is: " << sum << endl;
}
int main()
{
show_sum();
return 0;
}
2. Functions with no argument but a return value.
// 2. 'no argument' but 'return '
#include <iostream>
using namespace std;
int n;
int show_sum()
{
cout << "Enter the number: ";
cin >> n;
int sum = 0;
for (int i = 1; i <= n; i++)
{
sum += i;
}
return sum;
}
int main()
{
cout << "Sum of natural numbers from 1 to " << n << " is: " << show_sum() << endl;
return 0;
}
3. Functions with argument but no return value.
// 3. 'argument' but 'no return '
#include <iostream>
using namespace std;
void show_sum(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
{
sum += i;
}
cout << "Sum of natural numbers from 1 to " << n << " is: " << sum << endl;
}
int main()
{
int n;
cout << "Enter the number: ";
cin >> n;
show_sum(n);
return 0;
}
4. Functions with argument and a return value.
// 4. 'argument' and 'return '
#include <iostream>
using namespace std;
int show_sum(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
{
sum += i;
}
return sum;
}
int main()
{
int n;
cout << "Enter the number: ";
cin >> n;
cout << "Sum of natural numbers from 1 to " << n << " is: " << show_sum(n) << endl;
return 0;
}
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP