GAME OF CODE’22 — DAY 4.1

Hey everyone!

Now you are going to learn about functions in C++. We hope you all are just as excited as we are!

In this blog, you are going to learn everything about functions from the scratch. By the end, y’all will be familiar with functions, passing parameters into functions and function overloading, and would be able to code functions with ease.

What is a function?

A function is a block of code that performs a specific task and runs only when it is called. Data can be passed into a function in form of parameters.

The idea behind creating a function is to put together a task that needs to be done repeatedly into a function and reuse the code by calling it as many times as required.

You people might be thinking why exactly do we need to create a function. Functions are an important part of a program.

They have some advantages:

· Functions help us in reducing code redundancy. This means that if a certain task has to be performed multiple times in the program, then instead of writing the code, again and again, we can define a function and call it wherever required.

· Functions make code modular. It is easier to read a code if it is divided into functions.

· Functions provide abstraction. This means we can use call functions wherever required without worrying about their internal functionality.

Interesting, right? Now that we are done with the basic introduction to the function, we are going to learn to declare (or create) a function.

How to declare a function?

Syntax: Return_type function_name([arg1_type arg1_name, ]){Code}

Here is an example of a function declaration:

👉🏻Here, the name of the function is goc().

👉🏻The return type of the function is void.

👉🏻The empty ‘()’ brackets mean that it doesn’t have any parameters.

👉🏻The function body is written inside ‘{}’.

As we now know how to declare a function, let’s learn how to use a function in the main function.

How to call a function?

To use a function, we need to call it. Here’s how we call a function.

This is what the entire code will look like:

How to pass parameters in a function?

Before we start, there are a few important terms which you must know.

👉🏻Actual Parameters: Parameters passed to function.

👉🏻Formal Parameters: Parameters received by function.

There are three ways to pass parameters in C++.

· Pass by Value: In this method, values of actual parameters are copied to the function’s formal parameters, and both types of parameters is stored in different memory locations. Therefore, any changes made inside the functions have no effect on the actual parameters.

· Pass by Reference: Actual and formal parameters are stored in the same locations, therefore, any changes made inside functions affect the actual parameters.

· Pass by Pointer: In this method, the memory location of the variables is passed to the parameters in the function, and then the operations are performed.

What is the main function?

The main function is a special type of function which every C++ program possesses. It serves as the entry point of code as the computer will start running the program from the beginning of the main program.

Types of the main function:

· Main Function without parameters:

· Main Function with parameters:

Q) Why do we need the parameter option for the main function?

The reason we need the parameter option for the main function is to allow input from the command line as it saves every group of characters after the program name as elements in an array named argv.

Q) Why do we need to write ‘return 0’ at the end of the main function?

It is imperative to write a return statement at the end of the code because the main function has a return type of int. The number zero informs the calling program that there was no problem in the execution of the program.

Default Arguments in C++

The automatically assigned value provided in function declaration by the compiler is called a default argument. This is assigned if the caller of the function doesn’t provide a value for the argument.

Points to Remember:

· Default arguments can be overwritten if required.

· Default arguments are overwritten when the calling function provides values for them.

· Arguments from calling function to called function are copied from left to right during the calling of function.

· Default arguments are assigned from right to left.

Advantages:

· Useful in increasing the capabilities of an existing function.

· Helps in reducing the size of program.

· Improves consistency of program.

Disadvantages:

· Execution time is increased.

Now, we know a bit about functions and can write a simple function.

Moving on, we are going to learn about the inline function.

What are Inline Functions?

A function is said to be an inline function when a copy of the code of that function gets placed at each point where the function is called at compile time. The program needs to be recompiled each time we make a change in the code of the inline function otherwise it will continue with old functionality.

To declare an inline function, we need to use the keyword ‘inline’.

Syntax:

Inline retur_type function_name(parameters)

{ //function code }

Inlining is only are request to the compiler and not a command which the compiler might reject in the following circumstances:

· If a function contains a loop.

· If a function contains static variables

· If a function is recursive.

· If the return type is something other than void and the return statement doesn’t exist in the function body.

· If a function contains a switch or goto statement.

Advantages:

· Function call overhead does not occur.

· It saves the overhead of push/pop variables on the stack when the function is called.

· It also saves the overhead of a return call from a function.

· Compiler-specific optimizations on the body of function may be enabled when we inline a function.

· May be useful for embedded systems because inline can yield less code than the function call preamble and return.

Disadvantages:

· After in-lining a function, if the number of variables increases drastically, would cause overhead on register utilization.

· Using too many inline functions will drastically increase the size of the binary executable file.

· Too much inlining can also reduce your instruction cache hit rate, thus reducing the speed of instruction fetch from that of cache memory to that of primary memory.

· May increase the compile-time overhead.

· They may not be useful for many embedded systems as size is more important than speed in embedded systems.

· Inlining functions might cause thrashing which causes the performance of the computer to degrade.

Example:

Return from Void Functions in C++

Q) What are void functions?

Functions that do not have a return type are called void functions. They are also known as non-value returning functions.

Q) Can void functions return values?

Well, they can!

Some of the cases are listed below:

· A Void Function can Return: We can write a return statement at the end of a void function. It is, in fact, a good practice to write a return statement indicating the end of the function.

Example:

· A Void Function can return another void function: A void function can return another void function while it is terminating.

Example:

· A void() can also return a void value: A void() can’t return a value that can be used but can return a value that is void without giving an error.

Example:

What if two or more functions have the same name? Will it work? What do you think?

It will work! This feature is known as function overloading. Astounding, right? Read further to know more about it.

What is Function Overloading?

Function overloading is a feature of object-oriented programming in which two or more functions can have the same name but different parameters.

You cannot overload a function that only differs by return type.

Example:

This is it for now. Hope you enjoyed this blog as much as I did while writing this. Happy learning!

Blog Credits: Rivi Vig

Post Credits: Vandana Chauhan

--

--