0% found this document useful (0 votes)
3 views36 pages

5 Function

The document discusses the concept of functions in programming, including how to define, call, and return values from functions. It explains the importance of prototypes, parameter passing, local variables, and the scope and lifetime of variables. Additionally, it includes examples and lab exercises related to these topics.

Uploaded by

cpb977zs9n
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views36 pages

5 Function

The document discusses the concept of functions in programming, including how to define, call, and return values from functions. It explains the importance of prototypes, parameter passing, local variables, and the scope and lifetime of variables. Additionally, it includes examples and lab exercises related to these topics.

Uploaded by

cpb977zs9n
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Function

Weng Kai
Sum for Prime Numbers
Sum

• Print sum of 1 to 10, 20 to 30 and 35 to 45


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函数?

• Function is a piece of code, which takes zero or many


parameters, does one thing and returns zero or one value

• It is something like the function:

• y = f(x)
Definition
return
parameter
name

head

body
Calling a Function
• function name(parameter values);

• () is the evidence of a function call

• () is needed when there is no


parameter at all

• Values should be provided in


correct order and number

• These values will be used to


initialize the parameters
respectively
to Return From a Function

• The function knows


the place it is
called, and returns
to the right place
to Return a Value Back

• return ceases the execution of the


function, and sends back a value

• return;

• return expression;

• There may be more than one return in a


function
The Value Returned
• Can be assigned to a
variable

• Can be passed to a function

• Can be dropped off

• Sometimes it is the side-


effect that we want from
the function
Function without Return
Value
• void function name(parameter list)

• There is not to be return with value

• No return is OK

• Can not be right value at calling


n ha s a ty p e.
t he fu nc t i o
wh e n
v a l u e m u st be used
et ur n w i t h
Ar
Lab 1

• PTA W6 6-1 使⽤用函数求阶乘和


Prototype原型
Where do We Put the
Function
• The reason we have to put sum()
before main() is:

• The compiler parses code line by


line

• It needs to know how sum() looks


like at the line sum(1,10)

• How it looks like means the


name, the parameter list and the
return type

• So it can check whether your


calling is correct
If the Compiler does not
Know

• How about we put the function


after the main()?

• It will assume all the parameters


and return type are int

• What if that is not the case...


Prototype
Prototype
• A function head, ending by ;, is the prototype of
that function

• It tells the compile how the function looks like

Check based on
• name
prototype
• parameter(number and types)

• return type

• Prototype could be in the function that calls it

• But it is usually somewhere before the function


nowadays

• The names of the parameters are not required in Actual head


the prototype.
Lab 2

• PTA W6 6-2 判断⼀一个数字是否在⼀一个正整数中


Passing of
Parameters
Calling a Function
• If the called function has parameters, the calling must
provide values of the right number and right types

• The values are expression, including:

• literal

• variable

• return value

• expression
What if type mismatch?

• It is the biggest leak in C that type may not match at


calling.

• The compiler always converts that quietly. But it may not


the way you expected.

• Later languages, C++/Java are more strict on it.


What is to be passed?

s ed i n t h e C
c an b e p a s
Only v a l u e

• Can we swap a and b like this?


Passing by Value
• Each calling of a function has its own memory space, Former
Parameter
Parameter
where parameters are in. That space is isolated from
other functions.
Real
Value
• At calling, the values provided are used to initialize the Parameter
parameters. It is not the variables that are passed to.

• In the past, the parameters in the function head are


called “former parameter形式参数”, while the values
at the calling are called “real parameter实际参数”
Former
Parameter
Parameter
• These terms are easily to be misunderstood that it is
the “real” be passed into the function to replace the
“former” one, rather than the values. So we suggest
do not call them like this.

• They are parameters and values.


Lab 3

• PTA W6 6-3 在数组中查找指定元素


Local Variables
本地变量量
Local Variables
• Each execution of a function creates an independent
variable space. All variables inside that space are exclusive
for this execution, and are called “local variables”.

• Local are to the execution rather than the function.

• All variables defined inside a function are local variables.

• Variables defined outside any functions are global


variables.

• Parameters are local variables either.


Lifetime and Scope
⽣生存期和作⽤用域

• Lifetime: When does the variable appear and when does it


disappear.

• Scope: Where is the variable accessible (can be used)

• For local variable, the two questions have the same


answer: the block (within a pair of brackets)
Rules to Local Variables
• They are defined inside a block

• Either a block of a function • Redefinition of the same name


hides that outside.

• Or a block of a statement
• No duplicated name can be
defined in one block.
• Even a block of nothing

• There are no these variables


• No default initialization for local
variables.
before execution into the block.

• They are to be disappeared at


• Globals have that.
leaving the block.
• Parameters are to be initialized
at calling (not assigned).
• Variables defined outside (and
before) the block are valid inside.
Lab 4

• PTA W6 6-4 使⽤用函数判断完全平⽅方数


Other Details
Empty Parameter List

• void f(void);

• or
• void f();

• means the parameter list of f() is unknown, but not has


to be empty (could be empty either)
comma operator?

• The comma in calling of a function is not an operator but


a symbol

• f(a,b)

• f((a,b))
Function Defined Inside A
Function?

• Embedded function definition is illegal in the C.


What is this?

• int i,j,sum(int a, int b);

• return (i);
About main()
• int main() is a function too.

• Necessary to be int main(void)?

• Any one cares 0 that returned?

• Windows:if errorlevel 1 …

• Unix Bash:echo $?

• Csh:echo $status
Lab 5

• PTA W6 6-5 统计个位数字

You might also like