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 valueto 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
语法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.
algorithmdefine a function pyramid(), this will take an integer n用于初始化 i := 1,当 i <= n 时,更新(增加 i 1),执行 −for initialize j := 1, when j <= n - i, update (increase j by 1), do −显示空白空间end forfor initialize j := 1, when j <= i, update (increase j by 1), do −display *end for去到新的一行end for结束函数体调用 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 );}
输出 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
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.
algorithmdefine reverse() function which takes a number nrev := 0while n > 0, dorev := (rev * 10) + (n mod 10)n := floor of (n / 10)end whiledisplay rev结束函数<li>使用任意数字调用reverse()函数,它将显示该数字的反转。</li>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 );}
输出reverse of 14586 is: 68541reverse of 1250 is: 521reverse of 15651 is: 15651
这里还是从调用函数中获取数字作为参数,然后直接显示结果,所以没有返回任何内容。
结论函数用于使代码模块化和易于处理。在大多数情况下,我们使用函数来接收参数并在某些计算后返回某个值。但这不是强制性的过程。在本文中,我们讨论了如何在c++中编写一个接收参数但不返回任何内容的函数。就像在我们的第一个例子中,星星金字塔是根据输入的n行打印出来的,并直接显示结果。在第二个例子中,我们将数字作为参数传递,然后将其转换为其反向形式,并且不返回结果,而是在控制台上显示结果。
以上就是创建一个带有参数但没有返回值的c++程序的详细内容。