Functions in C Programming

Functions in C Programming

In this tutorial, you are going to learn about Functions in C Programming.

Functions

A function is a block of statements, which is used to perform a specific task. A C program has at least one function named main().

Function Syntax

Now, let's see how a function is written in C Programming.

returnType funtionName(parameters)
{
Function body
}

returnType: If you want your function to return some values, the datatype of this return value will come here. 'void' is written if your function doesn't return any value.

functionName:Just like giving a name to variable, here you give your function a name.

Parameters: You can pass some values to your function like passing two numbers for multiplication. They are separated by comma. Also they are optional. You can leave them blank if your function doesn't contain any parameters.

Function body: To perform certain tasks, you can write commands over here which will be enclosed within curlybraces.

If your function return any value you will have to write return value; command at the end of function body. The datatype of the return value should match the return type of the function.

Types of functions

There are two types of function in C which includes Library function and User defined function.

Library Function : Library functions are the built-in function in C such as puts(), gets(), printf(), scanf() etc. These are the functions which already have a definition in header files.

User defined function : C allows users to define their own function according to their requirement in the program which are known as User defined function.

Function Example

/* Function returning sum of two numbers */
int add(int num1, int num2)
{
/* local variable declaration */
int sum;
sum= num1+num2;
return sum;
}

In the above program, int is the return type of the function named add. Two variables are passed as parameters. Inside function body addition task is written and sum is returned.


This post on Functions in C Programming is written by Rajnish Kumar. If you like TheCode11, then do follow us on Facebook, Twitter and Instagram.

Previous Post Next Post

Contact Form