What is Function in C Programming Language?

Welcome back guys, in this module we are going to talk about what is function in C programming language in detail, how to declare functions, what is their uses, advantages, and disadvantages, and many more. Till now we have seen various interesting concepts in our previous modules.

So, let’s start with this important topic,

What is Function in C Programming Language?

Suppose, you are working on a large project, and in that, you have written 1000 lines of code and even more, there is some piece of line which are getting repeated again and again in the code, so this is creating ambiguity in the program and so avoid this ambiguity you can create functions, so that the lines of code which are getting repeated, again and again, you write that line of code in that function, and whenever you have to use that lines of code, you can simply call that particular function in place of that. This makes the code look beautiful and also more efficient.

What is Function in C Programming Language

Therefore, the function is a block of code, which is being called in a program whenever needed. It gets only executed when it is called in the program. It is basically dividing up your code into smaller fragments called functions. The collection of functions constitutes a program.

Every C Program has at least one function i.e., main( ) function, from which the program body starts executing. Recall the same from our previous module, i.e., Hello World Program in C.

The function is of two types in C Programming and that is:

Pre-defined functions

These functions are also called library functions, i.e., they are present in the c library, we can just import the necessary header file to use the same. These functions have particular characteristics and can use to perform useful tasks. Such functions are scanf( ), printf( ), pow( ), sqrt( ), etc.

User-defined functions

These are the functions that are created by the user for their own uses in a particular program. So that user can use that function multiple times as per the requirement. It helps in reducing the complexity of the code and also makes it more efficient.

What are the aspects of the functions?

There are three aspects of functions in C Programming.

  • Function Declaration: It is especially done to tell the compiler about the function name, its parameters, and its return type, so that the compiler gets awake, that this function has been used in the programming. It is done globally in the program.
  • Function Call: It is used to call a function wherever we want to use that function in our program.
  • Function Definitions: It is also known as function body, it consists of the actual statement that is to be executed from the function. It comes to use when the function is called in the program.

How to create a function in C?

Now for using a function in the program, it must be created first, the syntax for creating a function in C writes the return type and then specify the name of the function (that can be anything), followed by the parenthesis () like,

return_type function_name(parameters)

  • Return types tell that whether the function is returning some value or not, return type is simply the data type that tells which types of value the function is going to return, and in case there are some functions which do not return any value, we use return type as a void in that case.
  • The function name is the name of the function which you create, it can be anything, it has no constraints, it totally depends on you what name you want to give to the functions.
  • Parameters are like the placeholders, it is the value which the function uses in its definition for obtaining a particular result, it comes into use when the function is invoked. It is also called arguments. The function may contain parameters or not depending on the requirement.

Let’s see an example of creating a function in C.

void myfunction( )
{
  // code to be executed
}

Here in the above example, we have created a function named myfunction and the return type of this function is void as the function is not returning any value. This function contains no parameters.

How to call a function in C?

We create function because we have to use them in our program, and for that, we have to call that function wherever we have to use. When you create a function, they didn’t come into use immediately, they are saved for later, and when we called them, then the function body is executed, To call a function, write the function name, followed by parenthesis () then semicolon ;

Let’s see the example.

#include <stdio.h>
void myfunction( )  // create the function
{
   printf ("Hello world!");
}
int main( )
{
   myfunction ( );    //function called
   return 0;
}

The output of the above code is:

Hello world!

In the above program, we have created the function first-named myfunction which is simply printing Hello world! and then in the main function, we have called that function and got the desired output.

Some key points to be noted are:

  • The function can be called multiple times in the program as per requirements.
  • If a user-defined function, such as myfunction() in the above example, if is declared after the main() function, an error will occur. It is because C programming only supports top to bottom approach that means that if the function is not declared above main(), the program is unaware of it.

Let’s see one more sample example to get the concept clearer.

#include <stdio.h>
int add (int a, int b)     // function created
{
    int sum = 0;
    sum = a + b;
    printf("The result is: %d", sum);
}
int main( )
{
    printf("Enter two values: \n");
    int num1, num2;
    scanf("%d %d", &num1, &num2);
    add(num1,num2);       // function called
    return 0;
}

The output of the above program is:

Enter two values:
20 56
The result is: 76

In the above program, we have created the function named add, it has taken two parameters, to add two values and then printed the result and we have called that function inside the main function with the arguments, Hence, got the desired result.

What are the advantages of using functions?

We have talked a lot about functions, let’s see some of the advantages of it.

  • It is easier to find the errors, easy to understand, and maintain the code.
  • It creates a code reusable, i.e., can be used in another program also.
  • It divides a large program into fragments, so it helps the programmer to build a particular project in an efficient manner.

So, I hope you all have now become familiar with this particular topic, it’s the most important topic of this series, used for building large programs in an efficient manner. Hope you all must enjoy the module.

Stay Connected, keep coding, Happy Learning!