User define function in C programming

What is a function in C programming?

function in C programming
User-defined function in C programming

The function is like some tasks defined in a group of instructions. If we call the function, it's do something for us and sometimes return something.
For example, let's think about a factory that inputs some Raw material and processes it, then gives us some output. Like a blending machine. We input water, ice, sugar, lemon, and the output is lemon juice.
In a function, there is some instruction and when we call it, it gives us some results according to the instruction.

Every c program must have at least one function called the main function looks like main(). There are many library functions in C, like as printf ( ); scanf( ); etc. Again we can write functions our-self.

Basic structure of a function:


* Return type: Return type is the kind of output of the function. For the blending machine, the output is "lemon juice". So here return type is liquid.
* Function name: It's just the name of the function. Like "blending machine".
* Parameter list: The parameter list is the input list. Here water, ice, sugar, lemon.
* Body of the function: The instruction of processing our inputs. 

Example: 



Output:

Explanation:

In basic instruction:


is define as:


Here returns type is an integer. The parameter type is important. Here are two parameters a and b which are integer types. So, we have to pass the integer type argument as input.


Benefits of function:

If we need to do the same processing in many places of our code, we have to write the same code again and again. Like as if we need to add two numbers several times, we can write a function and call it again and again.

Like as:



Output:

Another Example:



Output:


I hope it helps to understand function declaration and how to use the function, make function.
Happy coding!!!

 Related Post:

Let's play with stars using C programming  

0/Post a Comment/Comments