Functions in C

The function is a set of programming statements written between blocks. There is always a function or more than one function is present in a program for instance the main function. Steps to construct a function:

Declaring a function in C:

To declare a function in C, it should be declared or should be initialized globally.

Syntax:

return type function name (parameter list);

Defining a function:

A function should be defined through which the actual statements are declared and the main purpose of the function is fulfilled.

Syntax:

return type function name (parameter list)
{
//body;
}

Calling a function in C:

A function after it’s defining and its declaration it should be called. After the call, the control comes to the function and the commands and statements are executed.

Syntax:

function name (argument list);

Example Of Function:

#include<stdio.h>
int add(int a,int b);
int main()
{
int n1,n2;
printf("\nEnter the values of the variables");
scanf("%d%d",&n1,&n2);
int a=add(n1,n2);
printf("\nThe result after function call %d",a);
return 0;
}
int add(int a,int b)
{
int s=a+b;
return s;
}

Output:

PSGuDE1LXJXUiwWNB3PcNua7uYnh6fyUtq

Types of function C

There are two types of functions in C, they are:

  • Library functions
  • User-Defined functions

Let us discuss them briefly.

Library functions:

The library functions are the standard functions that are by default found in the C language. These functions are defined in the header files. Let us see some examples:

printf() is a library function that is defined in the stdio.h header file used to print messages.

main() where we define the statements of the code which are to be executed.

There are several other functions such as the sqrt(), scanf() etc.

User defined functions in C:

Apart from library functions, we can also define user defined functions, it can be constructed by following the steps to form a function i.e., declaring a function, defining a function and then calling a function. 

Let us see another example:

#include<stdio.h>
int mux(int a,int b);
int main()
{
int n1,n2;
printf("\nEnter the values of the variables");
scanf("%d%d",&n1,&n2);
int a=mux(n1,n2);
printf("\nThe result after function call %d",a);
return 0;
}
int mux(int a,int b)
{
int m=a*b;
return m;
}

Output:

Here we come to the end of the topic on functions in C, hope we have cleared all your doubts.

If you want to improve this article or contribute to lingarajtechhub then WhatsApp Us.

Default image
Soham Malakar
Hello my name is Soham Malakar am currently pursuing BCA Coding is one of my favorite hobbies. I love to code and an enthusiast to learn different types of programming languages. I love to get intoxicated in the world of technology and video games. A quick learner possessing better results.
Articles: 15

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply