Professional Documents
Culture Documents
Lab 4
Lab 4
Lab 4
Lab report 4
Roll no : 21
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;
}
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.
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
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:
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.