Professional Documents
Culture Documents
Function
OBJECTIVES
Differentiate
STORAGE CLASSES
There are two different ways to characterize variables
By data type refers to the type of information represented by a
variable
By storage class refers to the permanence of a variable andits
scope within the program, that is, the portion of the program
over which the variable is recognized.
There are four storage-class specifications in C
1. Automatic variables or Auto
2. External variables or Extern
3. Static variables
4. Registers
AUTOMATIC VARIABLES
Automatic variables are always declared within a function
and are local to the function in which they are declared; that is
their scope is confined to that function. Automatic variables
defined in different functions will therefore be independent of
another, even though they may have the same name.
Any variable declared within a function is interpreted as an
automatic variable unless a different storage class specification
is included within the declaration.
Since the location of the variable declarations within the
program determine the automatic storage class, the keyword AUTO
is NOT required at the beginning of each variable declaration.
There is no harm in including an auto specification within a
logic
#include<stdio.h>
#include<conio.h>
int a =3;
main()
{ clrscr();
int funct1( int count);
for(count = 1; count <= 5; ++count)
{ a = finct1(count);
printf(%d,a);
}
OUTPUT
getch();
4
6
return 1;
}
13
18
Seatwork: Logic trace the program and write the final output
inside the box.
#include<stdio.h>
#include<conio.h>
int a = 100, b = 200;
funct1(int x)
{ int c, d;
int funct2(int c);
c = funct2(x);
d = (c < 100) ? (a + c): b;
return(d);
}
funct2(int x)
{ static int prod = 1;
prod *= x;
return(prod);}
main()
{ int count;
int funct1(int count);
clrscr();
for(count = 1; count < = 5; ++count)
printf(%d, funct1(count));
getch();
return 1;
}
FUNCTION IN C
The
heart
decomposition.
of
effective
Taking
problem
problem
and
solving
breaking
is
it
problem
into
small,
function
construct
is
used
to
implement
this
program
which
top-down
method of programming.
A
function
is
section
of
perform
In
such
cases,
the
same
function
can
be
used
3.
Using
function
provides
natural
method
for
dividing
FUNCTION DECLARATIONS
The function declaration has a name of a function, the type
of the value to be returned (if there are any) and the number and
types of the arguments that must be supplied in a call of the
function. A function declaration may contain argument names.
syntax:
type function_name (parameter list)
Example
int ccmit (int bsit, int bscs);
void ccmit ();
float ccmit (float x, float y);
FUNCTION DEFINITION
The code that describes what a function does is called a
function definition. It must not be confused with the function
declaration.
general form:
The
function
definition
must
have
the
following
declaraction
local variables
statement;
}
Everything before the first brace comprises the header of
the
function
definition,
and
everything
between
the
braces
role
as
placeholders,
these
parameters
are
called
the
/*function definition*/
/*function definition*/
remember
that
block
of
code
is
begun
when
You
an
Sample Program
#include<stdio.h>
#include<conio.h>
int a=33
main()
{
int b = 77;
/*b is local variable to main()*/
printf(a = %d\n,a); /*a is global variable to main()*/
printf(b=%d\n,b);
return 0;
getch();
}
function.
They
maybe
accessed
by
any
expression
You
should
avoid
using
unnecessary
global
variables,
take-up
memory
the
entire
time
your
program
is
error
because
of
unknown
and
unwanted,
side
The
return(expression);
expression
being
returned
can
be
enclosed
in
passed
statement
back
to
contains
the
an
calling
environment.
expression,
then
the
If
the
value
return
of
the
FUNCTION PROTOTYPES
type
of
the
value
that
is
to
be
returned
by
the
function.
Example
double sqrt(double);
this tells the compiler that sqrt() is a function that takes a
single argument of type double and returns a double.
Syntax:
type function_name(parameter type list);
to void funct1(char,
variable.
In
particular,
you
can
assign
to
FORMAL
are
list
of
invoked
by
arguments
writing
within
their
names
parenthesis.
and
an
Typically,
to
manipulate.
However,
the
manipulation
Sample Program
#include<stdio.h>
#include<conio.h>
int sum;
/*global declaration*/
void funct_sample ( int y);
void main()
{
int n =5; clrscr();
print(The value of n here is %d,n);
funct_sample(n);
printf(\nValue of n is %d,n);
getch();
}
funct_sample( int y)
{
does
not
the
value
of
in
the
calling
environment
remains
call by reference
be
used
in
the
parameter
list
in
the
function
Example
Address of the parameter value of parameter
16825
*50126
address of parameter is
being passed to the function
Sample program 1
#include<stdio.h>
#include<conio.h>
compute_rating(float midterm, float final, float *rating)
{
/* Function that will compute the final rating */
*rating = (midterm + final)/2;
}
main()
{
char name[25];
float mid,fin,fin_grd;
clrscr();
puts(Please enter you name);
gets(name);
printf(Enter your midterm grade);
scanf(%f,&mid);
printf(\nEnter you final grade);
scanf(%f,&fin);
compute_rating(mid,fin, &fin_grd);
printf(%s got a final rating of %f, name,fin_grd);
getch();
return 0;
}
Sample Program 2
void swap_ref(int &a,int &b);
void swap_val(int a,int b);
int main()
{
int a=3,b=6;
printf(\\na=%d
swap_val(a,b);
b=%d,a,b);
printf(\\na=%d b=%d,a,b);
swap_ref(&a,&b);
printf(\\n a=%d b=%d,a,b);
return 1;
}
void swap_ref(int *a, int *&b)
{
//function accepts a reference
*a= *a + b;
//to original parameter variable
b= *a-b;
*a= *a-b;
}
void swap_val(int a, int b)
{
a=a+b;
b=a-b;
a=a-b;
}
OUTPUT:
a=3 b=6
a=3 b=6
a=6 b=3
Sample Program 3
Void
LESSON TWO
ARRAYS
OBJECTIVES
Introduction
ARRAY CONCEPTS
Imagine we have a problem that requires 20 integers to be
processed. We need to read them, process them, and print them.
We must also keep these 20 integers in memory for the duration of
the program. We can declare and define 20 variables, each with a
different name, as shown in the figure below.
Numbers0
Numbers1
Numbers2
Numbers19
Illustration:
Twenty variables
Characteristics of an Array
An array is a fixed-size, sequenced collection of elements of
the same data type.
An array is a sequence of data items that are of the same type,
that are indexible, and that are stored contiguously.
Arrays are data type that is used to represent a large number
of homogenous values.
Since an array is a sequenced collection, we can refer to
the elements in the array as the first element, the second
element, and so forth until we get to the last element. If we
were to put our twenty numbers into an array, we could designate
the first element as shown below.
Numbers0
In similar fashion, we could refer to the second number as
numbers1, and the third number as numbers2. Continuing the
series, the last number would be numbers19.
Process twenty variables
Numbers0 numbers1, , numbersn-1
What we have seen is that the elements of the array are
individually addressed through their subscripts. This concept is
graphically shown in the next figure to be presented. The array
as a whole has a name, numbers, but each member can be accessed
individually using its subscript.
we didnt
to process
powerful set
processing
Number0
Number[0]
Number1
Number[1]
Number18
Number[18]
Number19
Number[19]
Numbers
a. Subscript form
Numbers
b. Index form
An array of numbers
We can use loops to read and write the elements in an array.
We can use loops to add, subtract, multiply, and divide the
elements. We can also use loops for more complex processing such
as calculating averages.
But one question still remains. How can we write an
instruction so that at one time it refers to the first element of
an array, and the next time it refers to another element? It is
really quite simple: we simply borrow from the subscript concept
we have been using.
Rather than using subscripts, however, we will place the
subscript value in square brackets. Using this notation, we
would refer to number0 as:
numbers[ 0 ].
#include<stdio.h>
#include<conio.h>
int scores[20], i, sum=0;
void main
{
clrscr();
printf(please enter 20 scores:\n);
for(i=0; i<20; i++)
{
scanf(%d,&scores[ i ]);
sum=sum + scores[ i ];
}
getch();
}
array declaration
y[0] = 1.0
x[1] = 2
y[1] = 1.25
x[2] = 3
y[2] = 1.5
x[3] = 0
y[3] = 0
y[4] = 0
Example
Consider the character array definition. It includes an initial
assignment of the string constant CCMIT.
char college[6] = CCMIT;
for(i = 0;i<10,i++)
scanf(%d,scores[i]);
Several concepts need to be studied in this simple
statement. First, we start the index, i, at zero. Since there
are ten elements in the array, we must load the values from index
locations zero through nine. The limit test, therefore, is set
at i < 10, which conveniently is the number of elements in the
array. Then, even though we are dealing with array elements, the
address operator (&) is still necessary in the scanf call.
Finally,
are not going
(while or do
depend on the
scores [ i ] = i * 2;
Sample program 1
This is a program that sorts the values of the array num.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
in num[3] = {5, 3, 7};
initialization
int h, I, temp;
for(h=0;h<3;++h)
for(i=0;i<h;++i)
{
if (nm[i] > num[i+1])
{
temp = num[i];
num[i] = num [i+1];
num[i+1] = temp;
}
}
for(i=0;i<3;i++)
printf(%d\n, num[i]);
getch();
}
sample run:
Output:
3
5
7
Sample program 2
#include<conio.h>
void main( )
{
int a[50],n,count_neg=0,count_pos=0,I;
clrscr();
printf(Enter the size of the arrayn);
scanf(%d,&n);
printf(Enter the elements of the arrayn);
for I=0;I < n;I++)
scanf(%d,&a[I]);
for(I=0;I < n;I++)
{
if(a[I] < 0)
count_neg++;
else
count_pos++;
}
printf(There are %d negative numbers in the array\n,count_neg);
printf(There are %d positive numbers in the array\n,count_pos);
getch();
return;
}
Multidimensional Arrays
Multidimensional arrays are defined in much the same manner
as one-dimensional. A two-dimensional array requires two pairs of
square brackets.
In general term, a multidimensional array definition can be
written as
storage-class data-type array[expression1][expression2];
where storage-class refers to the storage class of the array,
data-type is its data-type, array is the array name and
expression1, expression2 are positive-values integer expression
that indicate the number of array elements associated with each
subscript. Remember that storage-class is optional; the default
are automatic for arrays that are defined inside of a function
and external for arrays, defined outside of a function.
Several multidimensional array definition are shown below
float IT[5][3];
int CS [3][3];
braces are assigned to the array elements in the first row, the
values in the second inner part of braces are assigned to the
array elements in the second row, and so on. Note that an outer
pair of braces is required.
{ 1,2,3},{4,5,6},{7,8,9}};
=
=
=
=
=
=
1
0
4
0
7
0
ccmit[0][1] = 2
ccmit[0][2] = 3
ccmit[1][1] = 5
ccmit[1][2] = 6
ccmit[2][1] = 8
ccmit[2][2] = 9
ccmit[0][0]
ccmit[0][3]
ccmit[1][0]
ccmit[1][3]
ccmit[2][0]
ccmit[2][3]
=
=
=
=
=
=
1
4
5
8
9
0
ccmit[0][1] = 2
ccmit[0][2] = 3
ccmit[1][1] = 6
ccmit[1][2] = 7
ccmit[2][1] = 0
ccmit[2][2] = 0
Sample program 3
/* example program to add two matrices & store the results
in the 3rd matrix */
#include< stdio.h >
#include< conio.h >
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q;
clrscr();
printf(enter the order of the matrix\n);
scanf(%d%d,&p,&q);
if(m==p && n==q)
{
printf(matrix can be added/n);
printf(enter the elements of the matrix a);
for(i=0;i < m;i++)
for(j=0;j < n;j++)
scanf(%d,&a[i][j]);
printf(enter the elements of the matrix b);
for(i=0;i < p;i++)
for(j=0;j < q;j++)
scanf(%d,&b[i][j]);
printf(the sum of the matrix a and b is);
for(i=0;i < m;i++)
for(j=0;j < n;j++)
c[i][j]=a[i][j]+b[i][j];