Functions in C

Nov 22 • Notes • 1654 Views • No Comments on Functions in C

In C, there are situations when we may need to execute a particular piece of code more than one time and writing the same code again and again is a time consuming process and also it becomes complex. So in order to resolve this issue C provides a special facility which is known as function.

c_programming_language

So function is a code module or a code snippet under a name that we can use any time and as many times as we need in our program just by calling the name. Though there are some predefined functions in C, we can also define some by ourselves. Those functions which would be defined by us are usually known as user – defined functions.

There is a particular syntax to write any function. So we need to follow that particular syntax.

SYNTAX:

data_type name_of_the_function( arguments)

{

BODY OF THE FUNCTION

}

Here the “data_type” is actually the type of value the function is going to return. The “name_of_the_function” is any name which you want to give that can identify it a particular set of code. And the “arguments” is any set of values which may be used in the function.

 To call any function we just have to write “name_of_function(arguments);”

for example:

void show()            // the show() is defined

{

printf(“this is a good opportunity to me.”);

}

int add(int x,int y)         // the add() is defined

{

int z= x+y;

return (z);

}

main()          //the main() function

{

show();         // the show() function is called

add(4,6);     // the add() function is called

}

We can call the functions by two methods:-

1.     Call by Value.

2.     Call by Reference.

Call by Value means that we deals with the direct values. And Call by Reference means that we deal with addresses where the values are stored.

For dealing with addresses we must have the clear concept of pointers. Pointers are variables that can hold the address of any variable.

So functions are very important part of C. Even in the practical field we can’t make any project without using function as it would become more complex and difficult without them.

But the most important thing which is also an issue if common sense that the name of the function must be relevant to the working of the function so that just by seeing the name only we can understand what implementation that function is doing.

Tell us Your Queries, Suggestions and Feedback

Your email address will not be published.

« »