0% found this document useful (0 votes)
14 views35 pages

Module 3 (POP) T

Module 3 covers key programming concepts in C, focusing on functions and arrays. It explains the importance of functions for program organization, detailing their declaration, definition, and calling mechanisms, along with parameter passing methods. Additionally, it discusses variable scope and storage classes, emphasizing how they affect variable accessibility and memory management.

Uploaded by

aimlhod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views35 pages

Module 3 (POP) T

Module 3 covers key programming concepts in C, focusing on functions and arrays. It explains the importance of functions for program organization, detailing their declaration, definition, and calling mechanisms, along with parameter passing methods. Additionally, it discusses variable scope and storage classes, emphasizing how they affect variable accessibility and memory management.

Uploaded by

aimlhod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Module 3

Contents
Chapter 11: Functions
 Introduction using functions, Function definition,
function declaration, function call, return statement, passing parameters
functions, scope of variables, storage classes, recursive functions
Chapter 12: Arrays
 Declaration of arrays, Accessing elements of an array,
storing values in array, Operations on array,
Passing arrays to functions, 2D array, operations on 2D array,
2D arrays to functions, multidimensional arrays, application of arrays
Chapter 11:Functions
Introduction:
 C enables programmers to break up a program into segments commonly known
as functions
 Every function in the program is supposed to perform a well-defined task. Therefore, the
code of one function is completely insulated from the other functions.
 Another point is that it is not only the main() function that can call other functions.
A function can call any other function.
Why are functions needed?
 The reasons for segmenting a program into functions
 • Dividing a program into separate well-defined functions facilitates each function to be written
and tested separately. This simplifies the process of getting the total program to work. Figure
11.3 shows that the main() function calls other functions for dividing the entire code into smaller
sections (or functions). This approach is referred to as the top-down approach.
 • Understanding, coding, and testing multiple separate functions is far easier than doing it for
one big function.
 • If a big program has to be developed without the use of any function other than the main()
function, then there will be countless lines in the main() function and maintaining this program
will be very difficult. A large program size is a serious issue in micro-computers where memory
space is limited.
 • All the libraries in C contain a set of functions that the programmers are free to use in their
programs.
 .When a big program is broken into comparatively smaller functions, then different programmers
working on that project can divide the workload by writing different functions.
 • Like C libraries, programmers can also write their functions and use them at different points
in the main program or in any other program that needs its functionalities.
Why are functions needed?
Using Functions
While using functions we will be using the following terminologies:
 A function f that uses another function g is known as the calling function and
g is known as the called function.
 The inputs that a function takes are known as arguments/ parameters.
 When a called function returns some result back to the calling function, it is
said to return that result.
 The calling function may or may not pass parameters to the called function. If
the called function accepts arguments, the calling function will pass
parameters, else it will not do so.
 Function declaration is a declaration statement that identifies a function with
its name, a list of arguments that it accepts, and the type of data it returns.
 Function definition consists of a function header that identifies the function,
followed by the body of the function containing the executable code for that
function.
Function declaration/Function prototype
 Before using a function, the compiler must know about the number of
parameters and the type of parameters that
1.the function expects to receive
2.the data type of the value that it will return to the calling function.

 The general format for declaring a function that accepts some arguments
and returns some value as a result can be given as
return_data_type function_name(data_type variable1, data_type
variable2,...);

 Naming a function follows the same rules as naming variables.

 A function should have a meaningful name that must specify the task that
the function will perform. The function name is used to call it for
execution in a program.
Function declaration/Function prototype
 Every function must have a different name that indicates the particular job
that the function does.
 return_data_type specifies the data type of the value that will be returned to
the calling function
 data_type variable1, data_type variable2, ...is a list of variables of specified
data types. These variables are passed from the calling function to the called
function.
 They are also known as arguments or parameters that the called function
accepts to perform its task. Table 11.1 shows examples of valid function
declarations in C.
Function declaration/Function prototype
 Things to remember about function declaration:
• After the declaration of every function, there should be a semicolon: If the
semicolon is missing, the compiler will generate an error message.
The function declaration is global. Therefore, the declared function can be called from
any point in the program.
• Use of argument names in the function declaration statement is optional. So both
declaration statements are the particular job that the function does valid in C.
int func(int, char, float);
int func (int num, char ch, float fnum);
• A function cannot be declared within the body of another function.
• A function having void as its return type cannot return any value.
• A function having void as its parameter list cannot accept any value. So the
function declared as

void print(void);
Or
void print();
does not accept any input/arguments from the calling function.
Function declaration/Function prototype

 • If the function declaration does not specify any return type, then by default, the
