Functions in C

FUNCTIONS IN C

Function 

A function in C is a block of code which is used to perform specific tasks as many times as it is called, and is used to make coding simpler and convenient using a group of statement and operations.

C function, this is a set of statements. there is a function in every single program. where function is required, function is called.

Note: int main() is also a function, every code has at least one function.

Advantages of function:-

  • The code written in the function does not have to be written repeatedly.
  • function protects programmer’s time and program space.
  • Large program can be divided into small functions.
  • The function can be called repeatedly where needed.
  • The readability of the program increases.
types of functions

Predefined function

  • In-built functions are also called predefined or Library functions.
  •  In-built functions, different header files or processors have been created for each functions.
  • There are lots of header files in C and they are grouped by different functions.
  • If programmer wants to create your own header files too.
  • Function declaration and definition is in header files.

     Example:

  • printf()
  • scanf()
  • strcpy()

User-defined function

User defined functions are those functions which programmer (you) create for yourself. Programmer can create as many functions as per your requirement.

Declaration of a function

When writing a function we need to tell the computer the name, the return type and the parameter(if any) of that function.

return-type – What kind of value will return when your function execution is complete, you define it by return type. If you are creating an addition program which adds 2 whole numbers then your return type is int.

function-name – this is the name of your function. These should be unique in the whole program. When you call the function, you write this name only.

list-of-parameters – These are the lists of variables that you will pass when calling the function. For example, if you are creating addition of function then you can pass 2 numbers or 2 variables as parameters, and then add them inside the function and show results. It is not necessary that you define parameters in all functions.

One thing you should always remember is that the function declaration statement is terminated from semicolon. But this does not happen with the function definition.

Function Definition

Function definition includes return-type, function-name, and list-of-parameters as in the function declaration. After that, those statements are written in the block of curly brackets that you want to execute.

Let’s see how

#include<stdio.h>
int add(int a,int b)  //a,b are the parameters which can be provided while calling the function
{
  int c;
  c=a+b;
  return c;//returns the integer value of the sum here
}
function in c

8 comments on “Functions in C”