C – Function Definition and Function Types

Function Definition and Function Types

C program consists of one or more functions along with function main(). Functions are known as modules this will make a program to easy.

In Every program the main() function should be placed, in every program library function like printf(). sqrt(). pow() etc.

C Function easy to define and use. The function is one of the most powerful features of the c program.

Actually, the function is known as the group of the statement in a single logical unit to perform some specific task.  

The function is made up of many small functions and each and every small function have their own particular task rather than the single main() function.

C Code, 

int sum(int x, int y){
     int result;
         if(x>= && y>0)
                  result = x+y;
        else
                  result = 0;
        return result;
}

In above code the name of the function is the sum and its return type is an integer which is declared before the function i.e int sum The function always enclosed with the parentheses and which can have the parameter declarations i.e int sum(int x, int y).

The function body start with the opening braces and closed with the closing braces. Inside the function body, it has a local variable called result which has the return type integer, it will give the value in the form of an integer.

Before function declaration there we need to define function i.e int sum(int x, int y); above example is just a declaration of the function to use this function we need to call sum function  then only the sum function will work otherwise it doesn’t work.

C Code,

#include<stdio.h>
int sum(int x, int y); //function declaration
int main (void){
      int x=6, int y=10;
      int total;
      total=sum(x,y); //function call
      printf("n Sum = %dn",total);
      return 0;
}
int sum(int x, int y) //function definition
{
     int result;
     if(x>0 && y>0)
        result = x+y;
     else
        result = 0;
    return result;
}

Function Definition

Definition of a function introduces a new function by declaring the type of value it returns parameters and their types and specifies the statements that are executed when the function is called.

C Syntax

return_type function_name (parameter_list)
{
     declarations;
     statement;
}

the return_type is known as the data type which function will return after the evaluation of the function.

If a function has the return type void, it will not return any value. function_name is the known as the name of the function which can have the parameter_list i.e int x, int y etc.

The parameters also called the formal arguments, parameters are always separated by comma operator.  If the function doesn’t have any parameter then we prefer to use void for the return type.

C Code

int mul(int x, int y)
{
    int a;
    a=x*y;
    return (a);
}

int mul(int x, int y) is known as the header of the function which is known as the function definition. Everything in between braces is known as the function body.

The function body can have the groups of statement which can perform any tasks given by the user. If a function has any return type then the function will use return keyword.

Types of Functions

There are four different ways to use function which is given,

  1. Passing No Argument and Returning No Value.
  2. Passing No arguments and Returning Value. 
  3. Passing Argument and Returning No Value. 
  4. Passing Argument and Returning Value. 

Passing No Argument and Returning No Value,

In this type of function call, the called function receives no value from the calling function and doesn’t send any result to the calling function. 

void function_name (void);

C Code,

/*============================================================
Name        : my.c
Author      : Satya
Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
=============================================================== */
#include <stdio.h>
#include <stdlib.h>
void add(void);
int main(void) {
add(); //function call
return 0;
}
void add(void)
{
int sum, a, b;
printf("Enter Number");
scanf("%d%d",&a,&b);
sum = a+b;
printf("n The Sum is %d",sum);
}

Passing No Argument and Returning Value

In this type of function call, the called function doesn’t receive the value of parameters from the calling function. But the calculated result is sent back to the calling function. 

return_type function_name (void);

C Code,

/*=======================================================
Name        : my.c
Author      : Satya
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ======================================================= * /
#include <stdio.h>
#include <stdlib.h>
int add(void);
int main(void) {
int c;
c=add(); //function call
printf(“The Sum is %d”,c);
return 0;
}
int add(void)
{
int sum, a, b;
printf(“Enter Number”);
scanf(“%d%d”,&a,&b);
sum = a+b;
return (sum);
}

Passing Argument and Returning No Value

In this type of function call, the called function receive the value through parameters from the calling of function, but the result is not sent back to the calling function.

void function_name (type args1, type args2....);

C Code,

/*=======================================================
 Name        : my.c
 Author      : Satya
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ======================================================= */
#include <stdio.h>
#include <stdlib.h>
void add(int, int);
int main(void) {
int a,b;
printf(“Enter two numbers:”);
scanf(“%d%d”,&a,&b);
add(a,b); //function call
return 0;
}
void add(int x, int y)
{
int sum;
sum = x+y;
printf(“The Sum is:%d”,sum);
}

Passing Argument and Returning Value

In this type of function call, the called function receives the value through parameters from the calling of the function and sends back to the result to the calling function.

return_type function_name(type arg1, type arg2,.....)

C Code,

/* ==========================================================
 Name        : my.c
 Author      : Satya
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 =========================================================== */
#include <stdio.h>
#include <stdlib.h>
int add(int, int);
int main(void) {
int a,b,c;
printf(“Enter two numbers:”);
scanf(“%d%d”,&a,&b);
c=add(a,b); //function call
printf(“The Sum is : %d”,c);
return 0;
}
int add(int x, int y)
{
int sum;
sum = x+y;
return (sum);
}