Anda di halaman 1dari 8

1) #include<stdio.

h>
#include<conio.h>
main()
{clrscr();
int n;
printf("enter num");
scanf("%d",&n);
while (n > 0)
{
int remn%!;
n n"!;
printf("%d#n",rem);
$
printf("%&'() *& +,-%)& .&/0 1/((/0 +,2 3)( *& 1',+&4 5/2)");
3etch();
$c
2) #include <iostream>
3) using namespace std;
4) int main()
5) {
6) long dec,rem,i=1,sum=;
!) cout<<"#nter t$e decimal to %e con&erted'";
() cin>>dec;
)) do
1) {
11) rem=dec*2;
12) sum=sum + (i,rem);
13) dec=dec-2;
14) i=i,1;
15) ./$ile(dec>);
16) cout<<"0$e %inar1 o2 t$e gi&en num%er is'"<<sum<<endl;
1!) cin3get();
1() cin3get();
1)) return ;
2) .
21)
CHAPTER 3
FUNCTIONS AND MODULAR PROGRAMMING
How to write a function:
A function in C contains the following parts:
t!"e of t#e re$u%t&
function na'e& ( %i$t of in"ut "ara'eter$ )
*
re$er+e an! +aria,%e$ nee-e- ot#er t#an t#e "ara'eter$.
ca%cu%ation$ nece$$ar! to reac# t#e re$u%t.
return ( t#e +aria,%e containin/ t#e re$u%t).
0
Therefore, the following three steps must be completed:
1. Is the function returning a result? If yes, t#e -ata t!"e of the result will be used
here. For eample, we want to write a function to calculate the sum of two integers.
The result will be integer. !o, int will be used here. "r, maybe I am writing a fuction
to gi#e a student$s final grade and calculate his letter grade as A, %, C... etc. !o, my
result will be of character type. I will write c#ar here.
&. Choose a $uita,%e na'e for the function. In the C programming language, the
name you choose for the function must not be used as a #ariable name in other parts
of the program.
'. (hat does the function need to do the calculations? The t!"e an- na'e of each
parameter will be written here, such as int 12 int ! meaning, I will gi#e you two
integer #alues, and y.
Then, we start writing the ,o-! of the function. (e start by reser#ing the #ariables
we will use in this function, other than those already selected as input parameters:
(e definitely need a #ariable for the result, so we declare this #ariable, then we
declare any other etra #ariables re)uired by the operations you will perform in the
function. These #ariables are called %oca% +aria,%e$. The function uses these
#ariables for its own calculations, these #ariables ha#e no relation with #ariables in
other functions or the main program, e#en if their names are the same.
After completing the declaration of local #ariables, we write the epressions
necessary to calculate the re)uired result. After completing the operations and the
result has been calculated, the result is returned through the return ( 333 )
statement. For eample, after my calculations if the result is stored in the #ariable y,
I will say return ( ! ).
E4AMPLE: 5rite a function w#ic# wi%% return t#e $u' of two /i+en nu',er$3
1. Is this function returning a result? *es, the sum of two numbers +a double result if
my numbers are double,
&. Choose a name for this function: sum_two
'. (hat are the input parameters to this function? The gi#en numbers num1, num2
let$s ma-e them double, because maybe the user will need to gi#e numbers with a
fraction.
.ote: If the user gi#es me & integer numbers, there is no problem if the parameters
are double, because the number will become &./ instead of &, which will not affect
the result. %ut, if I determine my parameters as integer, if the user gi#es me a
double number li-e &.0, my function will ta-e on the #alue &, so the result will be
affected. 1et me -eep e#erything safe and ma-e my input parameters double type.
The function declaration is now:
23 this function returns the sum of two gi#en numbers 32
double
sum4two + double num1, double num& ,
.ow let$s continue by writing the function body:
15 reser#e a #ariable for the result,
&5 calculate the result
'5 return the result.
.ow we ha#e got:
23 this function returns the sum of two gi#en numbers 32
double
sum4two + double num1, double num& ,
6
23 reser#e the etra #ariables 32
double sum7
23 calculate the result 32
sum 8 num1 9 num&7
23 return the result 32
return +sum,7
:
.otice that
15 we -o not a$6 t#e u$er for t#e +a%ue$ nu'7 an- nu'8 in the function. These
numbers will come to the function as the input parameters.
&5 we -o not re$er+e nu'7 an- nu'8 in the body of the function. These ha#e
already been reser#ed as parameters.
Ca%%in/ (u$in/) t#e function:
According to the eample abo#e, if we need to calculate the sum of two numbers, we
will use this function we ha#e written. (e will send our numbers as input
parameters, and we will recei#e the result.
In the main program, we will say, for eample:
8 ;7
y 8 1/7
< 8 sum4two + , y ,7
The function will find the sum of ; and 1/, and return the result. This result will then
be stored in <.
The parameters in the calling statement may be:
a direct #alue
y 8 sum4two + ;, = ,7
a #ariable
y 8 sum4two + , ' ,7
an arithmetic epression
y 8 sum4two + 9 ', y 5 1,7
If the parameters are arithmetic epressions, first the result of these epressions will
be calculated, then the final #alues will be passed to the function. The function will
ta-e these #alues into its input parameters, and control will pass to the body of the
function. After the result is calculated, the #alue will be returned bac- to the calling
statement, and control will pass bac- to the main program.
Su''ar! of ru%e$:
The #ariable names of input parameters in the function declaration and the
calling statement do not ha#e to be the same.
e.g. If you ha#e a function:
find + int a ,
The calling statement may be
result 8 find +a,7 or, it may be
result 8 find +,7 it doesn$t matter
The order of input parameters in the function declaration and the calling
statement are important
e.g. If you ha#e a function
calculate +int , int y,
and you call this function as:
result 8 calculate +;, =,7
don$t forget that will ta-e on the #alue ;, y will ta-e on the #alue = in the function.
The number of input parameters in the function and the calling statement
must be the same.
e.g. If you ha#e a function
hello + int a, int b, int c,
you cannot call this function as:
result 8 hello + &, ;,7 55> too few parameters
result 8 hello +', =, 1/, ;,7 55> too many parameters
The type of the input parameters in the calling statement will be con#erted
into the type of the input parameters in the function declaration before the
function starts operations. Therefore, it would be more suitable if the type of
each parameter in the calling statement and the function declaration are the
same.
e.g. If you ha#e the function:
goodbye +char a, int b,
remember that the first parameter is char type, and the second is integer type.
Therefore, the parameters in the calling satatement must also be of the same type:
res 8 goodbye+ $T$, ; ,7
?@AAB1?: (rite a function which returns the fractional part of a gi#en double
number. For eample, if I gi#e '0.&;C as parameter, the function should return
/.&;C.
The input parameter will be a double #alue +'0.&;C,. The result is also double
+/.&;C,.
double
return4fraction + double #alue ,
6
int whole4part7
double result7
whole4part 8 #alue7
result 8 #alue 5 whole4part7
return + result ,7
:
Deci$ion 9 t!"e function$:
!ometimes, instead of performing a set of calculations, we may want a function to
chec- a certain condition and answer yes or no. %y con#ention, we write the function
so that it returns 1 if the condition is true, and / if the condition is false.
1et$s consider writing the function which will ta-e an integer number as its input
parameter and chec- to see if this number is e#en or odd. If it is e#en, I will return
1, if it is odd I will return /.
1. Is this function returning a result? *es, it is returning an integer #alue, 1 or /. !o,
let me ma-e this fuction int type.
&. Choose a name for this function: check_even
'. (hat are the input parameters to this function? "ne integer number. number
.ow, assuming this number has arri#ed to the function, therefore the function -nows
its #alue, decide on the procedure to chec- if it is e#en. If it is e#en, I will store 1 in
result, if the number is odd, I will store / in result.
1astly, ha#ing completed all the necessary steps to find the result, I will return this
result to the calling statement.
(e can thus write our function as follows:
int
chec-4e#en +int number, 23 this is a function which ta-es one integer number and
returns an integer result 32
6
23 declare the local #ariables: I need a #ariable for the result. 32
int result7
23 chec- if number is e#en 32
if+number D & 88 /,
result 8 17
else
result 8 /7
23 after the operation is finished, we are ready to return the result 32
return +result,7
:
1et$s ha#e a problem where we will input a number. If it is e#en, find its s)uare. If
not, output a message Ethe number is oddE.
"ur main program will be as follows:
Finclude Gstdio.h>
int
main +#oid,
6
int n, s)uare4n7
printf +Eenter an integer #alueE,7
scanf +EDdE, Hn,7
if+chec-4e#en+num, 88 1,
6
s)uare4n 8 n 3 n7
printf+EThe number is e#en. Its s)uare is DdE, s)uare4n,7
:
else
printf+EThe number is oddE,7
return+/,7
:
It should be remembered that when the calling statement passes the control
together with the #alues of the input parameters to the function, you are in a totally
different memory location. (hat happens within the function, and which #ariables
are used, the #alues these #ariables are assigned, has no relation with main
program. In the same way, since #ariables declared within the function and those
used in the main program are totally independent, any #alue these #ariables contain
in the main program, e#en if their name is the same, has no relation with the
function.
This is #ery important to remember when tracing a program which uses functions.
E4ERCISES
73 Trace t#e fo%%owin/ an- $#ow w#at i$ #a""enin/:
a, int , y7
8 ;7
y 8 find+ ,7
8 find+ y ,7
printf+EDdDdE,
, y,7
int
find+ int n,
6
int -7
- 8 n 3 &7
return + - ,7
:
b, int , y, <7
8 ;7
y 8 07
< 8 find+ , y , 9
1/7
printf+EDdDdDdE,
, y, <,7
int
find+ int y, int
,
6
int -7
if + G y,
- 8 9 17
else
- 8 /7
return + - ,7
:
c, int , y7
8 ;7
y 8 5 find+ ,
',7
printf+EDdDdE,
, y,7
int
find+ int , int
y,
6
int -7
- 8 9 y7
return + - ,7
:
d, int , y, <7
8 ;7
y 8 find+ , ' ,7
< 8 I 9 find+ 2
&, y ,7
printf+EDdDdDdE,
, y, <,7
int
find+ int , int
y,
6
int -7
- 8 9 y7
return + - ,7
:
83 5rite co'"%ete function$ for t#e fo%%owin/2 an- a%$o write a $uita,%e 'ain
"ro/ra' w#ic# ca%%$ t#e$e function$3
1. (rite a function which returns the cubic root of a gi#en number.
&. (rite a function which returns the smallest of the gi#en three numbers.
'. (rite a function which returns the a#erage of gi#en three numbers.
C. a, (rite a function cent_to_fah which ta-es a temperature in degrees Centigrade
and returns the e)ui#alent in degrees Fahrenheit .
b, (rite the main program to input ' temperatures in degrees C. For each
temperature, call your function and output its e)ui#alent in degrees F.
;. a, (rite a function triangle which ta-es ' #alues for the sides of a triangle and
determines whether these ' sides form a trinangle or not +how?,. If "J, return 1, it
not, return /.
b, (rite the main program to input ' #alues. "utput the same #alues with a
message Eform a triangleE or Edo not form a triangleE, according to the result coming
from the function abo#e.
I. (rite a function which returns 1 if a gi#en integer is di#isible by 0, / if it is not
di#isible by 0.
=. (rite a function which is_div which determines whether a certain number n is
di#isible by another number m.
0. (rite a function round_off which ta-es a double #alue and returns its rounded
form as an integer.
K. (rite a function which ta-es the o#erall grade of a student and returns his letter
grade according to the criteria:
o#erall > K/ 5> A
01 5 K/ 5> %
=1 5 0/ 5> C
I1 5 =/ 5> L
GI/ 5> F
1/. (rite a function which ta-es the letter grade of a student and according to the
letter grade, produces one of the following messages:
A 5> ecellent
% 5> good
C 5> satisfactory
L 5> poor
F 5> fail
Mse the switch statement within the function.

Anda mungkin juga menyukai