Function in C program

Function in C:

Function in C program  are the building blocks of any program.It  performs specific task in program. There could be number of functions in any program. Every program contains at least one function which is main.

Why to use function in C program?

  • Large programs can be  divided into small sub programs.
  • It becomes easy to find error.
  • It becomes easy to understand the code because each function perform specific task.so code become easily readable.
  • It reduces the length of a program. Meanwhile it reduces the memory consumption.
  • If some specific functions have to use more then one times in particular program you can define a function and call  number of times you want.

Function in C is of  two types:

  • User defined functions
  • Library or system defined functions.

Library functions :

These are the programs which are predefined.these functions are already saved under some specific header files to perform  specific function or task. By calling these functions we can get desire output for which we didn’t require to write to whole code.

For example  sqrt() defined under the header file math.h used to find the square root of a number. strlen() function defined under the header file string.h  use d to find length of the string entered by the user.There are many more library functions which perform specific task in programs. Few of them are:strcpy(), pow(), strcat(), tolower(),toupper(),abs().

User defined functions:

As the name suggest these  are defined by the user to perform specific task.To define these type of functions you will  have to know the following three things :

  • Function declaration
  • Function definition
  • Function call

Before going through these points have a look to the program given below:

You have seen how all the three requirements to define a user defined functions are used  in the above program.

Function declaration:

Syntax:

As you know that compilation of a program start from main() function. We can define our function before or after the main. If you will define the function after the main function in such cases function declaration is necessary. If you will define the function before main in such cases function declaration is optional. function declaration simply tells the compiler that there is a function defined in the program anywhere.

As shown in the syntax function  declaration only consists return type of function, function name and arguments.

Return type signifies the type of value will return from that function. it may any of data type such as int, float ,double, bool etc. If you don’t want to return any value from function mention the return type as void.If you will not provide return type in function declaration, compiler will include int return type by default.

Function name could be any name but it must follow the rules of variables. Function name should be given as per the task of function so that it will be easy to understand the purpose of defining particular function.

Arguments of function must inside the parenthesis i.e. small brackets. In function declaration you can only mention the type of arguments. There is no need to write the variable name also in function declaration.

You can mention number of arguments in declaration as reuired for your program.the arguments present in function declaration are known as formal parameter/signature/formal argument. These parameters can be of different data type.

Note :

Function declaration ends with semicolon, missing it will lead to error.

Function definition:

Syntax: 

Function definition contains the body parts of the function. It can be done before the main function and it can also be done after the main. Its better to write the function definition before the main function. In function definition the arguments should be present along with the data type and variable. After the function name semicolon should not be there in function definition otherwise it will lead to error.

Example

Return statement inside the function returns the value to function call. Return statement should return the value of its return type.if the return type of function is void then return statement  will not return any value. Only one value or expression can be return from one function but there could be multiple return statements followed by different conditions. It will return the value of true condition.

Example of function definition

Function call:

Synatax:

Function call present inside the main function. Function calling contins the function name and arguments. The argument(s) type should be exactly match with the function definition. Any mismatching will lead to error.The arguments present in function call are known as actual arguments.

Example;

There are many site which provides information related to the Programming please click here for more details.

 

 

           

 

Leave a Reply