TechGeeks

Built-in Functions and User-Defined Functions

In a programming language, a function is referred to as a segment that groups code to perform a specific task.A C program will at least have one function called main( ).Without main() function, there is subsequently no C program.

Types of C functions

There are basically two types of functions in C programming:

Built-in Functions

The library functions are the in-built function in C programming system.
For example:

User defined function

The C programming language allows programmer to define their own function according to their requirement. These types of functions are referred to as user-defined functions. Consider that a programmer wants to find factorial of a number and check whether it is prime or not in the same program. Then, the programmer can just create two separate user-defined functions in that program: one for finding factorial and other for checking whether it is prime or not.

How user-defined function works in C Programming ?

#include <stdio.h>

void function_name()

 {

      statements....;

  }

 int main()

{

  statements...,


 function_name();

  statements...;

 }

NOTE: The function name is an identifier and should be unique.

Advantages of user defined functions :

Example of user-defined function

C Program to add two integers.Create a function called add to add integers and display sum in main() function.

 #include <stdio.h>

int addition(int a, int b);

void main()

  {

        int n1,n2,sum;

        printf("Enter number1: \n");

        scanf("%d",&n1);

        printf("Enter number2: \n");

        scanf("%d",&n2);

        sum=addition(n1,n2);

        printf("The sum of two numbers is %d",sum);



  }

int addition(int a,int b)

 {

       int add;

       add=a+b;

       return add;
 }

OUTPUT:Enter two numbers to add
2
4
sum=6

Syntax of function definition

  return_type function_name(type(1) argument(1),..,type(n) argument(n))

   {

        body of function

   }

Function definition has two major components:

Function declarator is the first line of function definition. When a function is called in the program, control of the program is transferred to function declarator.

Syntax of function declarator


return_type function_name(type(1) argument(1),....,type(n),argument(n)) 

In the above example, int add(int a,int b) is a function declarator.

Function declarator is followed by the body of the function inside the braces.
In programming, argument(parameter) refers to the data, this is passed to the function(function definition) while calling function.

Arguments that are passed in the function call and the arguments that are accepted in function definition should have same data type.

Consider if argument num1 was of int type and num2 was of float type then, argument variable a should be of type int and b should be of type float since type of argument during function call and function definition should be same.
A function in a program can be called with or without an argument.

Return Statement

Return statement is used for returning a value from the function definition to calling function.

Syntax of return statement :


return (expression);

For example:

  return a;

  return (a+b);

In above mentioned example, value of variable add in add() function is returned and that value is stored in variable sum in main() function. The data type of expression in return statement should also match the return type of the function in the program.

<< Back Next >>