Functions in C Language – CsTutorialpoint

Hello friends, today we are going to talking about Functions In C Language.

In this article, we will learn in detail about what is function? And How many types of functions in C Language? 

So without wasting time let’s first understand what is Function in C Language?

Functions in C Language
Function in C Language

What is Function in C Language

When we create a program in C language, then we make that program by dividing it into many small blocks and this Block of Code is called Function.

There are many statements inside the function which perform some specific task. These set of statements which are closed with curly bracket { } are called blocks.

When we make a program by dividing it into blocks, then it becomes easy to read and easy to modify.

Creating a Function also makes it easy to manage the code because if there is an error in our program, then we can easily detect that error and fix it.

When you have ever created a program in C language before, then you must have used a function named main(). in our program, this main () function is only one and the execution of the program is also started by this main () function.

If we want, we can create many such functions in our program and we can use it anywhere in our program if needed. But do you know why exactly we create functions? Can we not make programs without functions?

So let’s know about all these

Why we need functions In C

When we make a program in the C language, then many such situations can arise in the program in which we need to use the same code again and again. This leads to unnecessary duplication of code and the possibility of bugs in the code, as well as the programmer can find it boring to write the same code over and over again.

In such a situation, C language provides us this facility in the form of Functions. We can declare and define at once by placing many statements in a group and if required, can use that function by calling it multiple times.

We also use Functions for the following reasons:

  • Functions are created to avoid writing the same code over and over again.
  • Functions Once created, we can call it by its name in our program as many times as needed and use it.
  • By creating a function, we divide the code into many blocks, which makes debugging easier.
  • Creating a function makes it easier to read and understand a very large piece of code.
  • Duplicate copy of the same code is not created by creating functions, which reduces the size of the code.

There are two types of functions in C language – one which is already defined in the library files and the other defined by the user while creating the program.

Let’s know about the types of functions in C

Types of Functions in C

There are two types of functions in C language:

  1. Predefined Standard Library Functions
  2. User Defined functions

1. Predefined Standard Library Functions

Such functions which are already defined in the library files are called Predefined Standard Library Functions, we also know these functions by the name of built-in functions whose declaration is in header files.

printf(), scanf(), strcat() etc. are examples of Predefined Standard Library Functions whose definition is in library files and declaration is in header files (eg studio.h, conio.h, string.h).

We declare these header files at the beginning of the program so that we can use the already defined functions in our program.

For Example -: The declaration of printf() function is in studio.h header file, so before using printf() function we have to include studio.h header file at the beginning of the program by writing #include<stdio.h> .

2. User Defined functions

Such functions which are not already defined and which are defined by the programmer or the user himself in the program, are called User Defined Functions.

The syntax of a user defined function is as follows:

Syntax of User-defined Function

return_type function_name( argument list )
{
   body of the function
}

In the syntax you see above, some words like return_type, function_name, argument list, and body of the function are used.

A function is made up of all these. Function has a return type, name and we can pass some arguments in its parenthesis () and we write the statement in the body of the function, which is closed with curly bracket { }.

Let’s understand these parts of the function.

Part of function in C

return type

if we want to use a function, then we need to call it by writing its name. When the function is executed and finally the data type whose value the function returns, we write that data type in this place.

If the function does not return any value then in this place we use void data type and if the function is returning some value then we use data like int, char, float, double etc.

Example without return value:

void hello() //Here void is a return type
{
printf("hello c");
}

In this example, the function does not return any type of value, so the void data type is used in the return type.

Example with return value:

int get() //Here “int” is a return type
{
return 10;
}

In this example, the function is returning a value of type integer, so the int data type is used in the return type.

Function name

Function name is an identifier which is a special name of that function. We cannot use the same name with two functions in the program, Doing so may cause an error in the program.

Function name should be meaningful so that the purpose of that function can be easily understood.

Example -:

addition( int var, int var) //Here "addition" is a Function name
{
  statements;
  statements;
   …
   ...
}

Arguments

In the parenthesis () of the function, we also write its data types along with the variable name. By the way, whether or not to write anything in parenthesis depends on the behavior of that function.

For example, if there is a function that needs some number to perform its work, then we declare that variable in its parenthesis and make it a variable so that the function can put that value in these variables.

Example -:

Sum( int var, int var) //Here “ int var, int var ” is a Function argument list
{
  statements;
  statements;
   …
   …
   Body of Function
}

Body of the function

All the statements inside the function are covered with curly bracket { }, which we call the block, this block is called the body of that function.

Let’s understand all these things better with an example.

Example

