0% found this document useful (0 votes)
28 views2 pages

C Function No Arguments

Uploaded by

Sunil Kumar
This document contains code examples that demonstrate different ways to define functions in C without arguments or return values, with arguments and return values, and calling functions by value vs reference. It includes code for functions that calculate the area of a circle with and without passing radius as an argument. It also provides explanations of calling functions by value vs reference and examples of each.

Copyright:

© All Rights Reserved

Available Formats

Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
28 views2 pages

C Function No Arguments

Uploaded by

Sunil Kumar
This document contains code examples that demonstrate different ways to define functions in C without arguments or return values, with arguments and return values, and calling functions by value vs reference. It includes code for functions that calculate the area of a circle with and without passing radius as an argument. It also provides explanations of calling functions by value vs reference and examples of each.

Copyright:

© All Rights Reserved

Available Formats

Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 2

C Function No Arguments & No C Function Arguments

Return Value
Return Value
#include<stdio.h>
void area(); // Prototype Declaration
void main()
{
area();
}
void area()
{
float area_circle;
float rad;
printf("\nEnter the radius : ");
scanf("%f",&rad);
area_circle = 3.14 * rad * rad ;
printf("Area of Circle=%f",area_circle);
}

&

No

#include<stdio.h>
#include<conio.h>
void area(float rad);
void main()
{
float rad;
printf("nEnter the radius : ");
scanf("%f",&rad);
area(rad);
getch();
}
void area(float rad)
{
float ar;
ar = 3.14 * rad * rad ;
printf("Area of Circle = %f",ar);
}

Function with argument and return Function with


type
return type

no argument but

#include<stdio.h>
float calculate_area(int);
int main()
{
int radius;
float area;
printf("\nEnter the radius of the
circle : ");
scanf("%d",&radius);
area = calculate_area(radius);
printf("\nArea of Circle : %f ",area);
return(0);
}
float calculate_area(int radius)
{
float areaOfCircle;
areaOfCircle = 3.14 * radius * radius;
return(areaOfCircle);
}

#include<stdio.h>
float calculate_area();
int main()
{
float area;
printf("\nEnter the radius of the
circle : ");
area = calculate_area();
printf("\nArea of Circle: %f ",area);
return(0);
}
float calculate_area()
{
int radius;
float areaOfCircle;
scanf("%d",&radius);
areaOfCircle =3.14 * radius * radius;
return(areaOfCircle);
}

Function call by value

Function call by reference in C

This method copies the actual value of an


argument into the formal parameter of the
function. In this case, changes made to the
parameter inside the function have no effect on
the argument.

This method copies the address of an argument


into the formal parameter. Inside the function,
the address is used to access the actual
argument used in the call. This means that
changes made to the parameter affect the
argument.

#include <stdio.h>
/* function declaration */
void swap(int x, int y);

#include <stdio.h>
/* function declaration */
void swap(int *x, int *y);

int main ()
{
/* local variable definition */
int a = 100;
int b = 200;

int main ()
{
/* local variable definition */
int a = 100;
int b = 200;
printf("Before swap,value of a:%d\n",a );

printf("Before swap, value of a:%d\n", a );


printf("Before swap, value of b:%d\n", b );
swap(a, b);
printf("After swap, value of a: %d\n", a );
printf("After swap, value of b: %d\n", b );
return 0;
}
void swap(int x, int y)
{
int temp;
temp = x; /* save the value of x */
x = y;
/* put y into x */
y = temp; /* put temp into y */
}

return;

printf("Before swap,value ofb:%d\n",b );


swap(&a, &b);
printf("After swap,value of a:%d\n",a);
printf("After swap,value of b:%d\n",b );
return 0;
}
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
/* put
temp into y */
}

return;

You might also like