Unit 3 Programming For Problem Solving
Unit 3 Programming For Problem Solving
Functions
NextGen Academy
Contents
1 Arrays
Single Dimensional Arrays
Two Dimensional Arrays
String and Character Arrays
2 Functions
Types of Functions
Function Declaration
Function Definition
Argument Passing
Advantages of Functions in C
Actual and Formal Arguments
Parameter Passing in Functions
Call by Value
Call by Reference
Passing Arrays to Functions
Nested Functions and Recursion
Arrays in C
Arrays in C
Advantages:
Array variables can store multiple values, unlike single variables that store only
one value.
Example: int arr[100]; int mark[100];
Declaration of Array:
Syntax: Data type array name [size];
Example: int arr[100]; int mark[100]; int a[5] = {10, 20,
30, 100, 5};
Initialization of Array:
Upon declaration, local array elements contain garbage values; global or static
arrays are automatically initialized with zero.
Single-Dimensional Arrays
Single-Dimensional Arrays
Elements can be of any valid data type (integers, characters, floats, etc.) based
on the array’s declared type.
Arrays can hold a fixed number of elements specified during declaration, and
the size cannot be changed during runtime.
Elements in the array can be initialized individually or collectively during
declaration.
Accessing array elements: Elements are accessed using their indices; for
example, arr[0], arr[1], etc.
Example of initializing array elements: int a[5] = {10, 20, 30, 40,
50};
A loop is commonly used to traverse through and process elements in a
single-dimensional array.
Accessing elements in a 2D array requires two indices: one for the row and
another for the column.
Array indexing starts from zero for both rows and columns.
Example declaration: int matrix[3][3]; - a 3x3 matrix.
Elements are accessed using two indices: matrix[i][j] represents the
element at the i-th row and j-th column.
Common operations on matrices involve nested loops to traverse rows and
columns for processing elements.
Matrices are used extensively in mathematical operations, graphics, and other
data manipulation tasks.
String Initialization
Representation: J o h N ‘\0‘
The terminating NULL (’\0’) is crucial for functions working with strings to
identify the string’s end.
String can also be initialized as: char name[] = "John";
The NULL character is not necessary in this case, as the compiler assumes it
automatically.
strlen()
strcmp()
strcpy()
strcat()
Contents
1 Arrays
Single Dimensional Arrays
Two Dimensional Arrays
String and Character Arrays
2 Functions
Types of Functions
Function Declaration
Function Definition
Argument Passing
Advantages of Functions in C
Actual and Formal Arguments
Parameter Passing in Functions
Call by Value
Call by Reference
Passing Arrays to Functions
Nested Functions and Recursion
Function Definition
Function Characteristics
Function Invocation
Library Function
Function Declaration
Function Declaration:
It informs the compiler about three things:
The name of the function
Number and type of arguments received by the function
Type of value returned by the function
The name of the argument is optional
The function prototype is always terminated by a semicolon
Example:
int function(int, int, int); /* Function declaration */
Calling Function (in main()):
function(arg1, arg2, arg3);
Function Definition:
int function(type1 arg1, type2 arg2, type3 arg3) /* Function
definition */
{
Local variable declaration;
Statement;
return value;
}
Dr. Vishwa Pratap Singh Unit 3: Arrays & Function 22 / 34
Types of Functions
Function Declaration
Function Definition
Argument Passing
Arrays
Advantages of Functions in C
Functions
Actual and Formal Arguments
Parameter Passing in Functions
Passing Arrays to Functions
Nested Functions and Recursion
Function Definition
Function Definition:
Function definition consists of the complete description and code of the function.
It specifies what the function does, its inputs, and its output.
Syntax:
return type function(type1 arg1, type2 arg2, type3 arg3) /*
Function header */
{
Local variable declaration;
Statement 1;
Statement 2;
return value;
}
Function Calls:
Function(x);
Function(20, 30);
Function(a*b, c*d);
Function(2, 3, sum(a, b));
Advantages of Functions in C
Reusability:
Functions enable calling the same code multiple times, saving developer time and
effort.
Code can be reused in different parts of the program, enhancing efficiency.
Modular Programming:
Functions facilitate breaking down complex problems into smaller, manageable
pieces.
Enables the division of a program into smaller parts, easing development and
debugging.
Efficiency:
Breaking a program into functions allows for easier optimization of code for speed
and memory usage.
Optimized functions can result in faster program execution and reduced memory
consumption.
Actual Parameters
Definition:
Actual arguments are the values passed during a function call.
Defined in the calling function.
These values are the variables or expressions referenced in the parameter list of the
function call.
Characteristics:
No need to specify datatype for actual parameters.
Represented by variables or expressions used in the function call.
Formal Parameters
Definition:
Formal parameters are variables or expressions listed in the parameter list of a
subprogram specification (function or procedure).
The datatype of the receiving value must be defined.
Scope of formal arguments is local to the function definition where they are used.
Characteristics:
Must define the datatype for the receiving value.
Scope is restricted to the function or subprogram where they are defined.
Call by Value
Methodology:
Copies of actual parameter values are passed to formal parameters.
The called function uses these formal parameters for execution.
Effect on Parameters:
Changes made to formal parameters do not affect actual parameter values.
Upon returning to the calling function, actual parameter values remain unchanged.
Call by Reference
Methodology:
Memory location addresses of actual parameters are passed to formal parameters.
Formal parameters are pointers used to access actual parameter memory locations.
Effect on Parameters:
Changes made to formal parameters affect the values of actual parameters.
Using formal parameters directly accesses and modifies actual parameter values.
Requirement:
Formal parameters must be pointer variables in this method.
Array Passing:
In C, the entire array cannot be directly passed as an argument to a function.
However, a pointer to the array’s first element can be passed by specifying the
array’s name.
Passing Methods:
Syntax in function definition:
return type foo(array type array name[size], ...);
return type foo(array type array name[], ...);
return type foo(array type* array name, ...);
Array is passed as a pointer even when declared as an array in the function
argument.
Array Decay:
Passing an array to a function leads to array decay, losing size information.
Size or number of elements in the array can’t be determined within the function.
Nested Functions
Definition:
Nested functions refer to defining a function within another function.
The inner function has access to the variables of the outer function.
Often used for encapsulation and to organize code logically.
Characteristics:
Encapsulation: Inner functions have access to the outer function’s variables.
Local Scope: Inner functions are only accessible within the scope of the outer
function.
Clarity and Modularity: Helps in breaking down complex logic into smaller,
manageable parts.
Recursion
Definition:
Recursion refers to the process where a function calls itself directly or indirectly.
Used to solve problems by dividing them into smaller, similar subproblems.
Involves a base case to terminate the recursive calls.
Characteristics:
Divides Problems: Breaks down complex problems into simpler ones.
Base Case: Essential to prevent infinite recursion and end the recursive calls.
Requires Extra Memory: Each recursive call adds to the function call stack.