Programming Language & Its Applications — Introduction to C Programming, NEC licence examination syllabus (Nepal Engineering Council).
A function is a contract: given these inputs, produce this output.
A 500-line main() is unmaintainable and untestable. Functions let you name a piece of behaviour, test it independently, and reuse it. In C, the crucial thing to understand is the call by value rule — arguments are copied, so a function cannot modify the caller's variables unless you pass addresses. Missing that fact is the source of the most common beginner confusion.
#include <stdio.h>
void try_to_swap(int a, int b) { /* gets COPIES */
int t = a; a = b; b = t;
printf(" inside: a=%d b=%d\n", a, b);
}
void really_swap(int *a, int *b) { /* gets ADDRESSES */
int t = *a; *a = *b; *b = t;
}
int main(void) {
int x = 10, y = 20;
try_to_swap(x, y);
printf("after try: x=%d y=%d\n", x, y);
really_swap(&x, &y);
printf("after real: x=%d y=%d\n", x, y);
return 0;
}
Output:
inside: a=20 b=10
after try: x=10 y=20 <-- unchanged!
after real: x=20 y=10 <-- swapped
void f(int arr[]) looks like passing an array by value, but arrays decay to a pointer to their first element. So modifications inside f DO affect the caller's array — because you were passing an address all along, whether you realised it or not.
#include <stdio.h>
/* A function can return only one value, so extra results
go out through pointer parameters. */
int divmod(int a, int b, int *rem) {
if (b == 0) { *rem = 0; return 0; }
*rem = a % b;
return a / b;
}
/* min and max of an array, both returned via pointers */
void min_max(const int *arr, int n, int *mn, int *mx) {
*mn = *mx = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] < *mn) *mn = arr[i];
if (arr[i] > *mx) *mx = arr[i];
}
}
int main(void) {
int r;
int q = divmod(17, 5, &r);
printf("17/5 = %d remainder %d\n", q, r);
int data[] = {45, 12, 78, 3, 56};
int lo, hi;
min_max(data, 5, &lo, &hi);
printf("min=%d max=%d\n", lo, hi);
return 0;
}
Output:
17/5 = 3 remainder 2
min=3 max=78
#include <stdio.h>
int global = 100; /* file scope, whole program */
void demo(void) {
int local = 1; /* new each call, dies on exit */
static int persist = 1; /* created once, survives */
printf("local=%d persist=%d global=%d\n",
local, persist, global);
local++; persist++; global++;
}
int main(void) {
demo();
demo();
demo();
{ /* a nested block has its own scope */
int global = 999; /* SHADOWS the outer one */
printf("shadowed global = %d\n", global);
}
printf("real global = %d\n", global);
return 0;
}
Output:
local=1 persist=1 global=100
local=1 persist=2 global=101
local=1 persist=3 global=102
shadowed global = 999
real global = 103
#include <stdio.h>
#include <math.h>
double mean(const double *a, int n) {
double s = 0.0;
for (int i = 0; i < n; i++) s += a[i];
return s / n;
}
double variance(const double *a, int n) {
double m = mean(a, n), s = 0.0; /* reuses mean() */
for (int i = 0; i < n; i++) s += (a[i]-m) * (a[i]-m);
return s / n;
}
double std_dev(const double *a, int n) {
return sqrt(variance(a, n)); /* reuses variance() */
}
int main(void) {
double marks[] = {72, 85, 63, 91, 78};
int n = sizeof marks / sizeof marks[0]; /* = 5 */
printf("n = %d\n", n);
printf("mean = %.2f\n", mean(marks, n));
printf("variance = %.2f\n", variance(marks, n));
printf("std dev = %.2f\n", std_dev(marks, n));
return 0;
}
Output:
n = 5
mean = 77.80
variance = 96.16
std dev = 9.81
Note the sizeof arr / sizeof arr[0] idiom for array length — it works only where the array is declared, not inside a function that received it as a parameter (where it has decayed to a pointer).
Create a free account to tick topics off, take notes as you read, watch the video lessons and get a day-by-day study plan built around your exam date.
Loading…