Upload
angcw
View
216
Download
0
Embed Size (px)
Citation preview
7/25/2019 Slide Function 2016
1/26
FunctionFunction
7/25/2019 Slide Function 2016
2/26
FunctionFunction Several function you know:Several function you know:
main ( ) user defined functionmain ( ) user defined function
printf( ) & scanf( ) library functionprintf( ) & scanf( ) library function
Function enables program to be break up intoFunction enables program to be break up intosegmentssegments
7/25/2019 Slide Function 2016
3/26
FunctionFunction Advantages:Advantages:
Dividing the program into separate well-denedDividing the program into separate well-dened
functions enable each function to be written andfunctions enable each function to be written and
tested separately. t can simplies the process oftested separately. t can simplies the process of
getting the total program to work.getting the total program to work.
when a big program is broken into functions! pro"ectwhen a big program is broken into functions! pro"ect
can be divided to di#erent programmers. $achcan be divided to di#erent programmers. $ach
programmer responsible on di#erent functions.programmer responsible on di#erent functions.
%here are a set of functions in & libraries which are%here are a set of functions in & libraries which are
free to use. t will speeds up the programfree to use. t will speeds up the program
development.development.
7/25/2019 Slide Function 2016
4/26
Dene Function
'eneral form of a function:'eneral form of a function:
DataType function_name(parameter_list)DataType function_name(parameter_list)
{{
body of codebody of code
}}
Return values data type
Parameter passing
* Function Definition
7/25/2019 Slide Function 2016
5/26
void hello (void)
{ printf(hello world !\n )}
$(ample )$(ample )
To create simple function:
Data type Name Parameter passing
7/25/2019 Slide Function 2016
6/26
$(ample )$(ample )include"stdio#h$include"stdio#h$void hello(void)void hello(void){{ printf(hello world !\n )printf(hello world !\n )
}}
void main()void main(){{
hello()hello()}}note the use of the key word void.note the use of the key word void.
The CFunctionbody outsideOf the main
program
The c function called fromthe main program (calling fu
Called function
7/25/2019 Slide Function 2016
7/26
*ain memory + Func memory*ain memory + Func memoryinclude"stdio#h$
void func (){
int twotwo%&
printf('two is d\n' two)}
int main (void){
int oneone % *func ()
printf('+ne is d\n' one)
return (,)}
Two is 2
One is 1
7/25/2019 Slide Function 2016
8/26
*ain memory + Func memory*ain memory + Func memoryvoid func()
{ int local-ylocal % &y % .- % local / y
printf ('\t - % d\n' -)printf ('\t local in func % d\n' local)
}
int main (void){
int locallocal % *
printf('local in main % d\n' local)func()
printf ('local in main % d\n' local)
return (,)}
local in main 1
! "
local in func 2
local in main 1
7/25/2019 Slide Function 2016
9/26
,uestion ),uestion )
rite a program to calculate the arearite a program to calculate the area
of a circle using function. %heof a circle using function. %he
program should allow user to enterprogram should allow user to enter
the value of radius and display thethe value of radius and display the
result with proper unit.result with proper unit.
7/25/2019 Slide Function 2016
10/26
assing parameters /assing parameters /
arguments to the functionarguments to the function
hen function is called! the calling function mayhen function is called! the calling function may
have to pass some values to the called function.have to pass some values to the called function.
%here are 0 ways to pass the parameter/arguments%here are 0 ways to pass the parameter/arguments
to the called function.to the called function.
1i2 call by1i2 call by valuevalue3 values of the variables are passed3 values of the variables are passed
by the calling function to the called function.by the calling function to the called function.
1ii2 call by1ii2 call by referencereference3 address of the variables are3 address of the variables are
passed by the calling function to the called function.passed by the calling function to the called function.
7/25/2019 Slide Function 2016
11/26
%he argument names in the function declaration%he argument names in the function declaration
and function denition need not to be the sameand function denition need not to be the same
but the data types or the arguments must match.but the data types or the arguments must match.
%he arguments in the dened function must place%he arguments in the dened function must place
in the correct order too.in the correct order too.
assing parameters /assing parameters /
arguments to the functionarguments to the function
7/25/2019 Slide Function 2016
12/26
Function may accept as many parameters#argumentsas it
need$ Function wit% no parameter passing is fine too$
void prn_messa0e(int 1)
{int i
printf( 2ere is the messa0e3\n')
for (i % , i "1 i//){ printf( 2ave a nice day!\n')}
}
One parameter passing
assing parameters /assing parameters /
arguments to the functionarguments to the function
7/25/2019 Slide Function 2016
13/26
include"stdio#h$
void prn_messa0e(int 1){int i
printf ('2ere is the messa0e3\n')for (i % , i "1 i//){ printf (' 2ave a nice day!\n')}
}
int main (void){
int n
printf ('There is a messa0e for you# \n')printf ('2ow many times do you want to see it4')scanf ('d' 5n)
prn_messa0e(n)return (,)
}
calling function
$(ample 0
7/25/2019 Slide Function 2016
14/26
Function may accept as many parametersas it need$
void prn_messa0e(int 1 int 6 int m){
int iprintf( 2ere is the messa0e3\n')
for (i % , i "1 i//)
{ printf( 2ave a nice day!\n')}
}
assing parameters /assing parameters /
arguments to the functionarguments to the function
7/25/2019 Slide Function 2016
15/26
include"stdio#h$void print_min(int - int y)
{if (- " y)printf ('min % d\n' -)
elseprintf('min % d\n' y)
}
int main (void){
int a b
printf('7nput a4')scanf ('d'5a)printf ('7nput b4 ')scanf ('d' 5b)
print_min(a b)return (,)
}
$(ample 4
7/25/2019 Slide Function 2016
16/26
,uestion 0,uestion 0
rite a program to convert time torite a program to convert time to
minutes using function. rogramminutes using function. rogram
should allow user to enter the hoursshould allow user to enter the hours
and minutes and the time in minutes.and minutes and the time in minutes.
7/25/2019 Slide Function 2016
17/26
Function with returnFunction with return
StatementStatement%he return statement is used to terminate%he return statement is used to terminate
the e(ecution of a function and returnsthe e(ecution of a function and returns
control to the calling function.control to the calling function.
A return statement may or may not returnA return statement may or may not return
a value to the calling function. Functiona value to the calling function. Function
can only return ) value.can only return ) value.
A function having void as its return typeA function having void as its return type
cannot return any value.cannot return any value.
7/25/2019 Slide Function 2016
18/26
Function may return only 1 valueor no value
intmin(int a int b){
if (a"b)
return aelse
return b}
return a to main& a 'int(
Function with returnFunction with return
StatementStatement
7/25/2019 Slide Function 2016
19/26
int multiply(int -int y){ return (-8y) 98 can bereturn -8y 89}
int main(){ int answer answer % multiply(*,&,)
printf(The answer is d\n:answer)}
Prg50_FuncParameter.cpp
Function with returnFunction with return
StatementStatement
http://prg50_funcparameter.cpp/http://prg50_funcparameter.cpp/7/25/2019 Slide Function 2016
20/26
include"stdio#h$int min(int a int b){
if (a"b)return a
elsereturn b
}
int main(void){
int 6 1 m
printf ('7nput two inte0ers3 ')scanf ('dd' 56 51)
m % min(6 1)printf ('\nThe minimum is d# \n' m)return (,)
}
$(ample 5
7/25/2019 Slide Function 2016
21/26
int multiply(int -int y)
int main(){ int answer
answer % multiply(*,&,) printf(The answer is d\n:answer)}
int multiply(int -int y){ return (-8y) 98 can be return -8y 89}
Function %appen
after main ' (
Function with returnFunction with return
StatementStatement
7/25/2019 Slide Function 2016
22/26
6sing Functions:
include"stdio#h$include"math#h$ 98 contains s;rt() function89
7/25/2019 Slide Function 2016
23/26
include"stdio#h$
int addTwoDi0its(int)int firstDi0it(int)int secondDi0it(int)
int main(void){
int number int sum
printf ('=nter an inte0er3 ') scanf ('d' 5number)
sum % addTwoDi0its(number) printf('Bum of last two di0its is 3 d\n' sum)
return (,)}
int addTwoDi0its(int number){
int result
result % firstDi0it(number) / secondDi0it(number) return result}
int firstDi0it(int num){ return (num *,)}
int secondDi0it (int num){ int result result % (num 9 *,) *, return result}
$(ample 5
7/25/2019 Slide Function 2016
24/26
Function call by reference
Advantages:Advantages:
- provide greater time and space e7ciency- provide greater time and space e7ciency
1arguments are not copied into new1arguments are not copied into new
variables2variables2- function can change the value of- function can change the value of
argument in the caller.argument in the caller.
- function can return only ) value. f need- function can return only ) value. f needto return multiple values! pass theto return multiple values! pass the
arguments by reference so the values canarguments by reference so the values can
be modied in caller.be modied in caller.
7/25/2019 Slide Function 2016
25/26
Function call by reference
include"stdio#h$
7/25/2019 Slide Function 2016
26/26
When you go back