Week 11 - Lecture 30 - Parameters in Function
Week 11 - Lecture 30 - Parameters in Function
Functions
Dr Taimur Ahmed
Department of IT & CS, PAF-IAST
Lecture# 30
parameters/arguments, variables,
passing/returning values in functions
Passing Data into a Function
❑ Functions are defined in the main function to perform a specific task, so
data can be passed from main function into the user defined function.
Actual argument
Formal argument
➢ the number of arguments in the call must match the prototype and definition
➢ the first argument will be used to initialize the first parameter, the second argument to
initialize the second parameter, etc.
Actual argument
Formal argument
double x;
x = pow(2.0, 10.0);
❑ When the function begins, its local variables and its parameter variables
are created in memory, and when the function ends, the local variables
and parameter variables are destroyed.
❑ This means that any value stored in a local variable is lost between calls
to the function in which the variable is declared.
❑ The scope of a global variable is the portion of the program from the
variable definition to the end.
❑ This means that a global variable can be accessed by all functions that
are defined after the global variable is defined.
❑ You should avoid using global variables because they make programs
difficult to debug.
❑ static local variables are defined and initialized only the first time the
function is executed. 0 is the default initialization value.
In this program, each time showLocal is called, the localNum variable is re-
created and initialized with the value 5.
Lecture# 30 - Functions: Arguments/parameters, varibales type | 30
A Different Approach, Using a Static Variable