#include <stdio.h> //Header File
int addition(int num1, int num2) //Function declaration
/*
Here int is a return type, addition is a function name and int num1, int num2 is an argument list
*/
int main()
{
     int var1, var2,var3; //variable declaration
     printf("Enter number 1 : ");
     scanf("%d", &var1);
     printf("Enter number 2 : ");
     scanf("%d",&var2);

     var3= addition(var1, var2); // Function Call
     printf("Sum is : %d", var3);

     return 0;
}
int addition(int num1, int num2) // Function Definition
{
     int sum;
     sum = num1+num2; /* Arguments are used here*/
     return sum;
}

In this example, addition is the name of the function, and the int written before the addition is the return type of that function, here int is written in the return type because this function is returning integer type data.

The int num1 and int num2 which are written in the parenthesis ( ) of the function are the arguments list of the function.

By the way, if you look at this example a little carefully, then by commenting on it I have used some words like Function declaration, Function Call, and Function Definition, let’s understand about all these.

Function Aspects

A function has these three main aspects that you need to know -:

  1. Function declaration
  2. Function call
  3. Function definition

1. Function declaration

Whenever we use any function in our program, first of all, we have to tell the compiler through Function declaration that we are going to use this name and this type of function in our program.

If we go to use the function without function declaration, then the compiler will not know at compile time which function is being used in this program, so the compiler will give an error.

So whenever we are going to use Function in our program, then we have to declare it first.

Syntax of function declaration

return_type function_name (argument list);

Example -:

int multiply(int var, int var)

By the way, even if we do not write the name of the variable used in that function inside the function parenthesis () at the time of declaration, it will work, but the data type has to be written.

Example -:

int max(int, int);

Function declaration is required when we define that function under the main() function in the program and the declaration is necessary at the time of use of such function when that function is in some other files.

2. Function call

In C language we cannot use any function without calling it. If we want to use any function in our program, then we have to call it by writing the name of that function and it is called Function Calling.

When a function is called, the control of the program goes to that function and starts executing the code inside it and as soon as the execution of the function is over, the control of the program goes to that place again. is from where the function is called.

Syntax of function calling

function_name (argument_list)

Here we write any arguments in the parenthesis of the function when we have to pass some value to that function. If the function does not require any value, then we leave parenthesis empty.

Example -:

Result = multiply();

3. Function definition

In the function definition section, we write the actual statements to be executed in the program.

The control of the program comes in the part of the function definition when the function is called and the statement inside the function is executed, if the function returns a value then that value is passed back to the place in which that function is called.

Syntax of function definition

return_type function_name (argument list)
{
function body;
statements;
statements;
….
}

Example

return_type function_name(Function arguments)
{
//code to be executed
}

Friends, by now you understood very well that what is function in C? How many types of function in C Language, And how the function is declared, defined, and called.

Let’s discuss about some more aspects of the function which you need to know.

Different Aspect of function calling in C

A function either returns a value or does not and the parenthesis of the function remains empty or we write some arguments inside it. Looking at these aspects of the function, we can divide the function into four types.

  1. Functions with no arguments and no return value.
  2. Functions with no arguments and a return value.
  3. Functions with arguments and no return value.
  4. Functions with arguments and a return value

1. Functions with no arguments and no return value

In this type of function, we do not write any arguments inside parenthesis and their return type is void because they are not returning any type of value.

Syntax

void func (void);
main() .'
{
func ( ) ;
}
void func (void).
{
statement;
....
}

Example

#include<stdio.h>
void addition(void); //Function Declaration
void main()
{
    printf("\n Going to calculate the addition of two numbers : ");
    addition(); //Function Calling
}
void addition() // Function Definition
{
    int a,b;

    printf("\nEnter two numbers \n");
    scanf("%d %d",&a,&b);
   
    printf("The sum is %d",a+b);
 }

Output -:

Going to calculate the addition of two numbers :
Enter two numbers
10
20
sum is 30

In this program we did not write any type of arguments at the time of function calling and function declaration, so we have used void data type in parenthesis at the time of function declaration.

Since this function is not returning any value, we have written void data type in the return type of this function.

2. Functions with no arguments and a return value

In this type of function, we do not write any arguments inside the parenthesis, but the return type can be int, char, float, double, this return type depends on the type of data the function is returning.

Syntax

int func (void);
main() .'
{
  data_type var;
  …
  var = func();
  …
}
int func (void).
{
statement;
....
}

Example -:

#include<stdio.h>
int addition(void);
void main()
{
    int result;
    printf("\nGoing to calculate the sum of two numbers:");
    result = addition();
    printf("%d",result);
}
int addition()
{
    int a,b;
    printf("\nEnter two numbers");
    scanf("%d %d",&a,&b);
    return a+b;
}

Output -:

Going to calculate the addition of two numbers :
Enter two numbers
5
15
sum is 20

In this program we did not write any type of arguments at the time of function calling and function declaration, so we have used void data type in parenthesis at the time of function declaration.

