![Page 1: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/1.jpg)
1
Today:
FunctionsFunctions
![Page 2: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/2.jpg)
2
Some General Tips on Programming
Write your code modularly Compile + test functionality in
the process
![Page 3: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/3.jpg)
3
Some General Tips on Programming (Cont.)
Use Debugger or printf to follow your execution flow and find what went wrong
Understanding is good but not enough – you must practice!
![Page 4: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/4.jpg)
4
Functions
a group of declarations and statements that is assigned a name effectively, a named statement block usually has a value
a sub-program when we write our program we always define
a function named main inside main we can call other functions
which can themselves use other functions, and so on…
![Page 5: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/5.jpg)
5
What are They Good For? generalize a repeated set of instructions
we don’t have to keep writing the same thing over and over
they can break your problem down into smaller sub-tasks easier to solve complex problems
they make a program much easier to read and maintain abstraction – we don’t have to know how a
function is implemented to use it
![Page 6: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/6.jpg)
6
Example - Square #include <stdio.h>
double square(double a) { return a*a;}
int main(void) { double num;
printf("enter a number\n"); scanf("%lf",&num);
printf("square of %g is %g\n",num,square(num));
return 0;}
This is a function defined outside main
Here is where we call the function square
![Page 7: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/7.jpg)
7
Functions Templatereturn-type name(arg_type1 arg_name1, arg_type2 arg_name2,
…){
function body;return value;
}
double square(double a){
return a*a;}
int main(void){
…}
![Page 8: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/8.jpg)
8
Return Statement Return causes the execution of the function
to terminate and usually returns a value to the calling function
The type of the value returned must be the same as the return-type defined for the function (or a ‘lower’ type)
If no value is to be returned, the return-type of the function should be set to ‘void’
![Page 9: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/9.jpg)
9
Example: Factorial#include <stdio.h>
int factorial(int n){
int i, fact = 1;
for (i=2; i<=n; i++) fact *= i;
return fact;}
int main(void){ int num;
printf("enter a number\n"); scanf("%d",&num);
printf("%d!=%d\n",num,factorial(num));
return 0;}
![Page 10: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/10.jpg)
10
A Detailed ExampleWrite a program that receives a
nominator and a denominator from the user, and displays the reduced form of the number.
For example, if the input is 6 and 9, the program should display 2/3.
![Page 11: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/11.jpg)
11
Example – solution (step I)#include <stdio.h>
int main(void){
int n, d;
printf("Please enter nominator and denominator: ");scanf("%d%d", &n, &d);
Calculate n’s and d’s Greatest Common Divisor
printf("The reduced form of %d/%d is %d/%d", n, d, n/gcd, d/gcd);
return 0;}
![Page 12: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/12.jpg)
12
Example – solution (step II)#include <stdio.h>
int main(void){
int n, d, g;
printf("Please enter nominator and denominator: ");scanf("%d%d", &n, &d);
g = gcd(n, d);
printf("The reduced form of %d/%d is %d/%d", n, d, n/g, d/g);
return 0;}
![Page 13: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/13.jpg)
13
Example – solution (step III)/* Returns the greatest common divisor of
its two parameters. Assumes both are positive. */
int gcd(int x, int y){
int i;
for (i=x; i>0; i--)if (x%i == 0 && y%i ==0) return i;
}
![Page 14: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/14.jpg)
14
GCD – step by stepint main(void){
int n, d, g;
printf("Please enter … : ");scanf("%d%d", &n, &d);
g = gcd(n, d);
printf("The reduced form…", n, d, n/g, d/g);
return 0;}
n d g6 9 ---
![Page 15: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/15.jpg)
15
GCD – step by stepint main(void){
int n, d, g;
printf("Please enter … : ");scanf("%d%d", &n, &d);
g = gcd(n, d);
printf("The reduced form…", n, d, n/g, d/g);
return 0;}
n d g6 9 ---
![Page 16: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/16.jpg)
16
GCD – step by stepint gcd(int x, int y){
int i;
for (i=x; i>0; i--)if (x%i == 0 && y%i ==0) return i;
}
x y i6 9 ---
![Page 17: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/17.jpg)
17
GCD – step by stepint gcd(int x, int y){
int i;
for (i=x; i>0; i--)if (x%i == 0 && y%i ==0) return i;
}
x y i6 9 6
![Page 18: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/18.jpg)
18
GCD – step by stepint gcd(int x, int y){
int i;
for (i=x; i>0; i--)if (x%i == 0 && y%i ==0) return i;
}
x y i6 9 6
![Page 19: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/19.jpg)
19
GCD – step by stepint gcd(int x, int y){
int i;
for (i=x; i>0; i--)if (x%i == 0 && y%i ==0) return i;
}
x y i6 9 5
![Page 20: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/20.jpg)
20
GCD – step by stepint gcd(int x, int y){
int i;
for (i=x; i>0; i--)if (x%i == 0 && y%i ==0) return i;
}
x y i6 9 5
![Page 21: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/21.jpg)
21
GCD – step by stepint gcd(int x, int y){
int i;
for (i=x; i>0; i--)if (x%i == 0 && y%i ==0) return i;
}
x y i6 9 4
![Page 22: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/22.jpg)
22
GCD – step by stepint gcd(int x, int y){
int i;
for (i=x; i>0; i--)if (x%i == 0 && y%i ==0) return i;
}
x y i6 9 4
![Page 23: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/23.jpg)
23
GCD – step by stepint gcd(int x, int y){
int i;
for (i=x; i>0; i--)if (x%i == 0 && y%i ==0) return i;
}
x y i6 9 3
![Page 24: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/24.jpg)
24
GCD – step by stepint gcd(int x, int y){
int i;
for (i=x; i>0; i--)if (x%i == 0 && y%i ==0) return i;
}
x y i6 9 3
![Page 25: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/25.jpg)
25
GCD – step by stepint gcd(int x, int y){
int i;
for (i=x; i>0; i--)if (x%i == 0 && y%i ==0) return i;
}
x y i6 9 3
![Page 26: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/26.jpg)
26
GCD – step by stepint main(void){
int n, d, g;
printf("Please enter … : ");scanf("%d%d", &n, &d);
g = gcd(n, d);
printf("The reduced form…", n, d, n/g, d/g);
return 0;}
n d g6 9 3
![Page 27: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/27.jpg)
27
Example – Complete Solution
gcd.c
![Page 28: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/28.jpg)
28
Exercise: Fibonacci Input:
An integer n Output:
The n’th fibonacci number Note –
Use an appropriately defined function
![Page 29: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/29.jpg)
29
Solution
fibonacci_func.c
![Page 30: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/30.jpg)
30
ExampleWrite a program that gets a
positive integer from the user and prints all the prime numbers from 2 up to that integer.
(Use a function that returns 1 if its parameter is prime, 0 otherwise)
![Page 31: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/31.jpg)
31
Solution
is_prime_func.c
![Page 32: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/32.jpg)
32
Exercise Newton was the first to notice that for
any positive n, and when x0=1, the following series converges to sqrt(n) –
Use this fact to write a program that accepts a positive number and outputs its square root Hint – the thousandth element of
Newton’s series is a good-enough approximation
![Page 33: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/33.jpg)
33
Solution
sqrt.c
![Page 34: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/34.jpg)
34
Void When there’s no reason for a function to
return a value the function’s return type should be ‘void’
Calling ‘return’ in a function returning void is not obligatory
If the ‘return’ keyword is used within such a function it exits the function immediately. No value needs be specified
If the function receives no parameters, the parameter list could be replaced by ‘void’
![Page 35: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/35.jpg)
35
Example: Show Helpvoid ShowHelp(void){
printf("This function explains what this program does…\n");printf("Yadayadayada");
/* …. */}
int main(void){
char choice;
printf("Please enter your selection: ");scanf("%c", &choice);
if (choice==‘h’) ShowHelp();else if /* Program continues … */
}
![Page 36: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/36.jpg)
36
Pass-By-Value Function arguments are passed to the
function by copyingcopying their values rather than giving the function direct access to the actual variables
A change to the value of an argument in a function body will not changewill not change the value of variables in the calling function
Example – add_one.c
![Page 37: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/37.jpg)
37
add_one – step by stepint add_one(int b){
b=b+1;return b;
}
int main(void){
int a=34,b=1;
a=add_one(b);
printf("a = %d, b = %d\n", a, b);return 0;
}
a b34 1
Main() memory state
![Page 38: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/38.jpg)
38
add_one – step by stepint add_one(int b){
b=b+1;return b;
}
int main(void){
int a=34,b=1;
a=add_one(b);
printf("a = %d, b = %d\n", a, b);return 0;
}
a b34 1
Main() memory state
![Page 39: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/39.jpg)
39
add_one – step by stepint add_one(int b){
b=b+1;return b;
}
int main(void){
int a=34,b=1;
a=add_one(b);
printf("a = %d, b = %d\n", a, b);return 0;
}
a b34 1
Main() memory state
b1
add_one memory state
![Page 40: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/40.jpg)
40
a b34 1
Main() memory state
add_one – step by stepint add_one(int b){
b=b+1;return b;
}
int main(void){
int a=34,b=1;
a=add_one(b);
printf("a = %d, b = %d\n", a, b);return 0;
}
b2
add_one memory state
![Page 41: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/41.jpg)
41
a b34 1
Main() memory state
add_one – step by stepint add_one(int b){
b=b+1;return b;
}
int main(void){
int a=34,b=1;
a=add_one(b);
printf("a = %d, b = %d\n", a, b);return 0;
}
b2
add_one memory state
![Page 42: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/42.jpg)
42
add_one – step by stepint add_one(int b){
b=b+1;return b;
}
int main(void){
int a=34,b=1;
a=add_one(b);
printf("a = %d, b = %d\n", a, b);return 0;
}
a b2 1
Main() memory state
![Page 43: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/43.jpg)
43
add_one – step by stepint add_one(int b){
b=b+1;return b;
}
int main(void){
int a=34,b=1;
a=add_one(b);
printf("a = %d, b = %d\n", a, b);return 0;
}
a b2 1
Main() memory state
![Page 44: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/44.jpg)
44
What will be Printed Here?#include <stdio.h>
int factorial(int n){
int fact = 1;
while (n>1) { fact *= n; n--; }
return fact;}
int main(void){ int n;
printf("enter a number\n"); scanf("%d",&n);
printf("%d!=%d\n", n, factorial(n));
/* What will this print? */ printf("n = %d\n", n); return 0;}
![Page 45: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/45.jpg)
45
Scope of Variables
A variable declared within a function is unrelated to variables declared elsewhere (even if they share the same name)
A function cannot access variables that are declared in other functions
Example – scope.c
![Page 46: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/46.jpg)
46
Example: Error in Scopeint add_one(int b){
a=b+1;return a;
}
int main(void){
int a=34,b=1;
add_one(b);
printf("a = %d, b = %d\n", a, b);return 0;
}
![Page 47: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/47.jpg)
47
Function Declaration Most software projects in C are composed of
more than one file We want to be able to define the function in
one file, and to use it in all files For this reason, the function must be
declareddeclared in every file in which it’s called, before it’s called for the first time
the declaration contains: the function name the data types of the arguments (their names are
optional) the data type of the return value
![Page 48: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/48.jpg)
48
Function Declaration#include <stdio.h>
int factorial(int a); /* Function Declaration! */
int main(void){ int num;
printf("enter a number\n"); scanf("%d",&num);
printf("%d!=%d\n",num,factorial(num));
return 0;}
int factorial(int a){ int i,b=1; for(i=1; i<=a; i++) b=b*i; return b;}
![Page 49: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/49.jpg)
49
Function Declaration stdio.h actually contains a large
set of function declarations The #include directive tells the
compiler to insert these declarations into the file, so that these functions could be called
![Page 50: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/50.jpg)
50
The math Library A collection of mathematical functions Need to include the header file math.h
(#include <math.h>) Use functions of the library, e.g. double s,p; s=sqrt(p); Declared in math.h : double sqrt ( double x ); Content: Google: math C
![Page 51: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/51.jpg)
51
The math Library: Content
sin(x), cos(x), tan(x) x is given in radians
asin(x), acos(x), atan(x) log(x) sqrt(x) pow(x,y) – raise x to the yth power. ceil(x), floor(x) …and more
![Page 52: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/52.jpg)
52
Exercise (@home)
Write a function that uses the formula
2/6=1/1+1/4+1/9+1/16+…+1/n2 (where n goes to infinity)
in order to approximate . The function should accept an argument n which determines the number of terms in the formula. It should return the approximation of .
Write a program that gets an integer n from the user, and approximate using n terms of the above formula.
![Page 53: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/53.jpg)
53
Solution
pi.c
![Page 54: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/54.jpg)
54
Exercise (@home)
Modify the previous function that approximates . The function should accept an argument specifying the desired accuracy, and keep adding terms until the contribution of the next term drops below this level.
Write a program that gets a (small) double, epsilon, from the user, and approximates within this function.
![Page 55: 1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality…](https://reader035.vdocuments.us/reader035/viewer/2022062911/5a4d1bf77f8b9ab0599e9626/html5/thumbnails/55.jpg)
55
Solution
pi_eps.c