- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to create a function with arguments but without a return value
Functions in functional programming are used to create modular codes. We construct submodules in many applications to make our code simple to write, simple to debug, and also efficient by avoiding writing needless code repeatedly. Functions have a role in achieving these traits. Functions frequently take arguments and produce a result. It occasionally might not accept any arguments but still produce something. There are several rare circumstances when functions take a few inputs but don't return anything. This course will discuss C++ methods that accept arguments but nothing is returned.
Function with arguments but no return value
To define a such function with arguments but no return type, the return type must be void, and there must be at least one argument in the parameter list
Syntax
void function_name ( <type> argument1, <type> argument2, … ) { // function body }
In such a scenario, to produce output we just print something, or performing any displaylike operations, or perform some task whole inside the function without showing any output. Let us see one of such examples and let us see the implementation in C++. In our example, we will print a star pyramid for n number of lines where n is the argument passes through the function.
Algorithm
- Define a function pyramid(), this will take an integer n
- for initialize i := 1, when i <= n, update (increase i by 1), do −
- for initialize j := 1, when j <= n - i, update (increase j by 1), do −
- display blank space
- end for
- for initialize j := 1, when j <= i, update (increase j by 1), do −
- display "*"
- end for
- go to the new line
- for initialize j := 1, when j <= n - i, update (increase j by 1), do −
- end for
- End function body
- Call pyramid()
Example
#include <iostream> #include <sstream> using namespace std; void pyramid( int n ) { for( int i = 1; i <= n; i++ ) { for( int j = 1; j <= n - i; j++ ) { cout << " "; } for( int j = 1; j <= i; j++ ) { cout << "* "; } cout << endl; } } int main() { pyramid( 15 ); }
Output
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
This program, it is printing the pyramid for n lines, here n is 15. The line number is passed through the argument. Since it is printing the asterisks directly, nothing is returned. Let us see another example where we take one number and display the reverse of it using function call.
Algorithm
- Define reverse() function which takes a number n
- rev := 0
- while n > 0, do
- rev := (rev * 10) + (n mod 10)
- n := floor of (n / 10)
- end while
- display rev
- End Function
- call reverse() function with any number and it will display the reverse of it.
Example
#include <iostream> #include <sstream> using namespace std; void reverse( int n ) { int rev = 0; while( n > 0 ) { rev = (rev * 10) + n % 10; n = n / 10; } cout << rev << endl; } int main() { cout << "Reverse of 14586 is: "; reverse( 14586 ); cout << "Reverse of 1250 is: "; reverse( 1250 ); cout << "Reverse of 15651 is: "; reverse( 15651 ); }
Output
Reverse of 14586 is: 68541 Reverse of 1250 is: 521 Reverse of 15651 is: 15651
Here also the number is taken from the invoker function as argument and then directly displays the result, so nothing is being returned.
Conclusion
Functions are used to make code modular and easy to handle. In most cases, we use functions to take arguments and return some value after certain computation. But this is not a mandatory process. In this article, we discussed how a function can be written in C++ which takes argument but does not return anything. Like in our first example, the star pyramid is printed for n lines where n is the input and it shows the result directly. In the second example, we are taking the number as argument passing, then convert this into its reverse and without return back the result, it shows the result on the console.