Example : A program in c to display output using simple function .

#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>
#include<conio.h>
void main ()
{
	void sample();
	
	printf("\nWe are in main function");	
	getch();
}
void sample()
{
printf("\nWe are in sample function");
}

OUTPUT :
We are in main function

Example : A C program to pass a string through a function .

#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 show call by value function .

#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 show call by reference function .

#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 function  .

# 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 : WAP in C to calculate area and circumference of a circle using a user define                                 function and print/display the result in main( ).
#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>

int main()
{
	int r1,r2;
	float area1, area2;
	
	void area(int r1, int r2, float *, float *);
	
	printf("Enter the radius for area of circle : \n");
	scanf("%d", &r1);
	
	printf("\nEnter the radius for circumference of circle : \n");
	scanf("%d", &r2);
	
	area(r1,r2,&area1,&area2);
	printf("\n\nArea of Circle is  =  %f", area1);
	printf("\nCircumference of Circle is = %f ", area2);
	
	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 for area of circle :
5
Enter the radius for circumference of circle :
6
Area of Circle is  =  78.500000
Circumference of Circle is = 37.692001

-------------  OR  ---------------
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;
 }

-------------  OR  ---------------
#include<stdio.h>
int fun()
{
	int i=90;	
	return i;
}

void main()
{
	int a;	
	a=fun();	
	printf("%d",a);
        getch();
}

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

{
	int value=100;
	return value;
}

int main()

{
	printf("%d",function());
	return 0;
}

-------------  OR  ---------------
#include<stdio.h>
void func1 (void) __attribute__ ((destructor));

void func1 (void)
{
    printf ("The output returned by main() is = %d",main());
}

int main (void)
{
    int a=90, b=80,c;
    c=a+b;
    return c;
}
Examples of Function Type – With Parameter(Argument) with Return

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)     // or square(int)
{ 
	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);

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); 
}
Examples of Function Type – With Parameter(Argument) without(No) Return
Examples of Function Type – Without Parameter(Argument) with Return
Examples of Function Type – Without Parameter(Argument) without(No) Return

 1,106 total views,  1 views today

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.