C++ Program to create a function with arguments and a return value


Any programming language that uses functions has code that is simpler, more modular, and simpler to change while being debugged. Functions are a remarkably beneficial component in a modular piece of code. A function's ability to accept arguments and output results. It is not necessarily necessary for a function to accept inputs and to always produce a result. There are numerous instances where functions only take a few inputs and don't return anything. does not always respond and will not tolerate disputes. This article will explain how to create C++ programmes that use functions, which accept a number of arguments and produce a result after processing.

Function with arguments and also return values

To define a such function with a few arguments and return a value to the invoker function (invoker functions are the caller function which calls our function to execute certain operations), the return type must be of a specific type, not a void, and there must be a given argument list in the parameter list

Syntax

<return type> function_name ( <type1> argument1, <type2> argument2, … ) {
   // function body
}

In the following example, we take the number as argument passing, then calculate the factorial of the given number, and return the result. Let us see the algorithm and the implementation in C++.

Algorithm

  • Define a function factorial(), this will take n as argument
  • fact := 1
  • while n > 1; do
    • fact = fact * n
    • n = n - 1
  • end while
  • return fact
  • End function body
  • call factorial() and pass n to find factorial of n

Example

#include <iostream> using namespace std; long factorial( int n ) { long fact = 1; while ( n > 1 ) { fact = fact * n; n = n - 1; } return fact; } int main() { cout << "Factorial of 6 is: "; long res = factorial( 6 ); cout << res << endl; cout << "Factorial of 8 is: "; res = factorial( 8 ); cout << res << endl; cout << "Factorial of 12 is: "; res = factorial( 12 ); cout << res << endl; }

Output

Factorial of 6 is: 720
Factorial of 8 is: 40320
Factorial of 12 is: 479001600

Another example to check whether a number is palindrome or not using function. We pass a number as argument, and the function will return true when it is palindrome, and false when it is not a palindrome.

Algorithm

  • define a function solve(), this will take n
  • sum := 0
  • temp = n;
  • while n > 0, do
    • rem := n mod 10
    • sum := (sum * 10) + rem
    • n := floor of (n / 2)
  • end while
  • if sum is same as temp, then
    • return true
  • otherwise
    • return false
  • end if

Example

#include <iostream> #include <sstream> using namespace std; string solve( int n ) { int sum = 0; int temp = n; int rem; while( n > 0) { rem = n % 10; sum = (sum * 10) + rem; n = n / 10; } if( temp == sum ) { return "true"; } else { return "false"; } } int main() { cout << "Is 153 a palindrome? " << solve( 153 ) << endl; cout << "Is 15451 a palindrome? " << solve( 15451 ) << endl; cout << "Is 979 a palindrome? " << solve( 979 ) << endl; }

Output

Is 153 a palindrome? false
Is 15451 a palindrome? true
Is 979 a palindrome? true

Conclusion

Using functions while writing codes makes code modular and there are several advantages while debugging or using someone else’s code. There are different modes of functions which sometimes take arguments from the invoker function and returns result to the invoker function. Sometimes it does not take any input but returns a value. In this article we have seen with few examples that how to write functions that takes argument as well as return value to the caller function. Using functions are quite straightforward and easy to implement. It is always good to use functions while writing code to reduce unnecessary code duplication in many applications.

Updated on: 19-Oct-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements