Enrich your knowledge with our informative blogs
Functions are the block of statements encapsulated together and are designed to achieve a specific outcome.
The values that are passed are called the parameters or arguments and the function may or may not return a value.
Different programming languages have different names for functions such as procedures, methods etc.
Why do we use functions?
What happens when a function is called?
The moment the control of the program reads the function statement, it leaves the current section and shifts to first line of the called function.
Let’s understand the flow of control in a function.
Step1: The program reads the function call.
Step2: The control searches the function and then shifts to the first line of that called function.
Step3: All the statements/instructions are executed sequentially from top to bottom.
Step4: After the last statement, the control again shifts back to the origin point (calling function) from where it started.
Step5: If the function was designed to return a value, the control takes the value along with it to the primary program from where it started.
There are mainly two types of functions:
1). Built-in functions
Built-in functions are those functions that are already defined in the existing libraries of the programming language.
For example: Consider C language, printf() and scanf() are two inbuilt functions defined in the library stdio.h.
Where printf() sends everything which is passed as arguments to the console screen to be displayed, scanf() is responsible for scanning a user input.
While printf() does not returns any value, scanf() returns whatever input is given by user using a keyboard as “return value”.
2). User-defined functions
Here the functions are defined by the programmer or the coder to make the program optimized and modular.
It may happen that the code might be required to expand somewhere in the future, functions take the charge of everything.
It’s easier to scale the program with the help of functions; else it would be a daunting task and make the code look clumsy and cluttered.
Let’s see how functions are defined:
Defining a Function
In C language, the general syntax of a program is:
return_type name_of_function ( arguments ) {
statments_to_executed (body of the function)
return (expr);
}
Understanding parts of the function:
For example:
If you plan to make a calculator using a C code, you may have initially planned for just basic functions such as addition, subtraction, multiplication and division.
Using the same in code is easy and can be done by simple using switch statements.
But in case, you want to expand you code in future and would like to add some more operations such as trigonometry or logarithms, using functions makes the things sorted.
You can easily make function declarations such as:
int add( int a,int b ); // for addition
int subs( int a ,int b); // for subtraction
int mult( int a ,int b); // for multiplication
int divide( int a ,int b); // for division
float logs( float a ,float b); // for logarithm
float trigno( float a ,float b); // for trigonometric function
Things to consider before writing the function
The functions can call other functions and even themselves (recursion).
Make sure to go more sensibly with the functions. Do not keep on creating functions unnecessarily that call other functions, which further call some more.
The reason is whenever the program control reads the function, it pauses the current program to make the called function run.
The main program is held in the active memory. So, if more functions are called within the functions, memory consumption increases.
This turns out to be a real bad programming tactic. You need to be very careful while using functions as a slight blip may make your program go out of hand.
Read More – Coding and Programing Questions
View More – Useful links for Your Child’s Development
Unleash the power of true logic building with Real-time instructions and live coding exposure.