function returns an integer value. Therefore, when a function is declared as
sum(int a, int b);
 Then the function sum accepts two integer values from the calling function and in turn
returns an integer value to the caller.
 Some compilers make it compulsory to declare the function before its usage while
other compilers make it optional. However, it is a good practice to always declare the
function before its use as it allows error checking on the arguments in the function call.
Function definition
 When a function is defined, space is allocated for that function in the memory.

 A function definition comprises 2 parts


Function header
Function body
 The syntax of a function definition can be given as:

return_data_type function_name (data type variable1, data_type variable2,...)


{
……….
Stmnts
return variable();
}
Function definition
 The number of arguments and the order of arguments in the function header
must be same as that given in the function declaration statement.
 While return data type function_name (data type variablel,data_type
variable2,..) is known as the function header, the rest of the portion comprising
of program statements within { } is the function body which contains the code
to perform the specific task.
 The function header is same as function declaration.
 The only difference between the two is that a function header is not followed
by a semicolon. The list of variables in the function header is known as the
formal parameter list. The parameter list may have zero or more parameters of
any data type. The function body contains instructions to perform the desired
computation in a function.
Function call
 The function call statement invokes the function.
 When a function is called, the compiler jumps to the called function to execute
the statements that are part of that function. Once the called function is
executed, the program control passes back to the calling function.

 Function call statement has the following syntax:


function name(variable, variable2, ...);

 When the function declaration is present before the function call, the compiler
can check if the correct number and type of arguments are used in the
function call and the returned value, if any, is being used reasonably.
Points to Remember While Calling
Functions
The following points are to be kept in mind while calling functions:
 Function name and the number and type of arguments in
the function call must be same as given in function declaration and function header of
the function definition.
 If parameter passed is less than what is specified to accept, then unmatched argument
will be initialized to some garbage value
 Parameter list must be separated with commas
 Arguments may be passed in the form of expressions to the called function. In such
cases, arguments are first evaluated and converted to the type of formal parameter and
then the body of the function gets executed.
 The parameter list must be separated with commas.
Return statement

 The return statement terminates 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
 when the return statement is encountered. A return statement may or may not
return a value o the calling function. The syntax of return statement co be
given as
return ‹expression›;
 Here expression is placed in between angular bracket because specifying an
expression is optional. The value or expression, if present, is returned to the
calling function.
 However, in case expression is omitted, the return value of the function is
undefined.
Return Statement
 The expression, if present, is converted to the type returned by the function. A
function that has void as it return type cannot return any value to the calling
function.
 So in a function that has been declared with return type void, a return
statement containing an expression generates a warning and the expression is
not evaluated.
 For functions that have no return statement, the control automatically returns
to the calling function after the last statement of the called function is
executed. In other words an implicit return takes place upon execution of the
last statement of the called function, and control automatically returns to the
calling function
Passing parameters functions
 When a function is called, the calling function may have to pass some values
to the called function.
 There are two ways in which arguments or parameters can be passed to the
called function. They include:
• Call by value in which values of variables are passed by the calling
function to the called function.

• Call by reference in which address of variables are passed by the


calling function to the called function.
Passing parameters functions
 1.Call by Value
 Till now, we had been calling functions and passing arguments to them
using call-by-value method.
 In the call-by-value method, the called function creates new variables to
store the value of the arguments passed to it. Therefore, the called
function uses a copy of the actual arguments to perform its intended
task.
 If the called function is supposed to modify the value of the parameters
passed to it, then the change will be reflected only in the called
function. In the calling function no change will be made to the variables
and not to the actual variables.
Passing parameters functions
 To understand this concept, consider the code given below. The add() function
accepts an integer variable num and adds 10 to it.
 In the calling function, the value of num = 2. In add (), the value of num is
modified to 12 but in the calling function the change is not reflected.
Passing parameters functions
Advantages:
 Argument can be variables, literals, or expressions
Disadvantage:
 Copying data consumes additional storage spaces.
 It can take a lot of time to copy , resulting in performance penalty, especially when
function is called many times.
Passing parameters functions
 2.Call by Reference
 when a function wants to modify the value of the argument is to pass
arguments using call-by-reference technique.
 In call by reference, we declare the function parameters as references rather
than normal variables. When this is done any changes made by the function to
the arguments it receives are visible in the calling function.
 To indicate that an argument is passed using call by reference, an asterisk (*) is
placed after the type in the parameter list. This way, changes made to the
parameter in the called function will then be reflected in the calling function.
 Hence, in call-by-reference method, a function receives an implicit reference
