Beruflich Dokumente
Kultur Dokumente
Functions
10 20
Sum=30
Functions
Why functions
Program that we study from textbooks to learn c language
are very small when compared to real world business
problems, if the program is very big, there are some
disadvantages
1. Very difficulty to understand the big program without
modules
2. It is very difficulty for the programmer to write large
programs
3. Is it difficulty to identify the logical error and debug
4. Large programs are more prone to errors
These disadvantages can be overcome using functions.
Functions:
Example:
Function definition
Return_ type function_name(arglist) Calling a function
{ By using function name
//local variable declaration Ex Add(10,20);
// statements
//return statement
}
Write a program to find area of circle using Write a program to add two numbers
functions using functions
#include<stdio.h>
#include<stdio.h>
float findarea(int r);
int add(int x,int y);
Debugging is easy.
Function definition
Return_ type function_name(arglist) Calling a function
{ By using function name
//local variable declaration Ex Add(10,20);
// statements
//return statement
}
To add two numbers using functions
#include<stdio.h>
int add(int x,int y);
void main() a??? b??? sum??
{
int a,b,sum;
printf(“enter two numbers”); a=10 b=20 sum??
Sum=
scanf(“%d%d”&a,&b);
sum=Add(a,b); 30
printf(“sum is %d”,sum);
}
int Add(int x,int y)
{
int z; //local variable declaration X=10 Y=20
z=x+y; // statements
reurn z; //return statement
} z???
Z=30
Total space =2+2+2
2+2+2 bytes
Elements of User Defined Functions(UDF)
Function definition
Function call
Function declaration
program using functions with all elements indicated(adding two numbers)
include<iostream>
using namespace std ?????
int add(int x,int y); function declaration
void main()
{
int a,b;
printf(“enter two numbers”);scanf(“”%d%d”,&a,&b);
sum=add(a,b); ?????
Printf(“sum is %d”,sum) function call(caller)
}
int add(int x,int y)
{
int z;z=x+y; ??????????
return z; function definition(cally)
}
Types of functions:
Built-in/Library functions
Ex: sqrt(),pow(),log()…. Etc
User defined functions
Ex: Add(), max(a,b)
void add()
{ void TestFunct(void)
int a,b,sum;
{
printf(“Enter the values for a & b\n”);
scanf(“%d %d, &a, &b); // receive nothing
// and nothing to be
sum= a+b;
// returned
printf(“%d”, sum); }
}
2) Functions with no parameters and return values
#include <stdio.h>
void add(); void main(void)
{
int main() x = TestFunct();
{ ...
int sum;
}
sum=add();
printf(“sum is %d”,sum)
}
int TestFunct(void)
int add() {
{ // received nothing
int a,b,sum;
//but need to
printf(“Enter the values for a & b\n”);
scanf(“%d %d, &a, &b); // return something
return 123;
sum= a+b;
}
return sum
} Hey Add any two numbers and give me result
3) Functions with parameters and no return values
#include <stdio.h>
void add(int x,int y) void main(void)
{
int main() x = TestFunct();
{ ...
int a,b,sum;
}
cout<<“enter two numbers”;
scanf(“%d%d”,&a,&b);
sum=Add(a,b);
} void TestFunct(int x,…. …)
{
void add(int x,int y) // receive something
{ //but need not
int z; // return anything
z=x+y;
printf(“sum is %d”,z);
}
}
Function fact(n)
Step1:f=1
Step2: find factorial
for(i=1;i<=n;i++)
f=f*i
end for
Step 3: return f
1.Read the range n1 & n2
2. Generate prime numbers between n1& n2
for(i=n1; i<=n2; i++)
k=prime(i)
if(k==1)
print i;
end for
3.stop
Function isprime(n)
1. for(i=2;i<n;i++)
if(n%i==0)
return0
end if
return 1
end for
B
start
Isprime(n)
Read n1, n2
For(i=2;i<n;i++)
for i=n1; i<n2; i++ F
T
n%i=
A Isprime(i) B
0?
Print “i”
A
stop
Actual parameters and Formal parameters
Actual parameters: parameters present in the function call
Ex:
sum=Add(a,b); //here a and b are actual parameters
}
Note:
The number of actual parameters should be equal to number of
formal parameters( both number and type should match)
Scope of variables in functions
#include <stdio.h>
int add(int x,int y);
a,b ,sum are
local to main
int main()
{
int a,b,sum;
cout<<“enter two numbers”;
scanf(“%d%d”,&a,&b);
sum=Add(a,b);
printf(“the sum is”,sum)
}
Note:
The number of actual parameters should be equal to number of
formal parameters( both number and type should match)
Passing parameters to functions
Pass by value
Pass by reference
Write prgm to show pass by value and pass by ref
Example: By value Example: By reference
int add(int x,int y); void swap(int *x,int *y);
void main() int main()
{
{
int a=10,b=20;
int a,b,sum;
printf(“before swap %d%d”,a,b);
printf(“enter two numbers”); swap(&a,&b);
scanf(“%d%d”,&a,&b); printf(“after swap %d%d”,a,b);
sum=Add(a,b); }
printf(“sum is %d”,sum); void swap(int x*,int *y)
} {
int Add(int x,int y) int temp;
{ temp=*x;
*x=*y;
return(x+y);
*y=temp;
}
}
Main boss says, take these values and swap them, give me swapped values
Passing parameter to functions
int main() int main()
{ {
int a=10,b=20; sender(10,20);
sender(a,b); }
}
Receiver(int x,int y)
Receiver(int x, int y) { int main()
{ --------------- {
----------- } int a=10,b=20;
}
sender(&a,&b);
}
int main() int main()
Receiver(int *x,int *y)
{ int a=10,b=20; {
{
sender(a+2,b+3); int a=10; float b=20.5;
--------------------
} sender(a,b);
}
}
Receiver(int x,int y) Receiver(int x,float y)
{ {
---------------
--------------------
}
}
passing arrays to functions
Just pass the base address of the array to a function
Declaration :
data_type arr_name[rowsize][columnsize]
ex: int a[2][2]; int a[2][2]={ {1,2},
int matrix[3][3];
{3,4}
int a[2][2]={ {1,2}, {3,4} };
};
int a[2][2]={ {1,2}, {3,4} };
0,0 0,1 rows
1 2
1 2
a[0][0] a[0][1]
3 4
1,0 1,1
3 4
Column a[1][0] a[1][1]
i= row
j=column
In general to access any particular element
a[i][j]
Using 2-dimensional arrays:
Reading 2D arrays Printing 2D arrays
printf("entr the order of matrix");
printf(“matrix elements”);
scanf("%d%d",&m,&n);
printf("enter matrix A"); for(i=0; i<m; i++)
for(i=0;i<m;i++) {
{
for(j=0; j<n; j++)
for(j=0;j<n;j++)
{ {
scanf("%d",&a[i][j]); printf(“%d”, a[i][j]);
} }
}
}
Manipulation of 2-dimensional arrays:
Transpose of a matrix.
Trace of a matrix.
WAP to find sum of all elements in a matrix
sum =sum+a[i][j];
Using functions to find sum of all elements in a
matrix
......................
int main()
{
int a[10][10],m,n;
findsum(a,m,n);
}
void readmatrix(int a[10][10],int m,int n)
{
int i,j;
printf("enter matrix elements");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
}
Function to find sum of all elements in a matrix
void findsum(int a[10][10],int m, int n)
{
int sum=0,i,j;
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
sum=sum+a[i][j];
}
}
printf("the sum is %d",sum);
}
int main() }
{
int a[10][10],b[10][10],c[10][10];
int i,j,m,n; for(i=0;i<m;i++)
printf("entr the order of matrix"); {
scanf("%d%d",&m,&n); for(j=0;j<n;j++)
printf("enter matrix A"); {
for(i=0;i<m;i++) c[i][j]=a[i][j]+b[i][j];
{ }
for(j=0;j<n;j++) }
{ printf("sum of matrix A and B is ");
scanf("%d",&a[i][j]); for(i=0;i<m;i++)
} {
} for(j=0;j<n;j++)
printf("enter matrix B"); {
for(i=0;i<m;i++) printf("%d",c[i][j]);
{ }
for(j=0;j<n;j++) }
{ return 0;
scanf("%d",&b[i][j]); }
} Matrix Addition
Add 2 matrices and store the result in c using functions
int main()
{
int a[10][10],b[10][10],c[10][10]; int m,n,p,q;
printf("entr the order of matrix A");
scanf("%d%d",&m,&n); Read matrix a of
readmatrix(a,m,n); order mxn
int sum=0,i,j;
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
}
void printmatrix(int c[10][10],int m,int n)
{
int i,j;
printf("sum of matrix A and B is ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d",c[i][j]);
}
}
}
WAP using functions to find trace of a matrix
Trace of a matrix :
Matrix A of size 3x3
10 15 20
Trace
25 30 35
10+30+50 = 90
40 45 50
We can write the following steps:
Initial : sum = 0
Step 1: sum = sum + a[0][0]
Step 2: sum = sum + a[1][1]
Step 3: sum = sum + a[2][2]
.................................................
.................................................
Step n: sum = sum + a[n-1][n-1]
In general,
sum = sum + a[i][i] where i=0,1,2,3......n-1
So, we are forced to use the for statement:
to find the trace of a matrix.
sum=0
for(i=0; i<n; i++)
{
sum=sum+a[i][i];
}
-------------------------
WAP using functions to find product of 2 matrices
Home work
type
parameters
Function
Definition Declaration
statements
return statements
C FUNCTIONS
Do not pass argument Do pass arguments
void main(void) void main(void)
{ {
TestFunct(); TestFunct(123);
... ...
} }
www.tenouk.com, © 65/66
Functions
Manager
Programmer
Programmer 1 Programmer 3
2