Activity: C Review & Functions

From the origins of C to user-defined functions and call by value vs reference

Grade XII • Computer Science ⏱️ ~20 min

Brief Intro — C Review & Functions

Instead of writing one giant block of code, programmers break complex problems into smaller, reusable pieces called functions. C is built on this modular approach, allowing you to "call" a specific task whenever you need it.

In this activity, you will explore how to define your own functions and how data is passed between them using values and references.

Part 1 C Language Review

C is a powerful system language that has shaped modern computing, from operating systems like UNIX to high-performance databases.

Interactive Simulator: C Language Fact Explorer

Click a fact cluster in the dock below to explore it on the screen above.

💻
Select a cluster from the dock

Task 1: Origins of C

Who developed C, and for what purpose?

Check Answer

Correct Answer: Dennis M. Ritchie at Bell Labs, to develop the UNIX operating system. C was first implemented on the DEC PDP-11 in 1972, and Kernighan and Ritchie (K&R) produced the first description in 1978.

Task 2: Facts about C

Select ALL the true statements about C.

Check Answer

Correct Answer: a, b, c and e are true. C is a successor of B; it was formalized by ANSI in 1988; UNIX was totally written in C; and it is the most widely used system programming language. Linux and MySQL are also written in C.

Task 3: Why use C?

Which reason best explains why C became a widely used professional language?

Check Answer

Correct Answer: a. C is easy to learn, structured, produces efficient programs, handles low-level activities, compiles on a variety of platforms, and runs nearly as fast as assembly.

Task 4: Uses of C

Match each example to the use-of-C category it belongs to.

UNIX and Linux
The C compiler itself
MySQL
Programs that control network hardware
Check Answer

Correct Answer: UNIX and Linux → Operating Systems; the C compiler → Language Compilers; MySQL → Databases; network hardware programs → Network Drivers. Other listed uses include Assemblers, Text Editors, Print Spoolers, Modern Programs, Language Interpreters, and Utilities.

Part 2 Functions: Definition, Declaration, Call

Functions follow a precise structure: you declare their presence, define their behavior, and finally call them to perform a task.

Task 5: What is a function?

A function is a group of statements that together perform a task. Every C program has at least one function — main(). What best describes a function?

Check Answer

Correct Answer: a. A function is a group of statements that together perform a task, and each function performs a specific task.

Task 6: Declaration vs Definition

The declaration tells the compiler about a function's name, return type, and parameters. The definition provides the actual body of the function. Which statement is correct?

Check Answer

Correct Answer: a. The C standard library also provides built-in functions like strcat() (concatenates two strings) and memcpy() (copies memory).

Task 7: Anatomy of max()

Trace this code: int a = 100; int b = 200; int ret; ret = max(a, b); printf("Max value is : %d", ret); What is printed?

int max(int num1, int num2) {
    int result;
    if (num1 > num2)
        result = num1;
    else
        result = num2;
    return result;
}
Check Answer

Correct Answer: a. The call ret = max(a, b); passes 100 and 200; max() returns the larger value, 200, which is stored in ret and printed as Max value is : 200.

Task 8: Control Flow

When a program calls a function, what happens to program control?

Check Answer

Correct Answer: a. The called function performs its task, then returns control to the main program via return or its closing brace.

Part 3 Arguments: Call by Value vs Reference

Deciding whether to pass data by value or by reference determines whether a function can modify the original variables in your main program.

Interactive Simulator: Call by Value vs Call by Reference Swapper

Step through the swap program. Watch what happens to m and n — and what happens only inside swap().

main()
void swap(int a, int b)
Call by value
Copies the actual values: a = 22, b = 44. The swap happens on the copies.
Inside swap(): m = 44 and n = 22 (the labels point to a, b).
Back in main(): m = 22 and n = 44 — originals UNCHANGED.
Call by reference (the idea)
Passes the addresses of m and n, so the function works on the originals.
Result: the caller's m and n DO change.
By default, C uses call by value.
The full call-by-reference syntax (& and *) needs pointers, which the notes defer to the Pointers activity — we show only the idea here.

Task 9: Formal vs Actual Parameters

The variables declared in the function definition are the formal parameters; the arguments used in the function call are the actual parameters. Which is correct?

Check Answer

Correct Answer: a. Formal parameters behave like local variables: they are created on entry into the function and destroyed on exit.

Task 10: Call by Value

In call by value, changes made to a parameter inside the function have what effect on the actual argument?

Check Answer

Correct Answer: a. Call by value copies the actual value into the formal parameter, so changes inside the function have no effect on the argument.

Task 11: Call by Reference

In call by reference, changes made to the parameter do what?

Check Answer

Correct Answer: a. Call by reference copies the address of the argument into the formal parameter; inside the function the address accesses the actual argument, so changes affect it. The notes defer the full syntax until after pointers are understood.

Task 12: Swap by Value — Trace

After swap(m, n) is called by value with m = 22, n = 44, what are the values of m and n in main()?

void swap(int a, int b) {
    int tmp;
    tmp = a;
    a = b;
    b = tmp;
}
Check Answer

Correct Answer: a. The swap happens on the copies a, b, so the originals stay 22 and 44. The program prints Values before swap m = 22 and n = 44; the copies print Values after swap m = 44 and n = 22.

Ready to test your knowledge?

Take the Assessment →