2. Objective of
this session
Student able to know definition dan theories of function.
Student able to use and create functions.
Student able to know theories of variable scope.
Student able to identify the scope of variable.
3. Function
• A function is a block of code that performs a task
• A function is a type of procedure or routine.
• Some program contains at least one function: main()
• Why use functions?
▫ Allow programmer to avoid duplicating code
▫ Allow programs to be broken into manageable
tasks
• Two categories of function:
▫ Value-returning functions
▫ Void functions
4. Value –
Returning
Function
• A value-returning function returns precisely one value
after completing its assigned task.
▫ Usually returned to the statement that called function
Typically, statement displays return value, uses it in a
calculation, or assigns it to a variable
▫ Some value-returning functions
E.g., pow(3,2)
5. Value –
Returning
Function
• A value-returning function returns precisely one value
after completing its assigned task.
▫ Usually returned to the statement that called function
Typically, statement displays return value, uses it in a
calculation, or assigns it to a variable
▫ Some value-returning functions
E.g., pow(3,2)
9. Function
Header
• Function header: first line in a function definition
▫ Does not end with a semicolon
▫ Begins with returnDataType
▫ Next comes the name of the function
Naming rules are the same as for naming variables
Name usually begins with a verb
▫ Also has an optional parameterList enclosed in ()
Lists data type and name of formal parameters
Formal parameters store the information passed to the function when it is
invoked
Formal
Parameter
10. Function
Body
• Function body contains instructions the function
follows to perform its assigned task
▫ Begins with { and ends with }
▫ The last statement is usually return expression;
expression represents the value the function returns to the statement
that called it
▫ return statement alerts computer that the
function has completed its task
Data type of expression must agree with the returnDataType specified
in function header
12. Call a function
(1)
void main(){
int a, b, c;
b = 5;
c = sum (a, b);
write (c);
}
int sum (int num1, int num2)
{
int result;
result = num1 + num2;
return (result);
}
Calling function
Variable, get the
return of function
13. Call a function
(2)
void main(){
int a , b, c;
a = 9; b = 5;
c = sum (a, b);
write (c);
}
int sum (int num1, int num2)
{
int result;
result = num1 + num2;
return (result);
}
Actual Parameter
14. Passing
Information to
a Function
• Items passed to a function are actual perameters/
arguments
▫ Can be a variable, named constant, literal constant, or
keyword
You can pass a variable:
by value (a copy of the value is passed), or
by reference (the variable address is passed)
▫ Examples
calcRectangleArea(2, 3)
calcRectangleArea(length, width) pass by value
15. Using a
Function
Prototype
• If a function definition appears below main(), you must
enter a function prototype above main()
▫ Prototype specifies function’s name, data type of
return value and of each formal parameter
17. The Scope
and Lifetime
of a Variable
• A variable’s scope indicates where
in the program it can be used
▫ Local or global
Local variables are declared within a
function or appear in a function’s
parameterList
Global variables are declared outside
of any function and remain in memory
until program ends
Avoid using global variables in your
programs
• A variable’s lifetime indicates how
long it remains in memory
18. Summary
A function is a block of code that performs a task.
Categories of function: Value-returning functions
& Void functions
Components of function : Header Function &
Body Function
Types of Parameter : Formal & Actual
Next Recursive Function