to the argument, rather than a copy of its value. Therefore, the function can
modify the value of the variable and that change will be reflected in the calling
function as well. The following program uses this concept.
Passing parameters functions
Passing parameters functions

Advantages
The advantages of using the call-by-reference technique of passing arguments are as follows:
 Since arguments are not copied into new variables, it provides greater time and space efficiency.
 The called function can change the value of the argument and the change is reflected in the
calling function.
 A return statement can return only one value. In case we need to return multiple values, pass
those arguments by reference.
Disadvantages
 the side-effect of using this technique is that when an argument is passed using call by address,
it becomes difficult to tell whether that argument is meant for input, output, or both.
SCOPE OF VARIABLES

 In C, all constants and variables have a defined scope.


 By scope we mean the accessibility and visibility of the variables at different
points in the program.
 A variable or a constant in C has four types of scope: block, function,
program, and file.
 1. Block Scope
 We have studied that a statement block is a group of statements enclosed
within opening and closing curly brackets { }.
 If a variable is declared within a statement block then as soon as the control
exits that block, the variable will cease to exist.
 Such a variable also known as a local variable is said to have a block scope.
SCOPE OF VARIABLES
SCOPE OF VARIABLES
2.Function Scope
 Function scope indicates that a variable is active and visible from the
beginning to the end of a function.
 In C, only the goto label has function scope. In other words, function scope is
applicable only with goto label names.
 This means that the programmer cannot have the same label names inside a
function. Using goto statements is not recommended as it is not considered to
be a good programming practice.
 In this example, the label loop is visible from the beginning to the end of the
main() function. Therefore, there should not be more than one label having the
same name within the main() function.
SCOPE OF VARIABLES
SCOPE OF VARIABLES
3. Program Scope
 Till now we have studied that variables declared within a function are local variables. These local
variables (also known as internal variables) are automatically created when they are declared in
the function and are usable only within that function.
 The local variables are unknown to other functions in the program. Such variables cease to exist
after the function in which they are declared is exited and are re-created each time the function is
called.
 However, if you Want a function to access some variables which are not passed to it as.
arguments, then declare those variables outside any function blocks. Such variables local are
commonly known as global variables and can be accessed from any point in the program.

Lifetime Global variables are created at the beginning of program execution and remain in
existence throughout the period of execution of the program.
 These variables are known to all the functions in the program and are accessible to them for
usage. Global variables are not limited to a particular function so they exist even when a function
calls another function.
 These variables retain their value so that they can be used from every function in the program
SCOPE OF VARIABLES
4.File Scope
 When a global variable is accessible until the end of the file, the variable is said
to have file scope.
 To allow a variable to have file scope, declare that variable with the static
keyword before specifying its data type:
static int x = 10;
 A global static variable can be used anywhere from the file in which it is
declared but it is not accessible by any other file. Such variables are useful when
the programmer writes his own header files.
storage classes
 The storage class in the C language for determining the visibility,
lifetime, initial value, and memory location of any given variable.
 There are four types of storage classes in C language: auto, register,
static, and extern, each serving a specific purpose in memory
management and data accessibility.
 You can think of storage classes like employee roles in a company.
Some employees work temporarily (auto), some retain their tasks
daily (static), some work at the front desk for quick access (register),
and some share resources across teams (extern).
storage classes
There are 4 types of storage classes in C programming:
 auto for temporary local variables.
 register for high-speed variables stored in CPU registers.
 static for persistent local or global variables.
 extern for sharing global variables across multiple files.
Auto storage classes
Auto Storage Classes in C
 The auto storage class in C is the default storage class for local variables.
 Scope: Block (local to function).
 Lifetime: Created when function is called, destroyed when function exits.
 Memory Location: RAM.

Note: Auto variables are temporary and exist only within their function.
Register storage classes
The register storage class in C suggests that the variable should be stored in CPU registers for
faster access.
 Scope: Block (local to function).
 Lifetime: Created when function is called, destroyed when function exits.
 Memory Location: CPU register (if available, otherwise RAM).

Note: Register variables improve performance for frequently used variables (like loop
counters).
Static storage classes
The static storage class in C retains value between function calls.
 Scope: Local (if used inside a function), Global (if declared outside any function).
 Lifetime: Exists throughout program execution.
 Memory Location: RAM.
Extern storage classes
 The extern storage class in C is used to share global variables across multiple files.
 Scope: Global (across files).
 Lifetime: Exists throughout program execution.
 Memory Location: RAM.
Example (Multiple Files - extern.c and main.c)
File 1: extern.c (Variable Definition) File 2: main.c (Using extern to
Access the Variable)

You might also like