Functions HackerRank solution in c++

Functions can come really useful in task grouping as the functions are a bunch of statements glued together. This tutorial will help you with the Functions HackerRank solution in c++.

Functions HackerRank solution in c++

Problem statement

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 or something…

You can check out the complete problem on Functions at HackerRank.

Also Read: Hackerrank problem on Variable sized Arrays


Here a function named max_of_four is created with a return type of int type, which takes four arguments and compares among them to find the maximum of all.

First, it compares a with all the numbers, if that’s positive then a is the maximum of all. If that’s not the case, it does the same process with b and c respectively, if the results are negative then it returns d as the maximum number.

Later in the main function, it takes four integer numbers as input and calls the previously declared function, passing the arguments. The maximum number is returned as result and printed using cout.

#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){
        return a;
    else if(b>a && b>c && b>d){
        return b;
    else if(c>a && c>b && c>d){
        return c;
        return d;
int main() {
    int x, y, z, j;    
    cin >> x >>y >>z>> j;
    int max = max_of_four(x, y, z, j);
    cout << max;
    return 0;
Sharing Is Caring:
Soumya Mondal

An aspiring BTech Electronics and Communication student, obsessed with Coding, Gadgets, Blogging, & Digital Marketing.

Leave a Comment