Example : A C program to display message using User Defined Function (UDF).
#include<stdio.h>
#include<conio.h>
void main ()
{
	sample();
	
	printf("\nWe are in main function");	
	getch();
}

void sample()
{
   printf("\nWe are in sample function");
}

OUTPUT :
We are in sample function
We are in main function

------------------------  OR  ----------------------
#include<stdio.h>
int function()

{
	int value=100;
	return value;
}


int main()

{
	printf("%d",function());
	return 0;
}
Example : A C program to display the Local and Global variables .
#include<stdio.h>
#include<conio.h>

int a,b;	
	a=50;    // Global Variable
	b=60;    // Global Variable

void main()
{
	int sum;	
	sum=a+b;
	printf("The results are = ");	
	printf("%d %d %d",a,b,sum);	
	
	a=100;
	b=200;
	
	printf("%d %d",a,b);
	
	getch();
}

Output:
The results are = 50 60 110
100 200

---------------  OR  ---------------

#include<stdio.h>
#include<conio.h>

int a,b;	
	a=50;    // Global Variable
	b=60;    // Global Variable

void main()
{
	int a,b,sum;
	a=20;      // Local Variable
	b=30;      // Local Variable	
	
	sum=a+b;
	printf("The results are = ");	
	printf("%d %d %d",a,b,sum);	
	
	getch();
}

Output:
The results are = 20 30 50

---------------  OR  ---------------

#include<stdio.h>
#include<conio.h>

int a,b;	
	
	a=50;    // Global Variable
	b=60;    // Global Variable	

void main() 
{
	printf("%d %d",a,b);
	printf("\n");
	
	int a,b,sum;
	a=20;      // Local Variable
	b=30;      // Local Variable	
	
	sum=a+b;
	printf("The results are = ");	
	printf("%d %d %d",a,b,sum);	
	
	getch();
}

Output:
50 60
The results are = 20 30 50
Example : A C program to pass an Array through a User Defined Function(UDF) .
#include <stdio.h>
#include <conio.h>

int arrayvalsum(int x[]);

int main() 
{
    int sumtotal, x[] = {22,50,47,89,35};
    
    sumtotal = arrayvalsum(x); 
    printf("The total sum of array values are = %d", sumtotal);
    return 0;
}

int arrayvalsum(int y[]) 
{

  int i, sum = 0;

  for (i = 0; i < 5; i++) 
  {
      sum =sum+ y[i];
  }

  return sum;
}
Example : A C program to pass a String through a User Defined Function(UDF) .
#include <stdio.h>

void output(char str1[]);

int main()
{
    char str1[100];
    printf("Enter String Value = ");
    gets(str1);
	             
    output(str1);        
    return 0;
}

void output(char str2[])
{
    printf("String Result is = ");
    puts(str2);
}

Output :
Enter String Value = Codershelpline
String Result is = Codershelpline
Example : A C program to pass a Structure through a User Defined Function(UDF) .
#include <stdio.h>
#include <string.h>

struct student 
{
    int rollno;
    char sname[30];
    float permarks;
};

void strpass(struct student stu);
 
int main() 
{
    struct student stu;

    stu.rollno=107;
    strcpy(stu.sname, "Robert");
    stu.permarks = 73.25;

    strpass(stu);
    return 0;
}
 
void strpass(struct student stu1)
{
    printf("Student Rollno is: %d \n", stu1.rollno);
    printf("Student Name is: %s \n", stu1.sname);
    printf("Student Percentage Marks is: %.2f \n", stu1.permarks);
}
Example : A C program to pass a Structure through a User Defined Function(UDF) .
#include <stdio.h>

int main()
{
	int r1,r2;
	float area1, area2;
	
	void area(int r1, int r2, float *, float *);
	
	printf("Enter the radius of circle to calculate Area : \n");
	scanf("%d", &r1);
	
	printf("\nEnter the radius of circle to calculate circumference : \n");
	scanf("%d", &r2);
	
	area(r1,r2,&area1,&area2);
	
	printf("\n\nArea of Circle is  =  %.2f", area1);
	printf("\nCircumference of Circle is = %.2f ", area2);
	getch();
	return 0;
	
}

