Featured Post

How Computers Understand Human Language?

How To Code? Functions in Programming

what is a function in programming
unsplash-logoFabian Grohs

What is a function in programming?

A function in computer programming is just a block of related and reusable code which performs a single task or activity.
Function is also called method, procedure, sub-routine etc. I will try to cover each and every aspect of functions here. I have divided this post into two sections. One section explains the structural components and the other explains non-structural components. I am categorizing these things because I think it will help you better understand functions. These categories are not official. Tell me in the comments whether you liked the categorization or not. Based on your response, I will update this post.

Structural Components: 

There are four structural components of a function:
  1. Name
  2. Body
  3. Input
  4. Output

1. Name:

Function name is the same as a variable name. It follows the same conventions a variable name follows. Special characters and spaces are not allowed in function name. Name of a function is used latter in the code to call/execute the function. Name of a function should reflect the type of task it is performing. That's it. There is no rocket science. Just follow naming conventions.

2. Body

Body of a function consists of the lines of code which you want to execute whenever the function is called. It could be of any size, depends on your problem and your logic. But the body must contain only those lines which are related to each other and solve a single, independent sub-problem of your problem. Read the definition again, the block of code must be reusable. For example, you have to write code for some kind of mathematical equation like
c = a2 + b3
    Here, calculating square is one sub-problem while cube is another sub-problem. There will be two separate functions for each.

    3. Input

    The code in function body performs some operation and it might need data to operate on. In above example, you will need to specify the number you want to take square of. Same is the case with the cube function. The best approach to give input to a function is to use parameters. Parameters are also called arguments and are given during function call. For now, just remember that input to any function is given via parameters. I am going to explain it latter in this post.

    4. Output

    The output of a function is the result that we get after execution of the function. It could be in the form of a value or set of values, screen prints, files or combination of any of these or any other format. If the output value of a function is needed at the point where the function is called, then return is used. Although, it is not necessary for a function to return some value but in order to transfer data from one scope to another, return is used. An opening and closing brackets make a scope. Variables declared inside a scope are accessible only in that scope and its sub-scopes. It is often called lifespan of data/variables.

    Non-Structural Components:

    1. Function Definition
    2. Function Call
    3. Function Declaration (it is optional in many programming languages that's why it is on 3rd

    1. Function Definition

    Function definition contains two things: 
    • function signature
    • function body
    • Function Signature 
    Function signature consists of function name, types and numbers of parameters (input) and type of return value (output). These three things as a whole are called function signature because compiler or interpreter differentiate between functions using their signatures. If you are complete beginner and can't fully understand what I am trying to say, just believe me and remember that function signature contains function name, number of parameters and type of each parameter and return/output value type and it is used by compiler to recognize the function. These information will help you in function overloading.

    In order to define a function, you have to give it a name (should be meaningful) first. You need to specify the number and type of parameters if there are any. Parameters are placeholders for the input values which the function expects from function call. A parameter is defined the same way a variable is defined. It has a type and a name. The value then comes from function call.
    In mathematical equation example above, the square function could take float value or integer value but not a character value. Type and number of parameters are specified during function definition.
    •  Function Body
    Function body is just a block of code which performs the actual task you want it to do. Make sure, each function you define is an independent unit. What is meant by independent unit? It means that you can move each function to another project and it works there with no or little modifications. Try not to use any global variables inside a function. Provide all the input data using parameters and get the result using return value or reference parameters.

    2. Function Call

    Function call is a statement which specifies the name of the function to execute and values for input parameters defined in function definition. It also gives you the value which is returned by a function. You can store that value in a variable or use is directly as you use variables. For example a function which returns integer value can be used anywhere where integer variable could be used.
    Values that you mention in function call are copied to parameters in the function definition. Note that the sequence and type of values must the same as it is in the function definition.

    3. Function Declaration

    Function declaration is only one statement which specifies the function signature. It is function definition without a body. It is optional in many languages but still you should have an idea what it is. You might need it at some point.

    why do we use functions in programming?

     Functions are used because functions:
    1. are reusable units
    2. reduces code size
    3. makes debugging easier
    4. helps to make the code more modular
    5. makes the code easier to maintain
    Next: Types Of Functions In Programming


    Subscribe to our YouTube channel:

    Comments