Lab 4

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Kantipur Engineering College

Lab report 4

C lab report submitted by :

Name : prabesh raj upadhaya

Roll no : 21

Lab Date: 2078/03/08

SIGN
TITLE : Function

OBJECTIVE:
To understand the use of function in a program.

THEORY:
1) Function:
A function is a self-contained program segment that carries out some specific,
well defined task. Functions are used to encapsulate a set of operations and
returns information to the main program or calling routine. A c program is
usually made up of many small functions, each performing a particular tank,
rather than one single large main() function. There are two types of functions:
• Library Functions: They are predefined functions or built in functions is C
library. Example: printf(), scanf(), sqrt () etc.
User-defined Functions. They are functions written by the user at the time of
writing a program. Example: int sum( int a, int b)
int total = a + b ;
return total;
}

2) Function declaration or Prototyping: Function declaring or prototyping means


providing the or sample of a function we are going to use. it includes function
type, function name, type of passing argument. it should be done if we are
writing a function below the function that calls it
Example: int sum (int, int)

3) Function definition :
A function definition has two principal component.
a) functions declarator: It is the first of line a function that should be similar to
the function prototype but with variables with its type in the passing argument.
Syntax: return type function name (type1 arg1, type2 arg 2…..)
Example:
int sum (int a, int b).
arg1, arg2….. are called formal arguments as formal parameters which are
local to the function.
b) Body of the function: Function declarator is followed by the function body, it is
composed of statements that make up by braces which define the actions to be
takes by the function.

4) Passing arguments :
Providing values to the formal argument of a called function trough the actual
arguments of the calling function is called passing arguments.
Example:
int num1 = 2;
int num 2 = 3;
int total = scum (num 1, num 2);
Here, num 1 & num 2 is actual argument
5) Return Statement:
A function may or may not send bad any value to the calling function. If it does,
it is through the return statement. While it is possible to pass to the called
function any number of arguments but the called function can only return come
value per call, at the most. The returns statement can take one of the following
forms.
Syntax: return or return (expression);
If no value is to be return, return statement need not be present.

6) Elimination of function declaration: if the functions are called above the


function they are defined we need the prototype but if we write the function.
above the calling functions call don't need prototyping.

7) Function call:
Function calling is the process. of running and evaluating the function by
passing actual argument and values. I function call is specified by the function
name fallowed by the values parameters enclosed within parameters,
terminated by a semicolon. If you need to store the returned. data in a variable,
then assigns this call to a variable.
Syntax: return type variable= function name (arg1,arg 2 ……)

8) Return types:
There are 5 return types
• char
• float
 void
 painter return type
 int

(9) Ways of passing arguments to a function:


we can pass arguments to a function in two ways.
a) Pass by value:
Pass by value means to call the function by passing the value as to the
function. In this method, changes made to the formal argument in the called
function have no effect on the values of actual argument in the calling function.
b) Pass by reference:
Pass by reference means to call the function by passing address (reference) as
argument to the function. In this method, we can change the value of actual
argument from the called function.
10) Recursive Function:
A recursive function is a function that calls itself during the execution. The
process may expect several times, output of the result and the end of each
iteration. Two important condition that must be satisfied by any recursive
function are:
• Each time a function call itself, it must be closer a solution.
• There must be a decision criteria for stopping the process.
Therefore, recursion is defining large and complex problem in terms salvable
problem of a smaller and more easily soluble problem.
PROGRAMS

1) Write a program to calculate the permutation and combination for giver


n and r. the permutation and combination are given n and r defined for
p(n,r) = n!/(n-r)!
c(n,r) = p(n,r)/r!
Algorithm:
For Calling Function double main ( )
Step 1: Start
Step 2: Declare variables n, r, c as double.
Step 3: Read n and r.
Step 4: calculate c as: c = permu(n, r) / fact(r)
Step 5: Print permutation.
Step 6: Print combination
Step 7: Stop.
For Called Function double fact(double
Step 1: Declare variables i, f.
Step 2: Assign i = 1 and f= 1.
Step 3: Check whether i <= r?
If true, calculate f= f* i and i = i +1, then again go to step 3. If false, go to step 4.
Step 4: return f.
For Called Function double permu(double n, double r)
Step 1: Declare variable p.
Step 2: Calculate permutation as: p = fact(n) / fact(n – r) and go to step 3.
Step 3: return p.
Source Code:
#include <stdio.h>
#include <stdlib.h>
double fact(double);
double permu(double,double);
int main()
{
double n,r,c;
printf("Enter value of n: \n");
scanf("%lf",&n);
printf("Enter value of r: ");
scanf("%lf",&r);
c=permu(n,r)/fact(r);
printf("Permutation=%.3lf\n",permu(n,r));
printf("Combination= %.3lf",c);
return 0;
}
double fact(double r)
{
double f=1,i; for(i=1;i<=r;i++) f=f*i;
return f;
}
double permu(double n,double r)
{
double p; p=fact(n)/fact(n-r);
return p;
}
Output:
Enter value of n: 5
Enter value of r: 3
Permutation=60.000 Combination= 10.000

Flowchart:
2) Write a program to find the sum of sine series correct up to 4 places after
decimal. Here the nth and n-1th term of the series are related bytn=tn-1*x2/[(2n-
1)*(2n-2)]
So, let t denotes as term of the series and the sum denotes the sum of the series
up to the term represented by t. Initialize t with x and sum with t repeatedly
update the value of t and sum the desired accuracy is obtained.

