5 Function
5 Function
Weng Kai
Sum for Prime Numbers
Sum
o f b a d q u a l
i c a t i o n ” i s o ne evidence
“code dupl
• Almost identical
A function for sum
Function函数?
• y = f(x)
Definition
return
parameter
name
head
body
Calling a Function
• function name(parameter values);
• return;
• return expression;
• Can be dropped off
• No return is OK
Check based on
• name
prototype
• parameter(number and types)
• return type
• literal
• variable
• return value
• expression
What if type mismatch?
s ed i n t h e C
c an b e p a s
Only v a l u e
• Or a block of a statement
• No duplicated name can be
defined in one block.
• Even a block of nothing
• void f(void);
• or
• void f();
• f(a,b)
• f((a,b))
Function Defined Inside A
Function?
• return (i);
About main()
• int main() is a function too.
• Windows:if errorlevel 1 …
• Unix Bash:echo $?
• Csh:echo $status
Lab 5