Function in C Programming [Complete Guide]

Function in C Programming

Function:

A function is a block of code that performs a specific task.
Every C program has at least one function, which is main().
A function executes its body (set of instructions) when it is called.

Parts of a function:

There are four parts of a function
  • Return Type
  • Name of the function
  • Parameters
  • Function Body/Definition

Return Type:

The type of data function returns after performing its functionality.
Any primitive data type can be used as return type. So return type may be int, float, double, char, long etc.

Name of the Function:

Name of the function must show what the function is about. For example, if we want to make a function which simply adds some numbers, then the function name will be sum or summation or add just to show that the function simply adds numbers.

Parameters:

Data to receive whenever the function called is known as parameters of the function. Parameters are enclosed in the round brackets with there primitive data types.

Function Body:

Function body contains all the code to executes when the function called.

How to define a function?

Syntax:

Return_type    Function_Name(Parameters)
{
   Function Body
}

Real Function Example:

int sum(int a,int b){
return a+b;
}

Firstly, I write int which means it returns an integer after performing its functionality, then I write sum (the name of the function) and then in round brackets int a, int b which means whenever we have to call this function we have to pass two integers as arguments. And after all the function body is in curly brackets. In the function body, integer a added into integer b and returned to the calling point.
In Short:
This function (sum) receives two integers and return there sum.

How to call a function?

Only write function name and the arguments to pass in round brackets if any.
If we want to call upper function sum then we have to write function name sum and two integer values to pass as shown in the code below.
main(){
int x = sum(10,5);
printf("Sum is %d",x);
}

The function returns the sum of passed integers and we store the returned value in new integer x and then display. You can perform this in one line also.

Types of function :

  • Predefined functions
  • User-defined functions
types of functions

Predefined functions:

Functions described by the creators of c programming and are in standard libraries. To use these function we need to add the standard library in our program and call the function.
For example, rand() is a predefined function to generate random numbers in c.

User-defined functions:

The functions which we describe in our programs are known as user-defined functions.

Types of user-defined functions:

There are four types of functions we can define in our program. These are given below
  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:

Functions with no arguments and no return value

These type of functions didn't have any return type and also not receive any arguments.
For example, a function to display an error message.
void displayError(){
printf("This is an Error Message");
}

Explanation:
  • The function has return type void which means the function will not return anything after performing its functionality.
  • The name of the function is displayError.
  • You can see there is nothing written in round brackets which means this function will not receive any argument when called.
  • A printf function is called to display an error message in the body of the function.
Whenever we have to print the error message we call this function by simply writing displayError() instead of writing printf statement again and again.

2. Functions with no arguments and a return value:

Functions with no arguments and a return value

These type of functions receives nothing but return something.
For example, a function of getting a number from the user.
int getNumber(){
int x;
printf("Enter a number : ");
scanf("%d",&x);
return x;
}

Explanation:
  • The function has return type int which means its return an integer after performing its functionality.
  • The name of the function is getNumber.
  • The function didn't receive anything. 
  • In the body of the function, a variable x is declared, a message is printed and scanf to receive an integer in the variable x is called and after all, x is returned.
All the times when we need to get a number from the user we just call this function and save the returned value instead of writing all the code again and again.

3. Functions with arguments and no return value:

Functions with arguments and no return value

These type of functions receives some parameters but return nothing.
For example, a function to check a number is even or not.
void checkEven(int number){
if(number%2==0){
printf("Number is Even");
}else{
printf("Number is Odd");
}
}

Explanation:
  • The function has return type void which means nothing to return.
  • The name of the function is checkEven.
  • It receives an integer and saves it in the variable number
  • In the body of the function, there is an if-else statement to check the number is even or not.

4. Functions with arguments and a return value:

Functions with arguments and a return value

These type of function receives arguments and also return something.
For example, a function to add two numbers.
int sum(int a,int b){
return a+b;
}

Explanation:
  • The function has return type int that means the function returns an integer after performing its functionality.
  • The function name is sum.
  • The function receives two integers and stores the first integer in variable a and second in variable b.
  • In the function body, variable a and b added and returned.

Why functions?

Sometimes, we need to perform a task again and again in a program and if we write the same code again and again in the program to perform that task, our program become very lengthy and we will unable to manage it easily. For example, if we need to check some conditions for 100 students like if student grade is greater than 50% it is passed otherwise it fails, then we are unable to write these conditions again and again and this is also unprofessional practice. To solve this problem we write a function with given conditions and call it 100 times using a loop. This practice lessens our hundreds of lines of code and our code also looks pretty well.

So, we can summarize the advantages of using functions in our program.

Advantages of functions:

  • We can divide our program into smaller modules.
  • Program readability increase.
  • Functions once created can be reused in other programs.
  • Our main function becomes smaller.

Function Specificity:

A function must be specific to perform one task. If a function performs two task then divide it. 
You can understand the concept of a function specificity by understanding this concept that when we call a car mechanic it perform just only its duty not others similarly function must be specific to perform one task only, just like for summation we call a function and for multiplication, we call another. 


Comments

Popular posts from this blog

c/c++ Programming questions || Practice Problems for Beginners || C Programs List

Change your Application background in Scene Builder || JavaFX FXML Tutorial

Implementation of Graph in C || Directed and Undirected Graph