Anda di halaman 1dari 9

ICS 103: Computer Programming in C

Lab #7: Functions with Input Parameters


Objecties:
Learn how to write user defined functions.
Learn void functions and functions that return result.
Learn functions with input parameters.
Learn the scope of variables.
Functions in C are block of statements that do a certain task. They are used to organize a program
into smaller building blocks and make it easy to understand and follow its behavior.
Why functions?
odularity
aintenability
Code reusability
They facilitate information hiding !Consider the standard math functions we studied" we used
them without knowing how they are implemented#
!ote: In C" #ou cannot $e%ine a %unction insi$e another %unction&
Two types of functions will be covered in this lab$ void functions with input arguments and functions
with input arguments returning a single result$
%. void functions with arguments
& function returning no value has return type oi$
The value or values passed to a function when it is invoked are called actua' arguments.
'f no value is passed to a function when it is invoked( the argument list is empty.
The synta) of a oi$ function that has argument!s# is$
void functionName(parameterList){
declarations if any;
executable statements;
return; // optional
}
where parameterList is a comma separated list of variable declarations. *ach variable is
called a formal argument to the function.
'f a function does not physically appear before the function calling it( it must be declared before
any call to the function. That declaration is called the function prototype.
The synta) of the prototype for a void function that has arguments is anyone of the following$
void
functionName(parameterList);
void functionName(typeList);
The call to a void function with arguments is a statement" it is not part o% an e(pression. 'ts
synta) is$
functionName(expressionList);