Since this function is returning the Integer type value, so we have used int data type in the return type of this function.

3. Functions with arguments and no return value

In this type of function we write arguments inside parenthesis but void data type is used in return type because this function does not return any type of value.

Syntax

void func(int, int) ;
void main ( )
{
func(a,b) ;
....
}
void func( int c, int d)
{
statements;
....
....
}

Example

#include<stdio.h>
void addition(int, int);
void main()
{
    int a,b,result;
    printf("\nGoing to calculate the sum of two numbers:");
    
    printf("\nEnter two numbers:");
    scanf("%d %d",&a,&b);
    addition(a,b);
}
void addition(int a, int b)
{
    printf("\nThe sum is %d",a+b);
}

Output -:

Going to calculate the addition of two numbers :
Enter two numbers
15
25
sum is 40

In this program, we are passing two values ​​of integer type to the function at the time of function calling, and at the time of function declaration, we are creating two variables with int data type in the parenthesis of the function so that the value which is coming is stored in these variables.

Since in this function arguments of type integer is being written inside the parenthesis at the time of function calling and function declaration, so we have used two int data types at the time of function declaration also.

This function is not returning any value so we have used void data type in the return type of this function.

4. Functions with arguments and a return value

In this type of function, we write the argument inside the parenthesis and any one of the data type ( int, char, float, double), data type is used in the return type because this function returns some value.

But this return type depends on what type of data the function is returning.

Syntax

int func(int, int) ;
void main ( )
{
data_type var;
var = func(a,b) ;
....
}
int func( int c, int d)
{
statements;
....
....
}

Example

#include<stdio.h>
int addition(int, int);
void main()
{
    int a,b,result;
    printf("\nGoing to calculate the sum of two numbers:");
    printf("\nEnter two numbers:");
    scanf("%d %d",&a,&b);
    result = addition(a,b);
    printf("\nThe sum is : %d",result);
}
int addition(int a, int b)
{
    return a+b;
}

Output -:

Going to calculate the addition of two numbers :
Enter two numbers
25
35
sum is 60

In this program, we are passing two values ​​of integer type to the function at the time of function calling, and at the time of function declaration, we are creating two variables with int data type in the parenthesis of the function so that the value which is coming is stored in these variables.

Since in this function an argument of integer type is being written inside the parenthesis at the time of function calling and function declaration, so we have used two int data types at the time of function declaration also.

Since this function is returning the Integer type value, so we have used int data type in the return type of this function.

Advantages of function in C

There are many benefits of creating a function, some of them are as follows:

  • By creating a function, we can use the same code anywhere in our program without writing it again and again.
  • By creating a function, we can divide a very large program into blocks so that the program can be easily understood.
  • We can call the function as many times as we want in our program. There is no limit for calling a function.
  • Function provides us the facility to re-use the same code in the program.
  • Creating a Function makes debugging easier.
  • By dividing the program into Functions, it can be easily maintained.
  • If an error occurs in the program, it can be easily detected and rectified.
  • The size of the program is useful because the same code does not have to be written again and again.

Important Point about function

  • main() is also a function.
  • There can be many functions in our program.
  • If there is only one function in our program then it will be main() function because C language program starts from main().
  • After defining a function, we can call our program as many times as needed. There is no limit for calling a function.
  • Any function can call any function.
  • When someone calls himself, it is called Recursion, about which we will know in detail in our next article.
  • Each function will have its own name and its return type will either have a void data type or some other data type.
  • Before using a function in a program, it is necessary to define and declare it.
  • No other function can be defined inside one function.
  • The name of each function in the program should be different.
  • Function gets memory only when it is called, as soon as the function of the function is finished, the function releases its memory.
  • When a function is declared outside the main() function, then that function is called a global function and if it is declared inside the main() function, it is called a local function. If you want to know about it in detail, then tell me in the comment below, I will write an article in detail about it.
  • One function cannot access variables of another function.
  • The return keyword can return only one value.
  • It is necessary to put () after the return keyword when we have to write an expression (like a+b) inside it.
  • If we write a statement after the return keyword, then that statement will not run.

Conclusion

Friends, I hope that after reading this article you know very well, what is function in C? And how many types of functions are in C language?

If you want a complete tutorial of C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.

If you liked this post, then do not forget to share this post with your friends so that they can also get information about Function in C Language

If you still have any questions or doubts related to Function in C Language, then you must tell me, I will answer all your questions and you can contact us for more information.

Subscribe to this website newsletter to get information related to this new technology, Programming Language, Coding, C Language, C++, Python Course, Java Tutorial.

Jeetu Sahu is A Web Developer | Computer Engineer | Passionate about Coding, Competitive Programming, and Blogging

Leave a Comment