Lesson 7 of C Programming
Lesson 7 of C Programming
Lesson 7 of C Programming
Function
Learning Objectives:
At the end of the chapter, the students are expected to:
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
top-down
method of programming.
A
function
is
section
of
program
which
performs
In
such
cases,
the
same
function
can
be
used
repeatedly.
3.
Using
function
provides
natural
method
for
dividing
syntax:
type function_name (parameter list)
Example
int ccmit (int bsit, int bscs);
void ccmit ();
float ccmit (float x, float y);
The
function
definition
must
general form:
type function_name (parameter list)
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
Example
double twice(double x)
{
return(2.0*x)
}
/*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
effects.
1.2.3 THE return STATEMENT
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
Example
float f(char a, char b, char c)
{
int i;
:
:
return i;
/*the value returned will be converted to a
float*/
}
type
of
the
value
that
is
to
be
returned
by
the
function.
Example
double sqrt(double);
Syntax:
type function_name(parameter type list);
to void funct1(char,
int);
Example
void funct1()
program
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
does
not
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)
{
/* Sample function using call by value */
y*= 3;
printf(The new value of y is %d, y);
OUTPUT:
The value of n here is 5
The new value of y is 15
Value of n here is 5
Even though n is passed to funct_sample() and received by formal
parameter y, the value n which y in the body of that function is
changed,
the
value
of
in
the
calling
environment
remains
call by reference
be
used
in
the
parameter
list
in
the
function
Illustration
Address of the parameter value of parameter
16825
*50126
address of parameter is
being passed to the function
Sample program
#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();
putsf(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;
}
EXERCISE NO. 4
NAME:_________________________________ DATE: ____________
YR. & SEC. ___________________________ SCORE:____________
A.
1. What is a function? What is the syntax of a function?
___________________________________________________________
2. What is the use of the data type void?
___________________________________________________________
3. Explain a function that is called by value?
___________________________________________________________
4. What is an address? A pointer? Explain a function that is
called by reference?
___________________________________________________________
___________________________________________________________
___________________________________________________________
5. Explain a function that returns a value?
___________________________________________________________
___________________________________________________________
___________________________________________________________
B. TRACING
1. Trace the following programs:
(a)
void trace1(int x, int y)
{
X = 5; *y =2;
printf(%2d %2d\n, x, *y);
}
main( )
{
int x, y;
clrscr( );
x = y = 3;
trace1(x, &y);
printf(%2d %2d\n, x, y);
getch( );
return 0;
}
(b)
void trace(int x, int *y, int z)
{
x = 1; *y=2;z=4;
printf("%2d %2d %2d\n",x, *y, z);
}
main()
{
int x=1, y=3,z=4;
clrscr();
printf("%2d %2d %2d\n",x,y,z);
trace(y,&x,z);
printf("%2d %2d %2d\n",x,y,z);
trace(x,&z,y);
printf("%2d %2d %2d\n",x,y,z);
trace(z,&y,x);
printf("%2d %2d %2d\n",x,y,z);
getch();
return 0;
}
(c)
#include<stdio.h>
#include<conio.h>
void kar1(char *c, char b, char *a)
{
*a = 'c'; b = 'a'; *c = 'b';
printf("%c %c %c\n", *a, b, *c);
}
void kar2(char *b, char *a, char *c)
{
*a = 'b'; *b='c'; *c ='a';
printf("%c %c %c\n", *a, *b, *c);
}
main()
{
char a = 'a', b = 'b', c = 'c';
clrscr();
printf("%c %c %c\n", a, b, c);
kar1(&a,b,&c);
printf("%c %c %c\n", a, b, c);
kar2(&a,&b,&c);
defined a 1.
and would call the function prime that returns 0 for true and 1
for
false.
(Precondition:
Test
if
the
entered
value
is
nonnegative)
PROGRAMMING EXERCISES 4-3
Write a function int is_prime(int n) that returns 1 if n is prime
and 0 otherwise.
Hint: if k and n are positive integer, then k divides n if and
only if n % k has value 0.
PROGRAMMING EXERCISES 4-4
A famous conjecture, called the GOLDBACH conjecture, says that
every even integer n greater than 2 has the property tat it is
the
sum
of
two
prime
numbers.
Computers
have
been
used
found. Write a program the will prove that the conjecture is true
for all the even integers between the symbolic constants START
and FINISH.
+ 683
+ 691
+ 701
+ 1093
+ 1097
is DEFICIENT
Input N : 6
Proper divisors are 1,2 3
Sum of proper divisors: 1 + 2 + 3 = 6
6 < 6
is PERFECT
Input N : 12
Proper divisors are 1,2, 3, 4,6
Sum of proper divisors: 1 + 2 + 3 + 4 + 6 = 16
16 > 12
is ABUNDANT
CASE STUDY 2
Write a program to call function TRIANGLE to determine if a given
sides
is
equilateral,
EQUILATERAL
if
all
isosceles
the
three
or
sides
scalene.
have
the
triangle
same
is
length.
ISOSCELES if only two sides have the same length and SCALENE if
Military Time
from
24-hour
notation
to
12-hour
notation
using
function.
Input Specifications
The input must be a single integer ranging from 0 to 2400.
Any other value must result into an input error which the program
should display as a message to the user before it halts program
execution.
Output Specifications
Output the time in 12-hour notation using the following
format: 1:00 PM using a colon to separate the hour part from the
minute part and adding the abbreviations AM or PM to indicate
what part of the day it is. Note that you must observe the 2digit display.