where e(pressionList is a comma separated list of e)pressions. *ach such e)pression is
called an actual argument of the function.
*)amples$
#include <stdio.!
#include <stdlib.!
int main(void){
int len"t;
car c;
void dra#Line(int$ car);

printf(%&nter line len"t' %);
scanf(%(d%$ )len"t);
fflus(stdin);
printf(%*n&nter te dra#in" caracter' %);
scanf(%(c%$ )c);
dra#Line(len"t$ c);
printf(%*n%);
dra#Line(len"t+, $ c);
printf(%*n%);
system(%-./0&%);
return 1;
}
void dra#Line(int len"t$ car c){
int 2;
for(2 3 4; 2 <3 len"t; 255)
printf(%(c%$ c);
}
+ote$
There must be a one,to,one correspondence between actual arguments and formal
arguments in terms of number and compatible type.
& variable actual argument may or may not have the same name as the corresponding
formal argument !+ote that even if the arguments have the same names( they are
different variables#.
The scope of a formal argument is its function block.
The scope of a local variable !i.e.( a variable declared within a method( is from its point
of declaration to the end of the block" unless if it is re,declared inside a nested block.
-
-. functions with arguments returning a single value
The synta) of a function with arguments and returning a single value is$
return6ype functionName(parameterList){
declarations if any;
executable statements;
return expression;
}
+ote$
The e)pression in the return statement must have a type that is compatible with the
function.s return type.
't is possible for a function that has one or more selection structures to have more than one
return statement" however only one such statement will be e)ecuted.

The call to a function returning a single value is an e(pression" it can appear anywhere an
e)pression is allowed.
*)amples$
#include <stdio.!
#include <stdlib.!
int main(void){
double #$ 7;
double sum08uares(double$ double);

printf(%&nter t#o numbers' %);
scanf(%(lf(lf%$ )#$ )7);

printf(%*n# 3 (.,f $ 7 3 (.,f*n%$ #$ 7);
printf(%*n6e sum of s8uares of 9.1+# 5 ,.1 and 7 is (.,f*n%$
sum08uares(9.1+# 5 ,.1$ 7));

system(%-./0&%);

return 1;
}
double sum08uares(double x$ double y){
return x + x 5 y + y;
}
/
#include <stdio.!
#include <stdlib.!
int main(void){
int num4$ num,$ max:alue;
int find;ax(int$ int);
printf(%&nter t#o inte"ers' %);
scanf(%(d(d%$ )num4$ )num,);
max:alue 3 find;ax(num4$ num,);
printf(%*n6e maximum of (d and (d is (d*n%$ num4$ num,$
max:alue);

system(%-./0&%);
return 1;
}
int find;ax(int x$ int y){
int max;

if(x !3 y)
max 3 x;
else
max 3 y;

return max;
}
/+ <omputes te factorial of a number +/
#include <stdio.!
int factorial(int n);

int main(void) {
int num$ fact;
printf(%&nter an inte"er bet#een 1 and =' %);
scanf(%(d%$ )num);
if(num < 1) {
printf(%>actorial not defined for ne"ative numbers*n%);
} else if (num <3 =) {
fact 3 factorial(num);
printf(%6e factorial of (d is (d*n%$ num$ fact);
} else
printf(%Number out of ran"e' (d*n%$ num);

system(%pause%);
return 1;
}
int factorial(int n){
int i$ product 3 4;
for(i 3 n; i ! 4; ??i)
product +3 i;
return product;
}
0
Logical functions
'n C( logical functions !functions that return true or false# are declared to be of int type$ & function that returns 1(
returns false( and a function that returns non,zero value( returns true.
*)ample$
#include <stdio.!
#include <stdlib.!
int main(void){
int num;
int is&ven(int);
printf(%&nter an inte"er' %);
scanf(%(d%$ )num);
if(is&ven(num))
printf(%*n(d is even*n%$ num);
else
printf(%*n(d is odd*n%$ num);

system(%-./0&%);
return 1;
}
int is&ven(int b){
return b ( , 33 1;
}
2
Example 1:
//* prints the largest of two integers in a box;
#include<stdio.h>
double bigger(double n1, double n2; // function protot!pe
"oid print#rboxed(double rnu$; // function protot!pe
int $ain("oid %
double nu$ber1,nu$ber2, $ax;

printf(&'lease input two nu$bers (&;
scanf(&)lf )lf&, *nu$ber1, *nu$ber2;

$ax + bigger(nu$ber1, nu$ber2; // function call

printf(&,he $ax of )lf and )lf is-n&,nu$ber1,nu$ber2;
print#rboxed($ax ; // function call
s!ste$(&pause&;
return .;
/
//returns the largest of two nu$bers
// function definition
double bigger(double n1, double n2 %
double larger;

if(n1 > n2
larger + n1;
else
larger + n2;

return larger;
/
/* 0ispla!s a real nu$ber in a box. */
// function definition
"oid print#rboxed(double rnu$ %
printf(&***********-n&;
printf(&* *-n&;
printf(&* )1.2f *-n&, rnu$;
printf(&* *-n&;
printf(&***********-n&;
/
3
Example 2:
//draws a rectangle using functions
#include <stdio.h>
void draw_solid_line(int size);
void draw_hollow_line(int size);
void draw_rectangle(int len, int wide);
int main(void) {
int length, width;

rintf(!"nter length and width of rectangle >!);
scanf(!#d#d!, $length, $width);

draw_rectangle(length, width);

s%stem(!ause!);
return &;
'
void draw_solid_line(int size) {
int i;
for (i(); i<(size; i**)
rintf(!+!);
rintf(!,n!);
'
void draw_hollow_line(int size) {
int i;
rintf(!+!);
if (size > -) {
for (i(); i<( size.-; i**)
rintf(! !);
'
rintf(!+,n!);
'
void draw_rectangle(int len, int wide) {
int i;
draw_solid_line(wide);
if (len > -) {
for (i(); i<(len . -; i**)
draw_hollow_line(wide);
'
draw_solid_line(wide);
'
4
Laboratory Tasks:
). /odif% "0amle - so that the s%m1ol to draw the rectangle, instead
of 1eing 2+3, should 1e rovided 1% the user.
-. 4rite a void function divisors that receives an integer num1er and
disla%s its divisors on the screen including ) and itself. 4rite the main
function to test %our function.
5. /odif% the function divisors as an int function that now it returns the
num1er of divisors e0cluding ) and the num1er itself. 4rite a main
function to test %our function.
6he 7 divisors of 5- are -,7,8 and )9.
7. 4rite a logical function erfect_:;uare that receives a ositive
integer num1er and chec<s if it is a erfect s;uare or not.
=ote> erfect s;uare num1ers are 7, ?,)9,-@,59 etcA.
5
i.e., a ositive integer m is a erfect s;uare if a ositive integer n such
that m = n
2
4rite a main function that ma<es use of the erfect_:;uare function to
find and rint all erfect s;uares 1etween m1 and m2 inclusive, where m1
and m2 are end values of a range inut 1% the user. Bour rogram must
disla% an aroriate message if there is no erfect s;uare 1etween m1
and m2 inclusive.
@. 4rite a logical function, is_prime, that ta<es a ositive integer num1er
and determines if the num1er is rime or not.
=ote> C rime num1er is one that does not have roer factors i.e., it is a
num1er whose onl% divisors are ) and itself.
4rite a main function that ma<es use of the is_rime function to find and
rint all the rime num1ers from - to )&&.
6

Anda mungkin juga menyukai