Enrich your knowledge with our informative blogs
A Function contains a set block of statements that are written outside of the main program and are executed to perform a desired set of operations.
This function may or may not take data from the primary function.
Using functions the modularity of the program is boosted as a large program gets divided into smaller sections. This makes the code optimized, reusable and scalable too.
Functions are known by different names in different programming languages such as methods, subroutines, procedures etc.
There are primarily two types of Functions:
1). In-built or Pre-defined Functions or Library functions
All the programming languages contain some built-in functions such as standard input/output functions, maths functions etc. These functions are listed in files called as system libraries.
To use a function, you need to add that library in your code. This helps in writing the code error free.
The system library is usually included in the code before the main() function begins.
For example: sqrt(); function is used to find the square root of any number and is defined in the library math.h header file in C programming language.
This function can easily be called as:
x = sqrt (y); (y should be positive)
Here, square root of y is calculated and the returned value is stored in the variable x.
2). User-defined functions
These functions are defined and created by the programmer or the person doing the coding.
Functions are properly created and tested as per a working and optimal algorithm before going for the final builds.
You don’t have to include any header file and the functions are defined in the same file containing the main() function.
Now depending on the arguments passed and return values the functions fall in four different categories:
1. Without Parameters and without return values
As, there is no return value, the return type of the function becomes “void”.
For example: Let’s code a program in C to add two numbers without passing any arguments.
Let’s understand the flow of control:
2. Without parameters but with return values
User creates a function that returns a value but no parameters are passed.
Understand the same addition program defined using without using parameters that returns an integer value.
Flow of control:
3. With parameters and without return values
Here parameters are sent in the function but nothing is returned. This makes the return type “void”.
One thing is to remember here is to ensure that the datatypes of the variables that are passed should be same as that defined in the function. Else the compiler will give an error.
Check the same addition function made using the parameter values but does not returns anything.
Things to consider:
Since the function sum() is uses two parameters that are integers. The function definition should also contain only two parameters that are integer.
Any mismatch will give error. The function will not work.
Always remember:
void sum (int, int); and void sum (int, int, int); and int void (int,int); are all different. You need to be very careful about what exactly you want your function to do.
4. With both parameters and return values
Now, let’s see how to make a function that performs addition using return type as well as with parameters.
Flow of control:
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.