Functions in C++ Hackerrank Solution. Write a function int max_of_four(int a, int b, int c, int d) which returns the maximum of the four arguments it receives. Functions hackerrank solution in C++. Functions are a bunch of statements glued together. A function is provided with zero or more arguments, and it executes the statements on it. Based on the return type, it either returns nothing (void) or something.
In this challenge, you will learn simple usage of functions in c. functions are a bunch of statements grouped together. a function is provided with zero or more arguments, and it executes the statements on it. based on the return type, it either returns nothing (void) or something.
The syntax for a function is
return_type function_name(arg_type_1 arg_1, arg_type_2 arg_2, ...) {
...
...
...
[if return_type is non-void]
return something of type `return_type`;
}
For example, a function to return the sum of four parameters can be written as
int sum_of_four(int a, int b, int c, int d) {
int sum = 0;
sum += a;
sum += b;
sum += c;
sum += d;
return sum;
}
Write a function int max_of_four(int a, int b, int c, int d) which reads four arguments and returns the greatest of them.
+=: Add and assignment operator. It adds the right operand to the left operand and assigns the result to the left operand.
a += b is equivalent to a = a + b;
Input Format
Input will contain four integers - a, b, c, d one per line.
Output Format
Return the greatest of the four integers.
PS: I/O will be automatically handled.
Sample Input
3
4
6
5
Sample Output
6
Functions in C++ Hackerrank Solution
#include <iostream>
#include <cstdio>
using namespace std;
int max_of_four(int a, int b, int c, int d)
{
if (a > b && a > c && a > d)
{
cout << a;
}
else if (b > a && b > c && b > d)
{
cout << b;
}
else if (c > b && c > a && c > d)
{
cout << c;
}
else
{
cout << d;
}
return 0;
}
int main()
{
int a, b, c, d, max;
cin >> a >> b >> c >> d;
max = max_of_four(a, b, c, d);
}
First, we have to understand what is a function? After that, we can use the function. Now come to the point where we have to pass the value in a function, we can do this bypass by value and pass by reference(basically we passed the address of a variable), but here we are passing the value in the function. After passing the value put some condition in the function and return the output form function. for a better understanding let's see the program step by step.
A first function declaration(optional if you are defining function above main() function). After that take a user input in this case 4 value is taken and passed the values in the function then the function will return the greatest value for four value by some statement and condition.
Passing the value in the function
max=max_of_four(a,b,c,d);
I have written a return statement after each output for the understanding purpose, that is not compulsory to write a return after each and every statement you can see method 2 full solution with one return.
Submit your solution here: Click here
Functions Hackerrank Solution in C++ with Return Statements
#include <iostream>
#include <cstdio>
using namespace std;
int max_of_four(int a, int b, int c, int d)
{
if (a > b && a > c && a > d)
{
cout << a;
return (a);
}
else if (b > a && b > c && b > d)
{
cout << b;
return (b);
}
else if (c > b && c > a && c > d)
{
cout << c;
return (c);
}
else
{
cout << d;
return (d);
}
}
int main()
{
int a, b, c, d, max;
cin >> a >> b >> c >> d;
max = max_of_four(a, b, c, d);
}
The Output of Functions Hackerrank Solution in C++
Tips: You can use the Master Header file in C++ is # includes <bits/stdc++.h> after adding this header file no need to add any other header file.
0 Comments: