Function in C

 Function  in c

A function is a self contained code or group of statements or block of statement that perform specific well defined task in program or coherent task when it is called.

User can declarer function as per requirement, it’s totally depending on user how to define function. Multiple functions also defined to perform specific task in a program.

By using function a large program can be divided into sub programs.

Generally every C program contain at least one function which is main().

There two types of function :

1. Library function

2. User defined function

1. Library function

 Library functions are the system defined function it can’t be modified, it can only read and can be used in program.

These function are always provided with every C compiler

Source code of library function are pre complied and only object code used by the programmer by linking to the code using linker

e.g printf(),scanf(),getch(),clrscr().

2. User defined function

  When the user defines function according requirement is called user defined function.

 Function definition

Function definition consists of complete description and code of the function for specific well defined task.

It tells about task of function , what are its inputs  required and what are its expected out put .

Function definition consist two parts function header and function body

Syntax:-

return type function name(type1 arg1,type2 arg2,...)

{

   Local variable declaration;

   Statement 1;

   Statement 2;

   Return value

}

If the function does not going return any value to the calling point then the syntax looks like

 

function name(type 1 arg1, type2 arg2, ...)

{

Local variable declaration;

Statement 1;

Statement 2;

Return value

}

 The return type declare the type of the value that function will return if not declared it is assumed to be int by default.

if a function returns no value  that time  keyword void can be used before the  fun ction name.

The body of the function consist the group of  statements or block which consists of local variable declaration statement and optional return statement.

If the  variables  are declared inside a function is only local  variables to that function only. You  can’t be used anywhere in the program .

 

Example of Function Creating a user defined function

Creating a user defined function multiplication()

#include<stdio.h>

int multiplication(int num1, int num2)   //function prototype

int main()

{

    int var1, var2, res;

   printf("Enter number 1: ");

   scanf("%d",&var1);

   printf("Enter number 2: ");

   scanf("%d",&var2);

   res = multiplication(var1, var2);   //function call

   printf ("Output: %d", res);

return 0;

}

int multiplication(int num1, int num2)//function definition

{

  int mul;

  mul = num1*num2;

  return mul;                   //return statement

}

 Function Call

When the function is called the control of the program is transferred to function definition by calling it and once function executed with return value, the control again transfer to the main function of a program.   

Syntax of function call:

 Function Name (arg1,arg2,arg3,…);

In above example  multiplication(var1, var2); statement inside the main() function.

 Function Prototype

It is the declaration of a function which specifies name of function, parameters and return type. it doesn’t contain function body.

It tells to compiler that the functions will have number of arguments and return  type of a function required , it also tell about data type of a function parameter.

It also gives the information of function may be later used in program and If function not defined before it used, it must be declared by specifying the return type and the types of the parameters.

Syntax of Function Prototype

 return type function name(type 1 argument1, type2 argument2, ….)

 in above example multiplication(int num1, int num2) is the function prototype.


Passing argument to the function

When we pass variables to the functions we refer as argument and  if we used argument   inside the function call are called actual argument,

If we use  argument in the function definition are known as formal arguments.

So when we pass to the function and formal parameter need to match otherwise compiler will give error.

In above example var1  and var2 are passed during the function call and parameter num1 and num2  accept the passed argument in the function definition


Previous
Next Post »