Assignment # 03: " Functions "

You might also like

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

Computer Fundamentals

Assignment # 03
“ Functions ”

Submitted By : Abd-ur-Rehman Shamsi


Roll # : 02
Department : BSME 18-22
1. State three advantages to the use of functions.
Following are the advantages of using function in c programming.
1. Avoid repetition of codes.
2. Divide a complex problem into simpler ones.
3. Modifying a program becomes easier by using function.

2. What are arguments? What is their purpose? What


other term is sometimes used for an argument?
An argument of a function also called an independent variable is a
value that must be provided to obtain the function's result.

3. What are differences between passing an array to a


function and passing a single-valued data item to a
function?
Arguments are passed to a function by value when the arguments are
ordinary variables. While array arguments are passed by reference
rather than by value.

4. Each of the following is the first line of a function


definition. Explain the meaning of each.

(a) float f (float a, float b);


Function ‘f’ has two arguments of type float and return a value of type
float.

(b) long f (long a);


Function ‘f’ takes one long number in the input and return a long
number.

(c) void f(int a);


Function ‘f’ takes one integer in input and return nothing.

(d) char f (void);


Function ‘f’ takes nothing in input and return a character.

5. Write the first line of the function definition, including


the formal argument declarations, for each of the
situations described below.

(a) A function called sample generates and returns an


integer quantity.

int sample (int x);

(b) A function called root accepts two integer arguments


and returns a floating-point result.

float root (int x,int y);

(c) A function called convert accepts a character and


returns another character.
char convert (char);

6. Describe the output generated by each of the following


programs.

(a)

#include <stdio.h>
int funct1(int count);
main( )

int a, count;

for (count = 1; count <= 5; ++count)

a = funct1(count);
printf("%d", a);

}
}

int funct1(int x)

int y;
y=x*x;
return(y);

}
(b)

#include <stdio.h>
int funct1(int n);
main()

int n = 10;

printf (" %d " , funct1(n) );

int funct1(int n)

if (n > 0)

return(n + funct1(n - 1));

Answer:

(a)
Program display the squares of first five non-zero positive integers.

(b)
Give sum of all positive integers less than or equal to 10.
7. Express each of the following algebraic formulas in a
recursive form.

(a) y = (𝑥1 + 𝑥2 + ⋯ + 𝑥𝑛)

(b) y = 1 – x + x2/2 - x3/6 + x4/24 - …+(-1)nxn/n!


8. Write a function that will calculate and display the real
roots of the quadratic equation ax2+ bx + c = 0 using
the quadratic formula. Assume that a, b and c are
floating-point arguments whose values are given, and
that xl and x2 are floating-point variables.

Code:

#include
<stdio.h>
#include
<math.h>
float solve1(float , float , float
); float solve2(float , float ,
float ); int main()
{
float a, b, c, d, root1, root2;
printf("Enter the value of a: ");
scanf("%f",&a);
printf("\nEnter the value of b: ");
scanf("%f",&b);
printf("\nEnter the value of c: ");
scanf("%f",&c);
d = b*b -
4*a*c;
if(d>=0)
{
root1 = solve1(a, b,
c); root2 = solve2(a,
b, c);
printf("\nSolution is X1 = %f, X2 = %f" , root1,root2);
}
else
{
printf("\nRoots are complex");
}
return 0;
}
float solve1(float a, float b, float c)
{
float root1;
root1 = ( -b + sqrt((b * b)-( 4 * a * c))) / (2*
a); return root1;
}
float solve2(float a, float b, float c)
{
float root2;
root2 = ( -b - sqrt((b * b)-( 4 * a * c))) / (2*
a); return root2;
}

Output:

You might also like