Lecture User Defined Functions
Lecture User Defined Functions
• Function name and the number and type of arguments in the function call must be
same as that given in the function declaration and function header of the function
definition
• Names (and not the types) of variables in function declaration, function call and
header of function definition may vary
• Arguments may be passed in the form of expressions to the called function. In such
a case, arguments are first evaluated and converted to the type of formal
parameter and then the body of the function gets executed.
• If the return type of the function is not void, then the value returned by the called
function may be assigned to some variable as given below.
variable_name = function_name(variable1, variable2, …);
Advantages:
•The method doesn't change the original variable, so it is preserving data.
•Whenever a function is called it, never affect the actual contents of the
actual arguments.
•The other advantage could be arguments can be variables(e.g. x), literals(e.g.
6,0…), or expressions(e.g. x+1)
Disadvantages:
•Copy of the variable is created hence it can consume additional storage
space
•Along with more space, it can take a lot of time to copy, thereby resulting in
performance penalty, especially if the function is called many times
Advantages:
•It provides greater time and space efficiency as duplicate copies of the
arguments are not created
•In this method, there is no copy of the argument made. Therefore it is
processed very fast.
Disadvantages:
•Original values are not safe, as they can be modified
•We can only pass addresses as actual arguments(we cannot pass:
literals(1,2,3…) / or expressions(x+1,y+2..) as actual arguments
Value modification Original value not modified. The original value is modified.