C Functions

Function is a block of code which performs a specific task . function is a process . 

There are main two types of functions in c 

  1. System defined function : These types of functions are pre-defined functions . The functions which are already defined by system and user can not change them these functions called as system defined functions . e.g void main()
  1. User defined functions : These functions are defined by user . It has main four types which are explained below .
  • Function with arguments and with return value
  • Function without arguments and without return value
  • Function with arguments but no return value
  • Function without arguments but with return value
Parts of user defined function
  1. function declaration or prototype
  2. function calling
  3. function definition
Let's solve with the help of diagram,






Syntax :

data_type function_name(parameter/arguments)

int fun(int x, int y)

Let's explain it ,

1.Function with arguments and with return value:


Here we will pass the arguments and return value as well.

Example :

Q. Write c program for sum of two numbers using function 


#include<stdio.h>
#include<conio.h>
int sum(int p,int q);
void main()
{
int x,y,z;
printf("Enter value for x : ");
scanf("%d",&x);
printf("Enter value for y : ");
scanf("%d",&y);
z=sum(x,y);
printf("Sum =%d",z);
getch();
}
int sum(int p,int q)
{
int z;
z=p+q;
return z;
}


2. Function without arguments and without return value

This will be the function without arguments and without any return value

Example :

Q. Write C program that uses no arguments and no return value 


#include<stdio.h>
#include<conio.h>
void list(void);
void main()
{
int choice;
menus();
printf("Enter your choice : ");
scanf("%d",&choice);
getch();
}
void menus(void) 
{
printf("1.Ishita\n");
printf("2.Amar\n");
printf("3.Bhavana\n");
printf("4.Jogi\n");
}


Function with arguments but no return value