Skip to main content

Functions

Why do we use arguments and return type in a function?

Asking questions strengthen the concepts. So, Never hesitate to ask why behind something. ✌️

Use of arguments and return type in a function

Using arguments and return values in a function allows us to create a piece of code that can be used independently. It's like you create your notes for interview preparation and you can use these notes multiple times before any interview. You don't have to make your notes again and again before every interview. Isn't it very useful? Indeed it is. 😀

Let's understand this with an example of a function we write to add two numbers:

int AddNumbers(int n1, int n2) {
    int result = n1 + n2;
    return result;
}

Anyone can use this function without knowing how it works internally. They just need to know two things:

  1. Arguments: What values to provide when using the function (in this case, two numbers)?
  2. Return Value: What the function will give back as a result (in this case, the sum of the two numbers)?

We have been using many functions from the very beginning of this course without knowing how they work. Think about the main() function which we've been using from the beginning of this course. We don't know exactly how it works inside, but we can use it.

So, when you create a function to solve a problem, always consider two questions:

  1. What arguments will the function take?
  2. What value should the function return?

This way, your function becomes a handy tool that others (or even you) can use without worrying about the internal details.

Common Mistakes for Beginners

When we are a beginner, we make mistakes. That's a good thing because that's how we learn and improve.

Mismatched Return Type

Consider this example:

#include <iostream>
using namespace std;
 
int MultiplyNumbers(double number1, double number2) {
        
    double product = number1 * number2;
    return product;
    
}
 
int main() {

    int result = MultiplyNumbers(6.5, 3.5);
    cout << result;
 
    return 0 ;
}

What will be the output of this program?? Give it a try !!

Output

22

This program will print 22 instead of 22.75. This is because the function returns a double variable and we are storing it in int data type.

Here, you have a function to calculate the product of two numbers, and it returns a decimal. But, oops !! You try to catch it in a whole number i.e. int data type. It's like trying to fit a watermelon into a coffee mug – it just won't work. This program will print 22 instead of 22.75. This is because the function returns a double variable and we are storing it in int data type.

This mismatch in return type results in wrong output which we definitely don't want.

Not Using the Return Value Correctly

Now, think of a function that figures out the area of a rectangle. It calculates it perfectly (let's say 24 square units), but you forget to write it down or show it off. It's like baking a delicious cake and leaving it in the kitchen without storing it in an appropriate place like a refrigerator. Your cake will be of no use sadly due to a small mistake. 🥹

#include <iostream>
using namespace std;
 
double CalculateArea(double length, double width) {
        double area = length * width;
        return area;
}
 
int main() {

    CalculateArea(6, 4);
 
    return 0 ;
}

The function calculates, but since we don't capture or showcase the result, it's like the area never existed.

Don't you think it's like a situation where you study for your exams the whole year but in your final exams you don't write the answers properly and hence get a bad score? It is a very sad situation. So, Please try to avoid this mistake in coding as well.
Note: If a function hands you something valuable, make sure to keep it safe or share it somehow.

Incorrect Number of Arguments

Think of a function like ordering a cake. While placing the order you say, "Give me a large cake," it's cool. But if you say, "Give me a chocolate cake with Oreo and KitKat topping," and suppose they only hear "large cake," you might end up with a plain cake. So, always provide the inputs carefully !!

#include <iostream>
using namespace std;
 
double CalculateCost(double baseCost, double toppingCost) {
        double totalCost = baseCost + toppingCost;
        return totalCost;
}
 
int main() {

    double result = CalculateCost(15.0);
    cout << result;
 
    return 0 ;
}

Here, you will get an error. Think yourself why would you get that??

You will get the error because the CalculateCost() function wants two things, but we only provided one during the order.

Note: Understanding these coding scenarios helps prevent headaches and keeps your code running smoothly! Why to do common mistake which are already commited by others? Shouldn't we learn from them and avoid them. ✌️😄

See you in the next article with more energy and excitement ✌️ till then Happy Learning !! 😀