0% found this document useful (0 votes)
6 views

Function in C

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Function in C

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Function in C

Function is a block of statement which can be called by another function is called function.

A function is a set of statement that takes input do some specific computation and produce output.

There are two types of functions in C language

 Library function ( printf(), scanf(), gat(), puts() ETC


 user define function ( created by the user)

calling a function

 call by value
 call by reference

Advantages of user define function:


1. Big programs can be divided into smaller module using functions.
2. Program development will be faster.
3. Program debugging will be easier and faster.
4. Use of functions reduce program complexity.
5. Program length can be reduced through code reusability.
6. Use of functions enhance program readability.
7. Several developers can work on a single project.
8. Functions are used to create own header file i.e mero.h
9. Functions can be independently tested.

How may ways we can create a function?


 No argument no return value
 No argument and return value
 Argument and no return value
 Argument and return value

Write a program to input two numbers and find the sum.


1. No argument and no return value

#inclue<stdio.h>
#include<conio.h>
void sum(); //function decleration
void main()
{
sum(); //function calling
getch();
}
void sum() //function definition
{
Int a,b,c;
printf(“Enter two numbers”);
scanf(“%d%d”, &a,&b);
c=a+b;

printf(“the sum is %d”,c);

}
Factorial OF ANY NUMBER

#include<stdio.h>

#include<conio.h>

int factorial();

int main()

factorial();

getch();

int factorial()

int f=1,i,n;

printf("enter any number");

scanf("%d",&n);

for(i=n;i>=1;i--)

f=f*i;

printf("the factorial value is %d",f);

2. No Argument but return value

#include<stdio.h>

#include<conio.h>

int sum(); //function decleration

void main()

int s;

s=sum(); //function calling

printf(" The sum is %d",s);

getch();

int sum() //function definition

{
int a,b,c;

printf("Enter two numbers");

scanf("%d%d", &a,&b);

c=a+b;

return c;

Factorial OF ANY NUMBER

#include<stdio.h>

#include<conio.h>

int factorial();

int main()

int f;

f= factorial();

printf("the factorial value is %d",f);

getch();

int factorial()

int f=1,i,n;

printf("enter any number");

scanf("%d",&n);

for(i=n;i>=1;i--)

f=f*i;

return(f);

3. Argument with no return

#include<stdio.h>

#include<conio.h>

int sum(int,int); //function decleration


void main()

int m,n, s;

sum(m,n); //function calling

getch();

int sum(m,n) //function definition

int a,b,c;

printf("Enter two numbers");

scanf("%d%d", &a,&b);

c=a+b;

printf(" The sum is %d",c);

FACTORIAL OF ANY NUMBER

#include<stdio.h>

#include<conio.h>

int factorial(int);

int main()

int no=5;

// printf("entar the number");

// scanf("%d",&no);

factorial(no);

getch();

int factorial(int n)

int f=1,i;

for(i=n;i>=1;i--)

f=f*i;

}
printf("the factorial value is %d",f);

//4. argument and return value

#include<stdio.h>

#include<conio.h>

int sum(int,int); //function decleration

void main()

int m,n, s;

s=sum(m,n); //function calling

printf(" The sum is %d",s);

getch();

int sum(m,n) //function definition

int a,b,c;

printf("Enter two numbers");

scanf("%d%d", &a,&b);

c=a+b;

return c;

Factorial of any number

#include<stdio.h>

#include<conio.h>

int factorial(int);

int main()

int no=5;

// printf("entar the number");

// scanf("%d",&no);

no = factorial(no);

printf("the factorial value is %d",no);

getch();
}

int factorial(int n)

int f=1,i;

for(i=n;i>=1;i--)

f=f*i;

return(f);

what is call by value and call by reference in C?


While calling a function, we pass values of variables to it. Such functions are known as “Call by Values”. While calling
a function, instead of passing the values of variables, we pass address of variables (location of variables) to the
function known as “Call by References.

Parameter Call By Value Call By Reference

Return value Function cannot return more than one Function can return more than one
value at a time value at a time

Concept of Concept of pointer cannot be used in call Concept of pointer can be used in call
pointer by value by reference

Change in Any changes made in formal arguments Any changes made in formal
value do not change the actual arguments arguments will be reflected in the
actual arguments

Value sends A copy of data is sent to the function the memory address of data is sent to
the function

Call by Value Example: Swapping the values of the two variables


#include <stdio.h>
void swap(int , int); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a and b in main
swap(a,b);
printf("After swapping values in main a = %d, b = %d\n",a,b); // The valueof actual parameters do not chang
e by changing the formal parameters in call by value, a = 10, b = 20
}
void swap (int a, int b)
{
Output
int temp;
Before swapping the values in main a = 10, b = 20
temp = a;
After swapping values in function a = 20, b = 10
a=b;
After swapping values in main a = 10, b = 20
b=temp;
printf("After swapping values in function a = %d, b = %d\n",a,b); // Formal parameters, a = 20, b = 10
}

Call by reference Example: Swapping the values of the two variables


#include <stdio.h>
void swap(int *, int *); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a and b in main
swap(&a,&b);
printf("After swapping values in main a = %d, b = %d\n",a,b); // The values of actual parameters do change i
n call by reference, a = 10, b = 20
} Output
void swap (int *a, int *b) Before swapping the values in main a = 10, b = 20
{ After swapping values in function a = 20, b = 10
int temp;
temp = *a;
*a=*b;
*b=temp;
printf("After swapping values in function a = %d, b = %d\n",*a,*b); // Formal parameters, a = 20, b = 10
}

You might also like