Home » C programming language

C Library and User Define Functions (UDF)

C - Functions

Functions play a very useful role in modular programming, functions are also known as modules. A function is a set of statements which performs specific task.

In Modular Programming, a program can be divided into modules, these modules are known as functions.

There are two types of functions:

  • Library Functions
  • User Define Functions

Library Functions

Library Functions are those functions which are defined in the C Library, you do not need to declare and define them. Just include their header file in which functions are declared and you can use those functions. Library functions are printf(), scanf(), getch(), pow() etc

User Define Functions

User Define Functions are those functions which are defined and declared by the programmer to do some specific task. These are designed to re-use the code.

Benefits of designing a User Define Functions:

  • Using functions, you can divide your large program into small pieces according to different task which will provide you easy access to read and debug.
  • Using functions, you can re-use your code. You do not need to rewrite the code, just call the function and use it.
  • Using functions, you can modularize your code.

Declaration of an User Define Function

return-type  functionName(type-of-local-argument-list);

Defining an User Define Function

    return-type  functionName(type-of-local-argument-list);
    {
        Function-body/ set-of-statements;
    }

Calling an User Define Functions

functionName(actual-argument-list);

return-type

It is the data type of function’s return value. If function does not return value, function’s return type must be void. void is a data type which represent nothing i.e. function will not return any value.

functionName

It is the name of those set of statements which are written in function’s body. functionName can be any valid identifier’s name, please do not use any reserve word as a function name.

type-of-local-argument-list

List of the data type of those arguments (parameters) which are using in the function definition.

actual-argument-list

List of those parameters/ arguments, which are being passed in calling function.

Consider the example

/* program to print some text using User Define Function*/
#include <stdio.h>

/*	function declaration
	function return type : void - it will not return any value
	argumets : void  also will not take any parameter*/
void message(void);

int main()
{
	message();		/*function calling*/
	message();		/*function calling*/
	return 0;
}

/*function defintion*/
void message(void)
{
	printf("\nHello Guys!!!");
}

Output

Hello Guys!!!
Hello Guys!!!

Consider another example with argument list and return type

/* program to find sum, sub and multiplication of two numbers using UDFs*/
#include <stdio.h>

/*functions prototypes*/
int		add(int,int);
int		sub(int,int);
int		mul(int,int);

int main()
{
	int val1,val2,result;

	printf("Enter first  number : ");
	scanf("%d",&val1);

	printf("Enter second number : ");
	scanf("%d",&val2);

	result=add(val1,val2);
	printf("\nAdd = %d",result);

	result=sub(val1,val2);
	printf("\nSub = %d",result);

	result=mul(val1,val2);
	printf("\nMul = %d",result);

	return 0;
}

/*functions defintions*/
int add(int a,int b)
{
	int result;
	result=a+b;
	return result;
}

int sub(int a,int b)
{
	int result;
	result=a-b;
	return result;
}

int mul(int a,int b)
{
	int result;
	result=a*b;
	return result;
}

Output


Enter first number : 10
Enter second number : 20

Add = 30
Sub = -10
Mul = 200

Consider the example "How to define function with declaration?"

/* program to find sum, sub and multiplication of two numbers using UDFs*/

#include <stdio.h>

/*functions defintions with their declarations*/
int add(int a,int b)
{
	int result;
	result=a+b;
	return result;
}

int sub(int a,int b)
{
	int result;
	result=a-b;
	return result;
}

int mul(int a,int b)
{
	int result;
	result=a*b;
	return result;
}

int main()
{
	int val1,val2,result;

	printf("Enter first  number : ");
	scanf("%d",&val1);

	printf("Enter second number : ");
	scanf("%d",&val2);

	result=add(val1,val2);
	printf("\nAdd = %d",result);

	result=sub(val1,val2);
	printf("\nSub = %d",result);

	result=mul(val1,val2);
	printf("\nMul = %d",result);
	return 0;
}

Output

Enter first number : 10
Enter second number : 20

Add = 30
Sub = -10
Mul = 200


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.