Function is C Programming 


Today we will learn what is the function?, What are the use of function in C programming?, Why the C programming provides the concept of function? and how the functions makes programming easier. 

Actually in the programming language we have one rule that is know as the Don't repeat yourself (DRY) for that purpose the concept of the function in the C programming language has been introduced .

A function is a "BLOCK OF CODE" or "SET OF STATEMENTS" for do a specific task. That block of code we can use any where in our whole program by just call it. We don't need to write these line of code again and again in our program it makes the program small or easy to read or more understandable.


Why we use function in c programming /Advantage of function in C Programming :

we have many advantages of using function in C programming languages -

  1. We can avoid rewriting of code.
  2. Function makes program simpler and smaller.
  3. Functions can make debugging easy.
  4. Functions are also improve the readability of program.   

FUNCTION deceleration in C programming :

If you wrote any C program before then then you guys already know the how to make a user defined function and whats the syntax use for defining a function.

When we write any C program we must write "main()" . Its a function . Actually it's a very useful function for any C program because the execution of any C program start from the main function.


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

There are for thing to discuss in the syntax of function in C programming-

  1. Return_type
  2. Function_name
  3. parameters
  4. function body   

1.Return_type:

 A function may or may not be return a value.If function returns the value then the return type is the "data type" of the returned value. Else if function doesn't return the value then the return type is the keyword "VOID". 

2. Function_name: 

Function name is the name of function. It's use for the calling the function and its also the identity of the function. Function name can be any thing but for batter understanding we relate it from the aim of function and one more thing we can't use predefined keywords as the function name. You can check these keywords on google easily.  

3. parameters

parameters are the data that we provide to the function and the code present inside the function block may or may not be used data. In common language we can say these are the inputs given by the user for complete the specific task.

4. Function body: 

we can easily understand by the name. It contains block of the code or set of statement for perform a specific task.


Types of function in C programming language:

There are mainly two type of function in C programming language-


  1. Predefined  functions
  2. user defined functions 

1.Predefined functions:

Predefined functions are the built in function in standard library(like #include is standard library). we don't need to declaring these functions. We can directly call  these functions in our program. 

 For Example : Printf() ,scanf(), get(), put() etc.

2.User defined functions:

User defined functions are the defined by the user at the time of making the C program. we already know the syntax of defining the functions.


lets take few example for better understanding of function-

problem statement:

1.Write a function for finding the factorial of a number in C programming language. you can take any number for finding factorial.



2.Write a function for find the given number is a odd or a even number. You can take any odd or even number as a user input for check.

#include <stdio.h>

char* odd_even(int i);

int main()
{
   int number;

   printf("Enter an integer : ");
   scanf("%d", &number);

   printf("Result : %s",odd_even(number));
   return 0;
}

char* odd_even(int number)
{
  if (number%2 == 0){
      return "EVEN";
   }else{
      return "ODD";
   }
}