Algorithm:
For Calling function int main ( )
Step 1: Start
Step 2: Declare variables n, x. Step 3: Read the n,x.
Step 4: x= (x*3.14)/180
Step 5:, Call series(x,n) function. Step 6: Print sum of sine series.
Step 7: Stop.
For Called function float series(float x, float n)
Step 1: Declare variables i, sum, t.
Step 2: Assign i = 2 and sum = x and t = x.
Step 3: Check whether i <= n?
If true, calculate t = ((–1)*t*x* x)/ ((2*i–1)*(2*i–2)) and sum = sum + t and i = i + 1,
then again go to step 3.
If false, go to step 4.
Step 4: return sum.
Flowchart:
Source code
#include <stdio.h>
#include <stdlib.h>
#define pi 3.14
float series(float,float);
int main()
{
float x,n;
printf("Enter value of x: \n"); scanf("%f", &x);
printf("Enter value of n: \n");
scanf("%f", &n);
x=(x*pi)/180;
printf("sum of sin series=%.4f",series(x,n));
return 0;
}
float series(float x,float n)
{
float i,t,sum; t= x;
sum = x;
for(i=2;i<=n;i++)
{
t=(t*(-1)*x*x)/((2*i-1)*(2*i-2));
sum+=t;
}
return sum;
}

Output:
Enter value of x:
45
Enter value of n:
4
sum of sin series=0.7068
3) write a program to print pascal's triangle using Function:

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
Algorithms:

Algorithms for factorial function :


Step 1: declare function fact and
local variables n, i, factorial: 1.
Step 2: check n.
if n is lesser than or equals to1 then
Return 1
else
Repeat step 2.1 for i= 1 until i≤n .
Step 2.1: factorial ← factorial ×i.
Step 3: Return factorial.

→Algorithm for permutation function:


Steps1: Declare function perm and local variable n,r.
Step 2: Return fact (n)/ fact (n-r);

→Algorithm for combination function.


Step 1: Declare function comb andlocal variables
Step 2: Return perm (n,r) /fact (r)

→ Algorithm for main function :

Step 1: start.
Step 2: Declare variables i, j, k, n;
Step 3: Read value n
Step 4: Repeat steps up to I for i = 0 until i<n
Step 5: Repeat steps up to 6 for K = n will k>i
Step 6: Display a space (“ ”)
Step 7: Repeat steps up to 8 for j=0 until j≤i
Step 8: display the comb (i, j) with width management.
Step 9: display a new line
Step 10: stop

Source Code
#include <stdio.h>
int fact (int n)
{
int i,factorial = 1;
if (n<=1) return 1;
else
for (i=1;i<=n; i++)factorial *=i ;
return factorial;
}
int Perm (int n, int r) {
return fact (n)/ fact (n-r); }
int Comb (int n,int r) {
return Perm (n,r) / fact (r); }
int main ()
{
int i, j, k, n;
printf ("Enter the rows: \n");
scanf ("%d", &n);
for(i=0; i<n; i++);{
for (k=n;k>i;k--);
{
printf (" "); }
for(j=0;j<=1;j++);{
printf("%3d",Comb(i,j));j;}
printf("\n");
}
}

Flowchart:
4) Write a program to swap the values of two numbers using pass by reference
method.
Algorithm:
→Algorithm main Function: int main ( )
Step 1: Start
Step 2: Declare variables a, b.
Step 3: Read a and b.
Step 4: Print before swap a and b.
Step 5: Call swap(&a, &b)
Step 6: Print after swap a and b.
Step 7: Stop.
→Algorithm for var-swap: int swap(int *p, int *q)
Step 1: Declare variables a as int.
Step 2 Assign: a =*p
*p=*q
*q=a
Step 3: return.
Flowchart:

Source code:

#include <stdio.h>
#include <stdlib.h>
int swap(int *, int *);
int main()
{
int a, b;
printf("Enter the values of a and b:\n");
scanf("%d%d", &a, &b);
printf("Before swap \n a = %d \n b= %d\n", a, b);
swap(&a, &b);
printf("After swap \n a = %d \n b = %d", a, b);
return 0;
}
int swap(int *p, int *q)
{
int a;
a = *p;
*p = *q;
*q = a;
}
Output:
Enter the values of a and b: 6 9
Before swap
a=6
b= 9
After swap
a=9
b=6

Discussion :

In this lab session, we learned about the functions and how to create one, how to
use one, how to call one. Basically, functions. are a piece of code segment that
helps to reduce effort for writing multiple algorithm to perform same set of action
with different value. Using the permutation knowledge of functions, we solve 4
problems.
The first problem was to find the permutation and combination for gives two
numbers. For that we used 3 functions for factorial, permutation and
combination. For permutation factorial function was called. For combination both
permutation and factorial function was called .Using functions, we reduced effort
for writing same actions for multiple problems.
Second problem was to find the sum of sine series correct up to 4 places after
decimal. Here we use different function to solve the problem.
Third problem was to display pascal's triangle. For that we used factorial,
permutation, Combination function to calculate the numbers in triangle.
Fourth problem was to swap variables. Basically, we can pass arguments by two
methods, by value and by reference. For swapping we need to use reference
method and swapped using after function.
Conclusion:
Hence, we became familiar with functions and passing arguments and also using
them recursively. Now, we can use function in our code to increase efficiency
modularity. This session acknowledged us with the increases. Types of function
and to solve problems using find them.

You might also like