Introduction to functions in C.
Hello coders! Welcome back!
Today will look into functions in C.
Today will look into functions in C.
What is a function?
The function is basically a set of instructions, that take input, perform some computation and produces the output.
Syntax of function-
<Return_type> <name_of_function> (set_of_inputs);
- Return type indicates the type of output the function is going to return. It can be integer, float, or character.
- Name of function is the name given to the function.
- Set of inputs are the values that are passed to the function.
Here, the function 'area_of_tri' is returning integer type data. Hence, 'int' is mentioned.
Name of function is your choice, you can give any name. But bear in mind that it should be understandable by other programmers too. Here we are trying to find the area of the triangle.
In the main function, we are passing the values 3,4 (observe line number 12).
Why we need to use the function?
Reusability- means we can use the same set of codes again and again without defining it once again. Let's take the above example, and try to print the value of the triangle bypassing other values -
Here, we are passing 5 and 2 values to the same function. We are using the same set of code once again without defining it one more time.
Here, we are passing 5 and 2 values to the same function. We are using the same set of code once again without defining it one more time.
Abstraction- In simple words, we use scanf function to read the values, we don't know how it works inside! That is an abstraction
This is for this blog. Will look into more details in a further blog. If any doubts up till here drop a comment always ready to clear them.
Happy learning!
Comments
Post a Comment