void area(int m, int n, float *area, float *circumference)
{
   *area = 3.14*m*m;
   *circumference = 2*3.141*n;
}
Example : A C program to display Call by Value concept using User Defined  Function (UDF).
#include <stdio.h> 
 
void change(int,int);  
int main()  
{  
    int a=50,b=60;
    
    printf("First value before calling of function of is : %d",a);  
    printf("\n");  
    printf("Second value before calling of function of is : %d",b);
	  
    change(a,b);
	 
    printf("\n\nFirst value after calling of function of is : %d",a);  
    printf("\n");  
    printf("Second value after calling of function of is : %d",b);  
    return 0;  
}  
void change(int x,int y)  
{  
    printf("\n\nFirst value inside body of function but before change is : %d",x);  
    printf("\n");  
    printf("Second value inside body of function but before change is : %d",y); 
    
    x=100;  
    y=200;
    
    printf("\n\nFirst value after calling of function but after change is : %d",x);  
    printf("\n");  
    printf("Second value after calling of function but after change is : %d",y); 	
	  
}  
Output :
First value before calling of function of is : 50
Second value before calling of function of is : 60

First value inside body of function but before change is : 50
Second value inside body of function but before change is : 60

First value after calling of function but after change is : 100
Second value after calling of function but after change is : 200

First value after calling of function of is : 50
Second value after calling of function of is : 60
Example : A C program to display call by reference concept using User Defined Function(UDF).
#include <stdio.h> 
 
void change(int*,int *);  
int main()  
{  
    int a=50,b=60;
    
    printf("First value before calling of function of is : %d",a);  
    printf("\n");  
    printf("Second value before calling of function of is : %d",b);
	  
    change(&a,&b);
	 
    printf("\n\nFirst value after calling of function of is : %d",a);  
    printf("\n");  
    printf("Second value after calling of function of is : %d",b);  
    return 0;  
}  
void change(int *x,int *y)  
{  
    printf("\n\nFirst value inside body of function but before change is : %d",*x);  
    printf("\n");  
    printf("Second value inside body of function but before change is : %d",*y); 
    
    *x=100;  
    *y=200;
    
    printf("\n\nFirst value after calling of function but after change is : %d",*x);  
    printf("\n");  
    printf("Second value after calling of function but after change is : %d",*y);	
	  
}  
Output :
First value before calling of function of is : 50
Second value before calling of function of is : 60

First value inside body of function but before change is : 50
Second value inside body of function but before change is : 60

First value after calling of function but after change is : 100
Second value after calling of function but after change is : 200

First value after calling of function of is : 100
Second value after calling of function of is : 200
Example : A C program to return multiple values using call by reference function .
#include <stdio.h>

int main()
{
	float len,breadth;
	float peri, area;
	
	void periarea(float length, float breadth, float *, float *);
	
	printf("\nEnter the length and breadth of a rectangle in metres: \n");
	scanf("%f %f",&len,&breadth);
	
	periarea(len,breadth,&peri,&area);
	printf("\nPerimeter of the rectangle is %f metres", peri);
	printf("\nArea of the rectangle is %f sq. metres", area);
	
	return 0;
}

void periarea(float length, float breadth, float *perimeter, float *area)
{
   *perimeter = 2 * (length +breadth);
   *area = length * breadth;
}

Output :

Enter the length and breadth of a rectangle in metres:
10
20

Perimeter of the rectangle is 60.000000 metres
Area of the rectangle is 200.000000 sq. metres
Example : A C program to return pointer using User Defined Function (UDF).
# include<stdio.h>

int main( )
{
	float *a;
	
	float *func( ); /* function prototype */
	
	a = func( );
	printf ("Address = %u", a);
	return 0;
}

float *func( )
{
	float r = 2.132;
	return (&r);
}

Output :
Address = 2358796
Example : A C program to Pass Pointer through a User Defined Function(UDF) and display the return output.
#include <stdio.h>

int main()
{
	int r1,r2;
	float area1, area2;
	
	void area(int r1, int r2, float *, float *);
	
	printf("Enter the radius of circle to calculate Area : \n");
	scanf("%d", &r1);
	
	printf("\nEnter the radius of circle to calculate circumference : \n");
	scanf("%d", &r2);
	
	area(r1,r2,&area1,&area2);
	
	printf("\n\nArea of Circle is  =  %.2f", area1);
	printf("\nCircumference of Circle is = %.2f ", area2);
	getch();
	return 0;	
}

