Module 3 CC
Module 3 CC
in
Return data type Converts a character to upper case. The function receives
a character as an argument, converts it into upper case
char convert_to_uppercase(char ch); and returns the converted character back to the calling
dunction.
Function name Calculated average if 2 integer numbers a and b received
as argument. The function returns a floating point value.
float avg (int a, int b);
Data type of variable Finds the largest of 3 numbers, a, b,c received as
Dept. of MCA argument. An integer value which is the largest of the three
Click to Edit int find_largest (int a, int b, int c); numbers is returned to the calling function.
Variable 1 Multiplies 2 floating point numbers a and b that are
received as arguments and returns a double value.
double multiply (float a, float b);
void swap (int a, int b); Swaps or interchanges the value of integer variables a and
b received as arguments. The function returns no value,
therefore the return type is void.
void print(void); The function is used to print information on screen. The
function neither accepts any value as arguments nor
returns any value. Therefore, the return type is void and
the argument list contains void data type
Function Definition
• A space is allocated for the function memory when it is defined.
• It comprises two parts:
a. Function Header
b. Function Body
• Syntax:
return_data_type function_name(data_type variable1, data_type
Dept. of MCA variable2, ….)
Click to Edit
{
………
Statements
………
return (variable);
}
Function Definition
• It is an error to place a semicolon after the function
header in the function definition.
• The parameter list in the function definition as well as
function declaration must match.
• A function can either be defined before or after the
Dept. of MCA
main().
Click to Edit
Function Call
• Syntax:
function_name (variable1, variable2,….);
• List of variable used in the function call is known as actual
parameter list. The actual parameter list may contain variable
names, expressions, or constants.
• Function name and number and type of arguments in the
Dept. of MCA
Click to Edit
function call must be same as that of given in function
declaration and function header of function definition.
• If the parameter passed to function more than what it is
specified to accept than the extra arguments will be discarded.
If anything less, then garbage value will be displayed.
• Names (not the types) of variables in function declaration, call
and header of function definition may vary
Function Call
• Arguments may be passed in the form of expressions to
the called function. In such cases, the arguments are
first evaluated and converted to the type of formal
parameter and then the body of the function gets
executed.
• Parameter list must be separated by commas.
Dept. of MCA
Click to Edit • It is an error to use void as the return type when the
function is expected to return a value to the calling
function.
return Statement
• The return Statement terminated the execution of the
called function and returns control to the calling
function.
• When the return statement is encountered, the program
execution resumes in the calling function at the point
immediately following the function call.
• Syntax: return <expression>;
Dept. of MCA
Click to Edit
return Statement
• An error is generated when the function does not retune
data of the specified type.
• A function that does not return any value cannot be
placed on the right side of the assignment operator.
Dept. of MCA
Click to Edit
Passing parameters to functions
• When a function is called, the calling function may
have to pass some values to the called function.
• There are 2 ways in which arguments/parameters can be
passed to function:
i. Call by Value in which values of variable are
Dept. of MCA
passed by the calling function to the called function.
Click to Edit
ii. Call by reference in which address of variables are
passed by the calling function to the called function.
Call by Value
• The called function creates new variables to store the
value of the arguments passed to it. The called function
uses a copy of the actual argument to perform its
intended task.
Dept. of MCA
Click to Edit
Call by Reference
• When calling function passes an argument to the called
function using call-by-value method, the only way to
return the modified value of the argument to the caller
is explicitly using the return statement. A better option
when a function wants to modify the value of the
argument is to pass arguments using call-by-reference
Dept. of MCA technique.
Click to Edit
• We declare the function parameters as references rather
then normal variables. When this is done any changes
made by function to the arguments it receives are
visible in the calling function.
Call by Reference
• To indicate an argument is passed using call by
reference, an asterisk (*) is placed after the type in the
parameter list. This way the changes made to the
parameter in called function will then be reflected in the
calling function.
• In call-by-reference method, a function receives an
Dept. of MCA
Click to Edit
implicit reference to the argument, rather then a copy of
its value.
• The function can modify the value of the variable and
the change will be reflected in the calling function as
well.
Advantages and Disadvantages
Advantages Disadvantages
In this method, the value of each variable in the In this method, the address of actual variables in
calling function is copied into corresponding the calling function is copied into the dummy
dummy variables of the called function. variables of the called function.
Dept. of MCA
Click to Edit
static Storage Class
• Scope: Local to the function or globally, depending on declaration.
• Lifetime: Preserves value across function calls till program termination.
• Initialized with: 0 by default.
• Example:
void counter()
{
Dept. of MCA static int count = 0; // Retains value across calls
Click to Edit
count++;
}
register Storage Class
• Scope: Local to the block.
• Lifetime: Ends when the block exits.
• Faster than auto as stored in CPU registers.
• Address cannot be obtained using pointers.
• Example:
void example()
Dept. of MCA {
Click to Edit
register int fast_var = 5; // Frequent usage variable
}
Storage classes
Storage Class Scope Lifetime Initialized With Key Feature
Dept. of MCA
Click to Edit static Local or Global
Till program
0 (default)
Retains value
termination across calls.
Module 3 – Arrays
Arrays
• An array is a collection of data items or similar elements of the
same type that are stored in contiguous memory locations.
Arrays are used in computer programming to organize data.
• An array is a data structure consisting of a collection of elements
(values or variables), of same memory size, each identified by at
least one array index or key. An array is stored such that the
position of each element can be computed from its index tuple by
Dept. of MCA
Click to Edit
a mathematical formula.
Declaring of Arrays
• An array must be declared before being used. Declaring an
array means specifying three thing:
Data type : int, char, float, double,…
Name : to identify the array
Size : maximum number of values that the array can hold
• Arrays are declared as follows :
Click to Edit
type name [size]
Eg. int marks[10];
• To declare and define an array, you must specify its name,
type and size.
• The number within brackets indicates the size of the array, i.e,
maximum number of elements that can be stored in array.
Declaring of Arrays
Dept. of MCA
Click to Edit