Categories
Technology & Troubleshooting Uncategorized

Types of user-defined functions in C language with examples

The article explores function and reusability of code in C programming language.In this blog you will be getting knowledge about types of user-defined functions in c with examples

What is a function ?

“A function is a small program or a part of the main program which can able to perform some specific tasks.”

Every programming language allows you to create your functions for accomplishing your desire tasks wit h reusability of code

How to create different types of user-defined function in C ?

Here are some steps that will helps you out to create user-defined function using C language

[1] Motivation : To create a function that does the summation of two integer numbers.

[2] Steps for Creation of a function:

Any function can be created through three below steps :

i) Declare: First we have to declare a function above the main function with it’ name
ii) Define: In the second step we have to put definitions/logic of the function
iii) Call: In the last step we have to call the function when we need it.

[3] Syntax of function prototype:

Declaration syntax :
Return_Type Function_Name (type1 Argument1, type2 Argument2, …..);

Declaration of a function Example :

Definition of function Example :

Call of function Example :

Why we need the function?

The function simply enables code re-usability, therefore with the help of functions, we can eliminate code duplication in our programming structure. While executing the program which has user-defined function prototype, The program bounce back each time to the function prototype or in other words we say definition when function getting called.

Now lets start, types of user-defined functions in c with examples

Different Types of user-defined functions in C with proper examples

Write a program to sum two integer numbers using a user-defined function (without Arguments)


#include <stdio.h>

void sum() //declaration of a function

int main()    //main function
{
	sum(); //calling a function
	retun 0;
}
void sum()
{
	int a, b,sum;
	
	printf("enter the value of a\n:");
	scanf("%d",&a);
	printf("enter the value of b\n:");
	scanf("%d",&b);
	
	sum =a+b;

	printf ("Sum of %d and %d is : %d",a,b,sum);
}

Write a program to sum two integer numbers using a user-defined function (with Arguments)


#include <stdio.h>

void sum(int a, int b) //declaretion of a function

int main()    //main function
{
	int num1, num2,sum;
	
	printf("enter the value of num1\n:");
	scanf("%d",&num1);
	printf("enter the value of num2\n:");
	scanf("%d",&num2);
	

	sum = sum(num1,num2); //calling function
	printf("The addition of %d and %d is : %d",num1,num2,sum);

	return 0;
}

void sum()  //defination of the function
{
	int result;
	result =a+b;
	return result;
}

Know more about : JavaScript, OpenStack, Python

Credit: Featured Image

Leave a Reply

Your email address will not be published. Required fields are marked *