void area(int m, int n, float *area, float *circumference)
{
   *area = 3.14*m*m;
   *circumference = 2*3.141*n;
}

Output:
Enter the radius of circle to calculate Area :
4

Enter the radius of circle to calculate circumference :
6


Area of Circle is  =  50.24
Circumference of Circle is = 37.69
Example : A C program to take values through Command Line Arguments in a User Defined Function(UDF) and display the return output.

void fun1(int *x1, int *y1)
{
   *x1  = 10;
   *y1  = 20;
 }

int main(int argc, char* argv[]) 
 {
	int x,y;
	fun1(&x, &y);
	printf("%d %d",x,y);
	return 0;
 }
Output:
10
20
Function Type – With Parameters/Arguments with Return Examples.
Example : A program in C to calculate the square value of a given number using user defined function (UDF) .
#include<stdio.h>
#include<conio.h>

square(int num)     
{ 
	int result ; 
	result = num*num;
	return (result); 
}

void main( )
{
	int n,sq; 
	printf("Enter a number to calculate square= ");
	scanf("%d",&n);
	
	sq=square(n); 
	printf ("\nThe Square of the number is : %d", sq);
	getch();
} 

---------------------------- OR -----------------------------
#include<stdio.h>
#include<conio.h>

square(int num)     
{ 
	int result ; 
	result = num*num;
	return (result); 
}

void main( )
{
	int num,sq; 
	printf("Enter a number to calculate square= ");
	scanf("%d",&num);
	
	sq=square(num); 
	printf ("\nThe Square of the number is : %d", sq);
	getch();
} 

---------------------------- OR -----------------------------
#include<stdio.h>
#include<conio.h>

square(int num);     //square(int );

void main( )
{
	int n,sq; 
	printf("Enter a number to calculate square= ");
	scanf("%d",&n);
	
	sq=square(n); 
	printf ("\nThe Square of the number is : %d", sq);
	getch();
} 
square(int num)
{ 
	int result ; 
	result = num*num;
	return (result); 
}

----------------------------   OR  ---------------------------

#include<stdio.h>
#include<conio.h>

void main( )
{
	int square(int num);
	
	int n,sq;	 
	printf("Enter a number to calculate square= ");
	scanf("%d",&n);
	
	sq=square(n); 
	printf ("\nThe Square of the number is : %d", sq);
	getch();
}
 
square(int num)
{ 
	int result ; 
	result = num*num;
	return (result); 
}

----------------------------   OR  ---------------------------
#include<stdio.h>
#include<conio.h>

void main( )
{
	int square(int num);
	
	int n,sq;	 
	printf("Enter a number to calculate square= ");
	scanf("%d",&n);
	
	sq=square(n); 
	printf ("\nThe Square of the number is : %d", sq);
	getch();
}
 
square(int num1)
{ 
	int result ; 
	result = num1*num1;
	return (result); 
}
Function Type – With Parameters/Arguments with No/Without Return Examples
Example : A program in C to calculate the square value of a given number using user defined function (UDF) .
#include<stdio.h>
#include<conio.h>

square(int num);    

void main( )
{
	int n, sq; 
	printf("Enter a number to calculate square value = ");
	scanf("%d",&n);		
	square(n);
	getch();
}
 
square(int num1)
{ 
	int result ;	
	result = num1*num1;
	printf ("\nThe Square of the given number is : %d", result);
}

----------------------------   OR  ---------------------------

#include<stdio.h>
#include<conio.h>

void square(int num);    

void main( )
{
	int n, sq; 
	printf("Enter a number to calculate square value = ");
	scanf("%d",&n);		
	square(n);
	getch();
}
 
void square(int num1)
{ 
	int result ;	
	result = num1*num1;
	printf ("\nThe Square of the given number is : %d", result);
}

----------------------------   OR  ---------------------------

#include<stdio.h>
#include<conio.h>

void square(int );    

void main( )
{
	int n, sq; 
	printf("Enter a number to calculate square value = ");
	scanf("%d",&n);		
	square(n);
	getch();
}
 
