Unit 3: Functions and arrays
Problem Solving Techniques notes · PTU syllabus (UGSEC2501)
On this page
Unit summary
As programs grow, they must be split into smaller pieces. Functions let you write a task once and use it many times; arrays let you store many values of the same type under one name. This unit also covers strings and the storage classes that decide a variable's lifetime and scope.
After this unit you can
- Define, declare (prototype) and call functions with arguments
- Explain call by value and write recursive functions
- Declare and process one- and two-dimensional arrays and pass them to functions
- Handle strings and explain automatic, external and static storage classes
PTU syllabus topics
- Defining and accessing functions
- passing arguments
- argument data types
- function prototypes
- recursion
- defining and processing arrays
- passing arrays to a function
- multi-dimensional arrays
- string declaration and manipulation
- storage classes (automatic, external, static)
What is passed
A copy of the value
The address (pointer) of the variable
Original variable
Unchanged
Can be modified
Memory
Extra copy created
No copy of the data
C example
swap(a, b) fails to swap
swap(&a, &b) works
Topic 1
Defining and accessing functions
A function is a self-contained block of code that performs a specific task. Functions reduce repetition, make programs easier to read and debug, and allow reuse.
- Function prototype (declaration): tells the compiler the name, return type and parameter types:
int add(int, int); - Function definition: the actual body.
- Function call: runs it:
s = add(4, 5);
c#include <stdio.h>
int add(int a, int b); /* prototype */
int main() {
printf("%d", add(4, 5)); /* call: prints 9 */
return 0;
}
int add(int a, int b) { /* definition */
return a + b;
}Topic 2
Passing arguments
- Actual arguments are the values in the call; formal arguments are the parameters in the definition.
- In C, arguments are passed by value: the function gets a copy, so changes inside it do not affect the original variable.
- To change the caller's variable, pass its address (call by reference using pointers), as in a swap function.
What is passed
A copy of the value
The address of the variable
Effect on original
Unchanged
Can be changed
Syntax
swap(a, b)
swap(&a, &b) with pointer parameters
Topic 3
Recursion
Recursion is when a function calls itself. Every recursive function needs a base case that stops the recursion and a recursive case that moves towards it.
cint fact(int n) {
if (n <= 1) return 1; /* base case */
return n * fact(n - 1); /* recursive case */
}Example
fact(4) = 4 × fact(3) = 4 × 3 × fact(2) = 4 × 3 × 2 × fact(1) = 24.
Exam tip
Without a base case recursion never stops and the program crashes with a stack overflow.
Topic 4
Arrays
An array is a collection of elements of the same type stored in consecutive memory locations under one name. Elements are accessed by an index starting from 0.
- One-dimensional:
int marks[5] = {70, 82, 65, 90, 77};— marks[0] is 70. - Two-dimensional (matrix):
int a[3][3];— a[i][j] is row i, column j. - Passing an array to a function passes the address of its first element, so the function works on the original array:
void display(int a[], int n);
c/* Largest element of an array */
int max = a[0];
for (int i = 1; i < n; i++)
if (a[i] > max) max = a[i];Topic 5
Strings
A string is an array of characters ending with the null character '\0'. char name[10] = "SBS"; uses 4 bytes ('S', 'B', 'S', '\0').
| Function (string.h) | Purpose |
|---|---|
| strlen(s) | Length, not counting '\0' |
| strcpy(d, s) | Copies s into d |
| strcat(d, s) | Joins s to the end of d |
| strcmp(a, b) | Compares: 0 if equal |
| strrev(s) | Reverses (compiler-specific) |
Topic 6
Storage classes
A storage class decides where a variable is stored, its default value, its scope (where it can be used) and its lifetime (how long it exists).
| Storage class | Default value | Scope | Lifetime |
|---|---|---|---|
| auto | Garbage | Inside the block | Until the block ends |
| extern | Zero | Whole program (global) | Whole program |
| static | Zero | Inside the block (or file) | Whole program; keeps its value between calls |
| register | Garbage | Inside the block | Until the block ends; stored in a CPU register if possible |
Example
A static counter inside a function keeps counting across calls: the first call prints 1, the second 2, and so on, because it is not re-created each time.
Key terms
- Function prototype
- A declaration giving a function's name, return type and parameters
- Recursion
- A function calling itself
- Base case
- The condition that stops recursion
- Array
- A collection of same-type elements in consecutive memory
- String
- A character array ending with '\0'
- Storage class
- Defines a variable's storage, scope, lifetime and default value
Quick revision
- Prototype, definition, call — three parts of using a function.
- C passes by value; use pointers to change the caller's variable.
- Every recursion needs a base case.
- Array index starts at 0; arrays are passed by address.
- Strings end with '\0'; static variables keep their value between calls.
Important exam questions
Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).
Short-answer questions
- Q1.What is a function prototype?
- Q2.Differentiate between actual and formal arguments.
- Q3.What is recursion? Write the base case for factorial.
- Q4.How is an array passed to a function?
- Q5.Name four string functions with their use.
- Q6.Differentiate between auto and static variables.
Long-answer questions
- Q1.Explain functions in C: definition, declaration, calling and types of arguments, with an example.
- Q2.Write a recursive program to print the Fibonacci series and explain how recursion works.
- Q3.Write a program to add two matrices using two-dimensional arrays.
- Q4.Explain the four storage classes of C with examples.
Stuck on this unit?
Message SBS on WhatsApp for help with Problem Solving Techniques, or to ask about studying BCA at Synetic.
