0% found this document useful (0 votes)
16 views

Week 11 - Lecture 30 - Parameters in Function

Uploaded by

rajabfrq
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)
16 views

Week 11 - Lecture 30 - Parameters in Function

Uploaded by

rajabfrq
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/ 37

Week# 11

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.

c = pow(a, b); //statement in the main function

❑ Values passed to function are called arguments, for example a and b in


above example
❑ Variables in a function that hold the values passed as arguments are
called parameters

Lecture# 30 - Functions: Arguments/parameters, varibales type | 3


Passing Data into a Function

Lecture# 30 - Functions: Arguments/parameters, varibales type | 4


Passing Data into a Function
❑ A parameter used in function call (in main() function) can also be called
as an actual parameter or an actual argument
❑ A parameter used in function definition and body can also be called as a
formal parameter or a formal argument

Actual argument

Formal argument

Lecture# 30 - Functions: Arguments/parameters, varibales type | 5


Passing Data into a Function
❑ For each function argument,
➢ the prototype must include the data type of each parameter inside its parentheses
➢ the header must include a declaration for each parameter in its ()

void evenOrOdd(int); //prototype


void evenOrOdd(int num) //header
evenOrOdd(val); //call

Lecture# 30 - Functions: Arguments/parameters, varibales type | 6


Passing Data into a Function
❑ When calling a function and passing multiple arguments:

➢ 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.

Lecture# 30 - Functions: Arguments/parameters, varibales type | 7


Passing Data into a Function

Lecture# 30 - Functions: Arguments/parameters, varibales type | 8


Passing Data into a Function

Actual argument

Formal argument

Lecture# 30 - Functions: Arguments/parameters, varibales type | 9


The return Statement
The return Statement
❑ Used to end execution of a function
❑ Can be placed anywhere in a function
➢ Statements that follow the return statement will not be executed
❑ Can be used to prevent abnormal termination of program
➢ Like using statement of return 0
❑ In a void function without a return statement, the function ends at its
last }

Lecture# 30 - Functions: Arguments/parameters, varibales type | 11


The return Statement

Lecture# 30 - Functions: Arguments/parameters, varibales type | 12


Returning a Value From a Function
Returning a Value From a Function
❑ A function can return a value back to the statement that called the
function.
❑ You've already seen the pow function, which returns a value:

double x;
x = pow(2.0, 10.0);

Lecture# 30 - Functions: Arguments/parameters, varibales type | 14


Returning a Value From a Function
❑ In a value-returning function, the return statement can be used to
return a value from function to the point of call. Example:
Return type

int sum(int num1, int num2)


{
double result;
result = num1 + num2;
return result;
}
Value being
returned

Lecture# 30 - Functions: Arguments/parameters, varibales type | 15


Returning a Value From a Function
❑ Functions can return the values of expressions, such as num1 + num2

int sum(int num1, int num2)


{
return num1 + num2;
}

Lecture# 30 - Functions: Arguments/parameters, varibales type | 16


Returning a Value From a Function

Lecture# 30 - Functions: Arguments/parameters, varibales type | 17


Returning a Value From a Function

Lecture# 30 - Functions: Arguments/parameters, varibales type | 18


Local and Global Variable
Local and Global Variables
❑ Variables defined inside a function are local to that function. They are
hidden from the statements in other functions, which normally cannot
access them.
❑ Because the variables defined in a function are hidden, other functions
may have separate, distinct variables with the same name.

Lecture# 30 - Functions: Arguments/parameters, varibales type | 20


Local and Global Variables

Lecture# 30 - Functions: Arguments/parameters, varibales type | 21


Local and Global Variables
❑ When the program is executing in main, the num variable defined in
main is visible. When anotherFunction is called, however, only
variables defined inside it are visible, so the num variable in main is
hidden.

Lecture# 30 - Functions: Arguments/parameters, varibales type | 22


Local Variables Lifetime
❑ A function’s local variables exist only while the function is executing. This
is known as the lifetime of a local variable.

❑ 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.

Lecture# 30 - Functions: Arguments/parameters, varibales type | 23


Global Variables and Global Constants
❑ A global variable is any variable defined outside all the functions in a
program.

❑ 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.

❑ Any global that you create should be global constants.


Lecture# 30 - Functions: Arguments/parameters, varibales type | 24
Global Variables and Global Constants
Global constants defined for
values that do not change throughout
the program’s execution.

Lecture# 30 - Functions: Arguments/parameters, varibales type | 25


Initializing Local and Global Variables
❑ Local variables are not automatically initialized. They must be initialized
by programmer.

❑ Global variables (not constants) are automatically initialized to 0


(numeric) when the variable is defined

Lecture# 30 - Functions: Arguments/parameters, varibales type | 26


Static Local Variable
Static Local Variables
❑ Local variables only exist while the function is executing. When the
function terminates, the contents of local variables are lost.

❑ static local variables retain their contents between function calls.

❑ static local variables are defined and initialized only the first time the
function is executed. 0 is the default initialization value.

Lecture# 30 - Functions: Arguments/parameters, varibales type | 28


Static Local Variables

Lecture# 30 - Functions: Arguments/parameters, varibales type | 29


Static Local Variables

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

Lecture# 30 - Functions: Arguments/parameters, varibales type | 31


A Different Approach, Using a Static Variable

Lecture# 30 - Functions: Arguments/parameters, varibales type | 32


A Different Approach, Using a Static Variable
If you do initialize a local static variable, the initialization only happens once.

Lecture# 30 - Functions: Arguments/parameters, varibales type | 33


Default Arguments
Default Arguments
❑ A Default argument is an argument that is passed automatically to a
parameter if the argument is missing on the function call.

❑ Must be a constant declared in prototype:


void evenOrOdd(int = 0);
❑ Can be declared in header if no prototype

❑ Multi-parameter functions may have default arguments for some or all of


them:
int getSum(int, int=0, int=0);

Lecture# 30 - Functions: Arguments/parameters, varibales type | 35


Default Arguments

Lecture# 30 - Functions: Arguments/parameters, varibales type | 36


Default Arguments

Lecture# 30 - Functions: Arguments/parameters, varibales type | 37

You might also like