void square(int num1)
{ 
	int result ;	
	result = num1*num1;
	printf ("\nThe Square of the given number is : %d", result);
}

----------------------------   OR  ---------------------------

#include<stdio.h>
#include<conio.h>

void square(int );    

void main( )
{
	int n,num,sq;	
	printf("Enter a number to calculate square value = ");
	scanf("%d",&n);
	num=n; 		
	square(num);
	getch();
}
 
void square(int num1)
{ 
	int result ;	
	result = num1*num1;
	printf ("\nThe Square of the given number is : %d", result);
}

----------------------------   OR  ---------------------------

#include<stdio.h>
#include<conio.h>

void square( );    

void main( )
{
	int n,sq;	
	printf("Enter a number to calculate square value = ");
	scanf("%d",&n);			
	square(n);
	getch();
}
 
void square(int num1)
{ 
	int result ;	
	result = num1*num1;
	printf ("\nThe Square of the given number is : %d", result);
}

----------------------------   OR  ---------------------------

#include<stdio.h>
#include<conio.h>

//void square( );    [Warning appears but program executed] conflicting types for 'square'

void main( )
{
	int n,sq;	
	printf("Enter a number to calculate square value = ");
	scanf("%d",&n);			
	square(n);
	getch();
}
 
void square(int num1)
{ 
	int result ;	
	result = num1*num1;
	printf ("\nThe Square of the given number is : %d", result);
}
Function Type – With No/Without Parameters/Arguments with Return Examples
Example : A program in C to calculate the square value of a given number using user defined function (UDF) .
#include<stdio.h>
#include<conio.h>

square();    

void main( )
{
	int sq; 
		
	sq=square(); 
	printf ("\nThe Square of the given number is : %d = ", sq);
	getch();
}
 
square()
{ 
	int n,result ;
	
	printf("Enter a number to calculate square value = ");
	scanf("%d",&n); 
	result = n*n;
	return (result); 
}
Example : A C program to calculate and display the area and circumference of a circle using user defined function(UDF).
#include<stdio.h>

float circle()
{
	int r1;
	float area1;
	printf("Enter the value of radius for circle area = ");
	scanf("%d",&r1);
	area1=3.141*r1*r1;
	return area1;
}

float circumference()
{
	int r2;
	float area2;
	printf("Enter the value of radius for circle circumferene = ");
	scanf("%d",&r2);
	area2=2*3.141*r2;
	return area2;
}

int main()
{
	printf("%f\n",circle());
	printf("%f",circumference());
	
	return 0;
}

Output :
Enter the value of radius for circle area = 2
12.564000
Enter the value of radius for circle circumference = 4
25.128000

-----------------------  OR  -----------------------

#include<stdio.h>

float circle();
float circumference();

int main()
{
	printf("%f\n",circle());
	printf("%f",circumference());
	
	return 0;
}

float circle()
{
	int r1;
	float area1;
	printf("Enter the value of radius for circle area = ");
	scanf("%d",&r1);
	area1=3.141*r1*r1;
	return area1;
}

float circumference()
{
	int r2;
	float area2;
	printf("Enter the value of radius for circle circumferene = ");
	scanf("%d",&r2);
	area2=2*3.141*r2;
	return area2;
}
Function Type – With No/Without Parameters/Arguments without/No Return Examples
Example : A program in C to calculate the square value of a given number using user defined function (UDF) .
#include<stdio.h>
#include<conio.h>

square();    

void main( )
{
	int sq; 
		
	square(); 

	getch();
}
 
square()
{ 
	int n,result ;
	
	printf("Enter a number to calculate square value = ");
	scanf("%d",&n); 
	result = n*n;
	printf ("\nThe Square of the given number is : %d  ", result);
	
}

-----------------------  OR  -----------------------


#include<stdio.h>
#include<conio.h>

void square();    

void main( )
{
	int sq; 
		
	square(); 

	getch();
}
 
void square()
{ 
	int n,result ;
	
	printf("Enter a number to calculate square value = ");
	scanf("%d",&n); 
	result = n*n;
	printf ("\nThe Square of the given number is : %d  ", result);
	
}

Loading

Categories: C

0 Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.