× About Us How it works Pricing Student Archive Subjects Blog Contact Us

Enrich your knowledge with our informative blogs

What are Functions in program?

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? 

  • Make the code clean, clear and well defined. 
  • The code is easy to debug and scale. 
  • Boosts modularity 
  • Makes the code reusable 
  • Significantly lowers down the LOC which is a great sign of good programming. 
  • Optimized code  

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:  

  1. Return_type : It defines the datatype of the value or expression that the function would be returning. If the function is not made to return a value, then it’s the return_type is “void”. 
  2. Name of the function: It’s the name of the function. Name the function using the variable naming conventions used in that program. Function name along with the arguments make the signature of the function. 
  3.  Arguments : Arguments are the parameters that act as the palceholders for the values that are passed from the primary function.  These are optional. 
  4. Body of the function: This is the main part where all the statements are executed to achieve a desired goal. 
  5. Return: In case the function needs to return a value, it is return using this statement.  

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  

  • Understand the need of the function. (Think from reusability, modularity and optimization point of view). 
  • Set the parameters and decide their datatypes. 
  • Work on the algorithm.      

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 

Unveil your gateway to a lucrative career!
Unveil your gateway to a lucrative career!

Unleash the power of true logic building with Real-time instructions and live coding exposure.

Book A Demo Class

Tel Guru
Tel Guru

Register For The Demo Class