Functions are statements performing various tasks. They have blocks of codes outside the main function and fulfill the assigned task whenever they are called.
The 3 characteristics of a function are:-
Function name : The name of the function has to be unique. The name of the function can not be the same as a predefined function and the same program can not have 2 functions with the same function name.
Return type : Every function returns a data type. The return type can be of any data type like void, int, bool, float, etc..
Number of arguments :- A function can be given several arguments of various data types. These data types can be passed by reference or by value (We’ll be looking at pass by reference and pass by value later in this article).
Suppose we have to find factorials several times, then we write the whole block of code for factorials every time we need the factorial of any number. To reduce this repetition of code, we use functions. In the below example, we create a factorial function with the number as the argument and the return type can be int.
Let's look at 2 examples, in the first one we won't be using functions:-
Now lets see how to calculate factorial with a function :-
Function name :- fact.
Return type :- int.
Number of arguments :- 1.
We can see that the code with functions is significantly shorter and is more convenient.
Whenever a function is called in a main function or any other function, it needs to be defined or declared before the function which is invoking it. Otherwise, the calling function cannot find the called function, and it will lead to a compile time error. If there are any parameters to be given to the function, they are also passed within the parenthesis.
We can see how a function is defined and declared before the calling function in the example given earlier.
Lets see how the function can be declared before the calling or invoking function and defined later on :-
Here we can see that the function was declared before the calling function that is the main function ( because factorial function is invoked in the main function ) and the function was defined after the calling function.
We can declare the functions inside the functions they are being called. These functions are local functions and are called only via the function in which they are created.
Here we can see that the function hello is declared and executed inside the main function.
Whenever we execute a program, the variables and codes are all stored in a memory pool. The memory pool is of 2 major types :-
Stack Memory :- Variables declared statically i.e. the size is known at compile time.
Heap Memory :- Variables that are created dynamically i.e. their size is unknown at compile time.
The functions keep stacking one above the other in the order of their calls. When the function returns, it is erased from the stack and all its local variables are destroyed. The variables that are present in one function are local to only that function. It cannot be accessed outside that function.
For instance a variable made in the main function is not available to the add function unless we pass it as a parameter to it. However, using pass by reference, a variable defined in one function can be accessed in other functions.
Output :- After swapping by value : a= 10 and b= 20
After swapping by reference : a= 20 and b= 10
Here we can see that only when the variables were passed by reference =, the changes were valid outside that function. Whenever a variable is declared by
Let's look another example :-
Output :- The value of a is 20
The value of b is 20
Here we can see that only the value of the variable which is passed by reference is changed.
Pass by Value
Pass by Reference
Changes in the formal parameters are not reflected back to the actual parameters
Changes in the formal parameters are reflected to the actual parameters.
New variables are created.
Already existing variables are given new names.
Happy Coding 😊
By Programmers Army
Contributed by: Tanmay Garg