You are on page 1of 11

Functions

Functions
 Why functions?
 Break longer jobs into conceptually smaller jobs which are precisely
defined.
 C program generally consists of many small functions.
 A piece of a code which repeats at many places can be written only once
and used again and again.
 Debugging and maintenance is easy.

Lectures on Numerical Methods 1


Functions:
Functions: Example
Example
#include
#include <limits.h>
 Sine Function <limits.h>

 Sine function is included in the double


double sin(double
sin(double x) x)
{{
standard library and is declared double
double sinValue
sinValue == 0.0;
0.0;
in math.h double xSquared;
double xSquared;
 The example here uses Taylor double
double term;
term;
series expansion. The int
int n = 1;
n = 1;
successive terms are added till xSquared
xSquared == xx ** x;
x;
the term becomes smaller than term = x;
term = x;
the machine epsilon.
while
while (( term
term >> DBL_EPSILON
DBL_EPSILON )) {{
sinValue
sinValue +=+= term;
term;
nn += 2;
+= 2;
term
term *=
*= (-
(- xSquared)/(n
xSquared)/(n -- 1)/n;
1)/n;
}}

return
return sinValue;
sinValue;
}}

Lectures on Numerical Methods 2


Functions:
Functions: Example
Example
#include
#include <stdio.h>
 Prime Numbers <stdio.h>

 Print a table as follows main()


main()
{{
1* 2* 3* 4 5* … 10 int
int i;
i;
11* 12 13* 14 15 … 20
for(
for( ii == 1;1; ii <=
<= 100;
100; i++
i++ )) {{
upto 100. All primes are prinf(“%d”,
prinf(“%d”, i); i);
starred. if
if (( isPrime(i)
isPrime(i) ))
 The main function prints the printf(“*”);
printf(“*”);
nice looking table. And it is not if
if (( ii %% 10
10 ==
== 00 ))
about finding primes. printf(“\n”);
printf(“\n”);
 The functinon isPrime(n) is else
else
expected to return 1(true) if n is printf(“\t”);
printf(“\t”);
}}
a prime and 0(false) otherwise.
}}

Lectures on Numerical Methods 3


Functions:
Functions: Example
Example
int
int isPrime(int
isPrime(int num)
 Prime Numbers {{
num)

 A rather simple implementation int i;


int i;
of isPrime function is given
here. if
if (( num
num << 33 )) return
return 1;
1;
 The function has same structure
as main function we saw before, for
for (i=2;
(i=2; i<=(int)
i<=(int) sqrt(num);i+
sqrt(num);i+
except that int before the name ++ ))
if
if (( num
num %% ii ==
== 00 )) break;
of the function and return break;
statement.
if
if (( num
num %% ii )) return
return 1;
1;

return
return 0;
0;
}}

Lectures on Numerical Methods 4


Functions
Functions
 The syntax of a function is
return-type function-name(argument-list)
{
Declarations and statements
}
 Smallest function is
emptyFunction() {}
 Rules for the function name are same as that of the variables.
 Return-type must be one of valid data-types or void. If it is not
mentioned then int is assumed. It is not mandatory to return a value.
The calling program is free to ignore the return value.
 The form of argument list is
type1 arg1, type2 arg2,..., typen argn
 it is understood that the arguments have been declared.

Lectures on Numerical Methods 5


Arguments
Arguments
 The term argument (actual argument, actual parameter) is used for the
variables that are passed to the function by a calling program. The term
parameter is used for the variables received by the function as described
in the function declaration.
 The arguments are passed by value. This means a separate copy of the
variable is passed to the function. The values of the variables in the
calling program are not affected by the changes to the parameters in the
function definition.
 The types of the arguments passed and the parameters declared must
match. If they don’t match, automatic type casting is applied. If these still
donnot match, there is an error.

Lectures on Numerical Methods 6


Arguments
Arguments
 This program will output #include
#include <stdio.h>
<stdio.h>
three lines:
4 newI
newI (int
(int I)
I) {{
II == 5;
5;
5
printf(“%d\n”,
printf(“%d\n”, I);
I);
4 }}

main()
main() {{
int
int I;I;

II == 4;
4;
printf(“%d\n”,
printf(“%d\n”, I);
I);

newI(I);
newI(I);
printf(“%d\n”,
printf(“%d\n”, I);
I);

}}

Lectures on Numerical Methods 7


Arrays
Arrays
/*
/* Evaluating
Evaluating aa polynomial
polynomial */
 Polynomials #include <stdio.h>
#include <stdio.h>
*/

 Read the coefficients of a


main()
main() {{
polynomial and print a int
table of values for all x int i,
i, order;
order;
float
float coef[10], x,
coef[10], x, val;
val;
= 0.1, 0.2,…,1.0.
printf(
printf( “Enter
“Enter thethe order
order ->
-> ““ );
);
scanf(“%d” , &order);
scanf(“%d” , &order);
for
for (( ii == 0;
0; ii <=
<= order;
order; i++
i++ ){
){
prinf(“a[%d] = “,
prinf(“a[%d] = “, i); i);
scanf(“%f”,
scanf(“%f”, &(coef[i])
&(coef[i]) ); );
}}

for
for (( xx == 0;
0; xx <=
<= 1;
1; xx +=
+= 0.1
0.1 ){
){
val = coef[0];
val = coef[0];
for
for (( ii == 1;
1; ii <=
<= order;
order; i++i++ ))
val
val +=
+= coef[i]
coef[i] ** pow(x,i);
pow(x,i);
prinf(“%f\t%f“,x,val);
prinf(“%f\t%f“,x,val);
}}
}}

Lectures on Numerical Methods 8


Arrays
Arrays
 An array declarationhas a form
Type array-name[constant-expression];
 Examples
int rollNos[100];
float marks[MAX*3];
 Arrays in C are indexed from 0.
 The elements of an array can be accessed by
array-name[int-expression]

Lectures on Numerical Methods 9


Sorting
Sorting Algorithms
Algorithms
 Insertion Sort
 An array is to be sorted in descending order.

1. Read an array a[n]


2. J = 1
3. Let k be such that a[k] = max{a[l]: j <= l < n }
4. Swap a[k] and a[j].
5. Increment j and if j < n-2 then go to step 3
6. Print sorted array a[n]

Lectures on Numerical Methods 10


Sorting
Sorting Algorithms
Algorithms
 BubbleSort
 An array is to be sorted in descending order.

1. Read an array a[n]


2. For j = 0 to n–2
3. For i = 0 to n – 1 - j
4. If a[I] < a[I+1] swap a[I] and a[I+1]
5. Print sorted array a[n]

Lectures on Numerical Methods 11

You might also like