f(x)
Plus Two Computer Applications (Commerce) · Chapter 03

Functions

5 sections
Interactive function workbench
15 review questions
3.1

Concept of Modular Programming

3.1

Why Break a Program Into Modules?

Consider a large software such as a school management system. It has to handle admissions, evaluation, accounts and staff management — far too much for one giant, tangled program. Instead, the bigger task is split into smaller tasks, or modules, that can be built in parallel and later combined into the finished software.

This style of building software — solving a big problem by writing separate programs for its smaller sub-problems — is called modular programming. Each sub-task becomes a module, and the process of splitting a large program into smaller ones is known as modularization. Different languages implement it differently; in C++, modularization is achieved through functions.

3.1.1

Merits of Modular Programming

Splitting a program into modules pays off in several ways:

  • Reduces the size of the program — when the same set of instructions is needed at several points (for example, evaluating (x5+y7)/(√x+√y) for different values), that logic can be written once and invoked by name wherever it is needed, instead of being repeated.
  • Less chance of error — a smaller program has fewer syntax slips, and because each module can be reasoned about on its own, logical errors are easier to spot and fix.
  • Reduces programming complexity — dividing the problem into smaller conceptual units makes designing the overall logic simpler.
  • Improves re-usability — a function written once can be reused in other programs later, saving development time instead of starting from scratch.
3.1.2

Demerits of Modular Programming

The approach is not entirely free of challenges. Deciding exactly how to break a problem into independent sub-problems is itself a demanding design task — each module needs to stand on its own, and care must be taken while deciding the order in which the modules should execute.

3.2

Functions in C++

3.2

What Exactly Is a Function?

Think of a coffee-vending machine. Water, milk, sugar and coffee powder go in; the machine boils the mixture according to its stored instructions and coffee comes out, collected in a cup. If the invocation button is named MakeCoffee, we could symbolically write:

Cup = MakeCoffee (Water, Milk, Sugar, Coffee Powder)

MakeCoffee is the name of the function; water, milk, sugar and coffee powder are its parameters; and the coffee it hands back is the return value, stored in the cup. A C++ function works the same way — it accepts parameters, processes them, and returns a result. An Add function that takes 3, 5, 2 and 6, adds them, and stores the answer in C could be written:

C = Add (3, 5, 2, 6)

So a function is a named unit of statements in a program that performs a specific task as part of the overall solution. Not every function needs parameters, and not every function returns a value.

C++ ships with a large collection of ready-made functions whose logic has already been written, debugged and compiled — their definitions live in files called header files. These are called predefined functions or built-in functions. When the built-in collection is not enough for a particular task, C++ lets the programmer create their own — these are the user-defined functions, where the task, name and required data are all decided by the programmer.

Even main() fits this picture — it can be considered user-defined since its task is written by the user, but it plays an essential role because program execution always begins in main(). Without it, a C++ program will not run. Every other function only executes when it is called (invoked) from somewhere in a statement.

3.3

Predefined Functions

3.3

Parameters, Arguments and Return Values

Many predefined functions need data to work with — this data is called parameters or arguments, supplied inside the parentheses of the function name. Some functions hand back a result after performing their task, known as the function's return-value; others simply carry out a task without returning anything. The header file relevant to a group of functions must always be included before those functions are used.

3.3.1

Console Functions for Character I/O

C++ also offers functions purely for single-character input/output, declared in the header file cstdio (stdio.h in Turbo C++).

getchar() returns the character typed at the keyboard, which can be stored in a variable:

char ch = getchar();

putchar() displays the character passed as its argument on the monitor. The argument may be a character constant, a variable, or even an integer treated as an ASCII value:

char ch = 'B'; //assigns 'B' to the variable ch putchar(ch); //displays 'B' on the screen putchar('c'); //displays 'c' on the screen putchar(97); //displays 'a' on the screen

Program 3.1 uses both functions together to search for a character within a string and count its occurrences.

// Program 3.1: To search for a character in a string using console functions #include <iostream> #include <cstdio> using namespace std; int main() { char str[20], ch; int i, num=0; puts("Enter a string:"); //To print '\n' after the string gets(str);//To accept a string with white spaces cout<<"\nEnter the character to be searched: "; ch=getchar();//To input the character to be searched /* A loop to search for the character and count its occurrences in the string. Search will be terminated when null character is found */ for(i=0; str[i]!='\0'; i++) if (str[i]==ch) num++; cout<<"\nThe number of occurences of the character "; putchar(ch); cout<<" is : "<<num; return 0; }

Sample run — input string examination, searched character a → output: The number of occurences of the character a is : 2

3.3.2

Stream Functions for I/O Operations

C++ also provides stream functions, available through the header iostream, that let a stream of bytes flow between memory and objects such as the keyboard (cin) and the monitor (cout).

A. Input functions — get() and getline(), invoked as cin.get() and cin.getline() using the dot operator.

cin.get(ch) → reads one character into chcin.get(str, 10) → reads a string of up to 10 characters
char ch, str[10]; ch=cin.get(ch); //accepts a character and stores in ch cin.get(ch); //equivalent to the above statement cin.get(str,10); //accepts a string of maximum 10 characters

getline() accepts a string until Enter, a character count, or a specified delimiter character, whichever comes first — it can be called with two or three arguments:

char ch, str[10]; int len; cin.getline(str,len); // With 2 arguments cin.getline(str,len,ch); // With 3 arguments

With two arguments, up to len-1 characters are stored (a byte is reserved for the string terminator). With three arguments, an extra delimiting character can be supplied — whichever limit is hit first (character count or delimiter) stops the read.

B. Output functions — put() and write(), invoked with cout.

char ch='c'; cout.put(ch); //character 'c' is displayed cout.put('B'); //character 'B' is printed cout.put(65); //character 'A' is printed

write() displays a string, printing exactly as many characters as its second argument specifies (padding with spaces if the string is shorter):

char str[10]="hello"; cout.write(str,10); // displays "hello" followed by 5 spaces

Program 3.2 demonstrates all four stream functions together.

// Program 3.2: To illustrate the working of stream input/output functions #include <iostream> #include <cstring> //To use strlen() function using namespace std; int main() { char ch, str[20]; cout<<"Enter a character: "; cin.get(ch); //To input a character to the variable ch cout<<"Enter a string: "; cin.getline(str,10,'.'); //To input the string cout<<"Entered character is:\t"; cout.put(ch); //To display the character cout.write("\nEntered string is:",20); cout.write(str,strlen(str)); return 0; }

With input p then hello world, the output shows only hello wo — the string was capped at 9 stored characters, and the leftover Enter key from the earlier get() is consumed as the first character. With input hello.world, only hello is stored, since getline() stops at the delimiting dot.

3.3.3

String Functions

String-manipulation functions live in the header cstring (string.h in Turbo C++).

strlen(string) returns the number of characters in a string:

char str[] = "Welcome"; int n; n = strlen(str); // n becomes 7 n = strlen("Computer"); // n becomes 8; a string constant works too

strcpy(string1, string2) copies string2 into string1:

char s1[10], s2[10] = "Welcome"; strcpy(s1,s2); // s1 now holds "Welcome" strcpy(str,"Welcome"); // second argument can be a string constant

Note that str = "Welcome"; is invalid — a character array can only be assigned a string literal directly at the point of declaration, e.g. char str[10] = "Welcome";

strcat(string1, string2) appends string2 onto the end of string1 — the first argument must be large enough to hold both strings combined:

char s1[20] = "Welcome", s2[10] = " to C++"; strcat(s1,s2); cout << s1; // displays "Welcome to C++"

strcmp(string1, string2) compares two strings by alphabetical order and returns:

  • 0 if the two strings are identical
  • a negative value if string1 is alphabetically lower than string2
  • a positive value if string1 is alphabetically higher than string2
char s1[]="Deepthi", s2[]="Divya"; int n; n = strcmp(s1,s2); // n is negative, so "s1 < s2"

strcmpi(string1, string2) works exactly like strcmp() but ignores case, treating uppercase and lowercase letters as equal:

char s1[]="SANIL", s2[]="sanil"; int n = strcmpi(s1,s2); // n is 0 → "strings are same"

Program 3.3 compares two strings and, if different, concatenates and measures them.

// Program 3.3: To combine two strings if they are different and find its length #include <iostream> #include <cstring> // header file essential for string functions using namespace std; int main() { char s1[15], s2[15], s3[30]; cout<<"Enter two strings: "; cin>>s1>>s2; int n; n=strcmp(s1,s2); if (n==0) cout<<"\nThe input strings are same"; else { cout<<"\nThe input strings are not same"; strcpy(s3,s1);//Copies the string in s1 into s3 strcat(s3,s2);//Appends the string in s2 to that in s3 cout<<"String after concatenation is: "<<s3; cout<<"\nLength of the new string is: " <<strlen(s3); } return 0; }

Sample run — input india and kerala → not same; concatenation gives indiakerala, length 11.

3.3.4

Mathematical Functions

Common mathematical functions are declared in the header cmath (math.h in Turbo C++).

abs(int) returns the absolute value of an integer (positive or negative). For floating-point numbers, use fabs() instead, which returns a floating-point result.

int n = -25; cout << abs(n); // displays 25

sqrt(double) returns the non-negative square root of its argument (which may be int, float or double):

int n = 25; float b = sqrt(n); cout << b; // displays 5

pow(double, double) returns xy for arguments x and y:

int x = 5, y = 4, z; z = pow(x, y); cout << z; // displays 625, since 5⁴ = 5×5×5×5

Program 3.4 uses sqrt() (via Heron's formula) and pow() to find the area of a triangle and a circle.

// Program 3.4: To find the area of a triangle and a circle using mathematical functions #include <iostream> #include <cmath> // header file essential for mathematical functions using namespace std; int main() { const float pi=22.0/7; int a,b,c, radius; float s, area1, area2; cout<<"Enter the three sides of the triangle: "; cin>>a>>b>>c; s = (a+b+c)/2.0; area1 = sqrt(s*(s-a)*(s-b)*(s-c)); cout<<"The Area of the Triangle is: "<<area1; cout<<"\nEnter the radius of the circle: "; cin>>radius; area2 = pi*pow(radius,2); cout<<"Area of the Circle is: "<<area2; return 0; }

Sample run — sides 5, 7, 9 → triangle area 17.4123; radius 2.5 → circle area 12.5714

3.3.5

Character Functions

Character-testing and conversion functions are declared in the header cctype (ctype.h in Turbo C++). Each testing function returns 1 if the condition holds and 0 otherwise.

FunctionSyntaxChecks whether the character is…
isupper()int isupper(char c)an uppercase letter
islower()int islower(char c)a lowercase letter
isalpha()int isalpha(char c)any alphabet (upper or lower)
isdigit()int isdigit(char c)a digit (0–9)
isalnum()int isalnum(char c)alphanumeric (a letter or a digit)

toupper(char c) and tolower(char c) convert a character to its uppercase or lowercase form respectively, returning the character unchanged if it is already in that case (or is not a letter at all):

char c = toupper('a'); // c becomes 'A' cout << (char)toupper('A'); // still 'A' — (char) cast avoids printing the ASCII code char x = 'a'; char d = tolower(x); // d stays 'a'

Program 3.5 scans a whole line of text and separately counts uppercase letters, lowercase letters and digits, then prints the string entirely in uppercase and entirely in lowercase.

// Program 3.5: To count different types of characters in the given string #include <iostream> #include <cstdio> #include <cctype> using namespace std; int main() { char text[80]; int Ucase=0, Lcase=0, Digit=0; cout << "Enter a line of text: "; gets(text); for(int i=0; text[i]!='\0'; i++) // loop stops at the null character if (isupper(text[i])) Ucase++; else if (islower(text[i])) Lcase++; else if (isdigit(text[i])) Digit++; cout << "\nNo. of uppercase letters = " << Ucase; cout << "\nNo. of lowercase letters = " << Lcase; cout << "\nNo. of digits = " << Digit; cout << "\nThe string in uppercase form is\n"; i=0; while (text[i]!='\0') { putchar(toupper(text[i])); i++; } cout << "\nThe string in lowercase form is\n"; i=0; do { putchar(tolower(text[i])); // using cout<< instead of putchar() here would print ASCII codes i++; }while(text[i]!='\0'); return 0; }

Sample run — input The vehicle ID is KL01 AB101 → 7 uppercase letters, 11 lowercase letters, 5 digits.

3.4

User-defined Functions

3.4

Anatomy of a Function Definition

Every program we have written so far contains main() — itself a function. Its header, int main(), is called the function's header (or heading), and the statements inside the following pair of braces form its body. The general syntax of a function definition is:

data_type function_name(argument_list) { statements in the body; }

data_type is any valid C++ type; function_name is a programmer-chosen identifier; and the optional argument_list is a comma-separated list of parameters, each preceded by its data type. Designing a function means answering a few questions: which data type should head the function, how many arguments it needs and of what type, what its body should do, and what (if anything) it should return.

3.4.1

Creating User-defined Functions

A minimal function that simply displays a message, with no arguments and no return value:

void saywelcome() { cout<<"Welcome to the world of functions"; }

Its return type is void and it takes no arguments. Consider four different ways of writing a function that finds the sum of two numbers — they differ in how they take input and whether they return a value.

Function 1

void sum1() { int a, b, s; cout<<"Enter 2 numbers: "; cin>>a>>b; s=a+b; cout<<"Sum="<<s; }

Function 2

int sum2() { int a, b, s; cout<<"Enter 2 numbers: "; cin>>a>>b; s=a+b; return s; }

Function 3

void sum3(int a, int b) { int s; s=a+b; cout<<"Sum="<<s; }

Function 4

int sum4(int a, int b) { int s; s=a+b; return s; }

The task performed is identical in each, but the number of parameters and the return type differ:

NameArgumentsReturn value
sum1()No argumentsDoes not return any value
sum2()No argumentsReturns an integer value
sum3()Two integer argumentsDoes not return any value
sum4()Two integer argumentsReturns an integer value
Table 3.1: Analysis of functions

A function with a return type other than void must return a value of that type using a return statement — which also immediately hands control back to the calling function, so any statements written after return within that function never execute. Functions of type void may omit return entirely, or use a bare return; with no value attached.

No function besides main() runs automatically — every sub-function, predefined or user-defined, executes only when it is called from main() or from another function. Here is a complete program calling all four:

int main() { int x, y, z=5, result; cout << "\nCalling the first function\n"; sum1(); cout << "\nCalling the second function\n"; result = sum2(); cout << "Sum given by function 2 is " << result; cout << "\nEnter values for x and y : "; cin >> x >> y; cout << "\nCalling the third function\n"; sum3(x,y); cout << "\nCalling the fourth function\n"; result = sum4(z,12); cout << "Sum given by function 4 is " << result; cout << "\nEnd of main function"; return 0; }

Since it returns exactly one usable result instead of printing internally, sum4()-style functions (parameters in, a value returned) are generally the most flexible and reusable design.

3.4.2

Prototype of Functions

A C++ program may define its functions in any order — main() first and the rest after, or the other way round.

Function before main()

int product(int a, int b) { int p; p = a * b; return p; } //Definition above main() int main() { int ans, num1,num2; cout<<"Enter 2 Numbers:"; cin>>num1>>num2; ans = product(num1,num2); cout<<"Product = "<<ans; return 0; }

Function after main()

int main() { int ans, num1,num2; cout<<"Enter 2 Numbers:"; cin>>num1>>num2; ans = product(num1,num2); cout<<"Product = "<<ans; return 0; } //Definition below main() int product(int a, int b) { int p; p = a * b; return p; }

The first version compiles cleanly. The second, however, fails with 'product' was not declared in this scope': while compiling main(), the compiler reaches the call to product() before it has seen the function at all, so it cannot verify the call is correct.

The fix is a function prototype — a declaration that tells the compiler the function's name, return type, and the number and type of its arguments (its accessibility too), without needing the full body. It takes the form of the function header alone, followed by a semicolon, placed before the call:

data_type function_name(argument_list);

Argument names are optional in a prototype — only the count and types must match. So the earlier error is resolved by adding this line before product() is called:

int product(int, int);

A function must be declared before it is used, exactly like a variable. If a function is fully defined before its first use, no separate prototype is required.

3.4.3

Arguments of Functions

Consider a function that computes simple interest:

float SimpleInterest(int P, int N, float R) { float amt; amt = P * N * R / 100; return amt; }
cout << SimpleInterest(1000,3,2);//Function call 1 int x, y; float z=3.5, a; cin >> x >> y; a = SimpleInterest(x, y, z); //Function call 2

In the first call, the values 1000, 3 and 2 are passed to P, N and R; in the second, the values of x, y and z are passed instead. The variables x, y, z supplying the data at the calling place are called actual (original) arguments; the variables P, N, R that receive the data in the function header are the formal arguments. If a prototype uses variable names purely for documentation, those are called dummy arguments.

Program 3.8 uses a helper function fact() that returns a factorial, called twice within the same expression to compute nCr using the formula n!/(r!(n−r)!).

// Program 3.8: To find the value of nCr #include<iostream> using namespace std; int fact(int); //Function prototype int main() { int n,r, ncr; cout<<"Enter the values of n and r : "; cin>>n>>r; ncr=fact(n)/(fact(r)*fact(n-r)); // actual arguments; function calls per the nCr formula cout<<n<<"C"<<r<<" = "<<ncr; return 0; } int fact(int N) //Function header; N is a formal argument { int f; for(f=1; N>0; N--) f=f*N; return f; // factorial is returned }

Sample run — n=5, r=2 → 5C2 = 10

3.4.4

Functions With Default Arguments

A function converting hours, minutes and seconds into total seconds can be written so that the minutes and seconds default to zero if the caller omits them:

int TimeSec(int H, int M=0, int S=0) { int sec = H * 3600 + M * 60 + S; return sec; }

This lets the function be invoked in several equally valid ways:

long s1 = TimeSec(2,10,40); // H=2, M=10, S=40 → 7840 long s2 = TimeSec(1,30); // H=1, M=30, S uses default 0 → 5400 long s3 = TimeSec(3); // H=3, M and S use default 0 → 10800

Default arguments must be placed from the right end of the argument list towards the left, since actual arguments are matched to formal ones from left to right during a call — a default value can only be "skipped" if every argument after it also has one.

3.4.5

Methods of Calling Functions

Suppose a teacher asks you to prepare invitation letters using a class name list. She could hand you a photocopy of the list — any marks you make don't affect her original — or she could hand you the original list itself, where any change you make sticks. C++ offers exactly this choice when passing arguments to a function.

a. Call by Value (Pass by value) method — a copy of the actual argument's value is passed to the formal argument. Changing the formal argument inside the function has no effect on the actual argument back at the calling place.

void change(int n) { n = n + 1; cout << "n = " << n << '\n'; } void main() { int x = 20; change(x); cout << "x = " << x; }

Output: n = 21 then x = 20 — x is untouched because n has its own separate memory location.

Before function callAfter function callAfter function execution
x = 20 (in main), n not yet allocatedx = 20, n = 20 (copy passed)x = 20 (unchanged), n = 21
Table 3.2: Call by value procedure — formal argument n has its own memory, separate from x

b. Call by Reference (Pass by reference) method — the reference (memory address) of the actual argument is passed, so the formal argument shares the very same memory location as the actual argument. A reference variable — an alias for another variable, declared using & between the data type and the parameter name — is used as the formal parameter, and it is allocated no memory of its own.

void change(int & n) { n = n + 1; cout << "n = " << n << '\n'; } void main() { int x=20; change(x); cout << "x = " << x; }

Output: n = 21 then x = 21 — because n and x refer to the very same storage location, changing one changes the other.

Before function callAfter function callAfter function execution
x = 20 (in main), n not yet linkedx = 20, n shares x's memoryx = 21, n = 21 (same memory)
Table 3.3: Call by reference procedure — n and x share one memory location
Call by Value MethodCall by Reference Method
Ordinary variables are used as formal parameters.Reference variables are used as formal parameters.
Actual parameters may be constants, variables or expressions.Actual parameters will be variables only.
Changes made in the formal arguments do not reflect in actual arguments.Changes made in the formal arguments do reflect in actual arguments.
Exclusive memory allocation is required for the formal arguments.Memory of actual arguments is shared by formal arguments.
Table 3.4: Call by value v/s Call by reference

Program 3.9 uses call by reference to exchange (“swap”) the values of two variables — something call by value could never achieve, since the caller's variables must actually change.

// Program 3.9: To swap the values of two variables #include <iostream> using namespace std; void swap(int & x, int & y) { int t = x; x = y; y = t; } int main() { int m, n; m = 10; n = 20; cout<<"Before swapping m= "<< m <<" and n= "<<n; swap(m, n); cout<<"\nAfter swapping m= "<< m <<" and n= "<<n; return 0; }

Output: Before swapping m= 10 and n= 20, then After swapping m= 20 and n= 10 — because x and y inside swap() share memory with m and n, so the exchange happens in main() too.

3.5

Scope and Life of Variables and Functions

3.5

Local Scope vs. Global Scope

The scope of a variable is the part of the program in which it can be legally used. A variable declared inside a function (including as a formal argument) has local scope: it belongs to that function alone and cannot be referred to elsewhere. Once the function finishes executing, the memory reserved for its local variables is released — that is the variable's life.

Consider a function cube() defined separately from main(), where each tries to use a variable belonging to the other:

int cube(int n) { int cb; cout<< "The value of x passed to n is " << x; // error: x belongs to main(), not here cb = n * n * n; return cb; } int main() { int x, result; cout << "Enter a number : "; cin >> x; result = cube(x); cout << "Cube = " << result; cout << "\nCube = "<< cb; // error: cb belongs to cube(), not here return 0; }

Both lines fail to compile: x is local to main() and unreachable inside cube(), while cb is local to cube() and unreachable inside main().

By contrast, a variable declared outside every function has global scope — it is accessible to every function in the program, memory is set aside for it just before the program begins running, and it is released only when the program ends.

Functions have scope too. A function defined inside the body of another function is a local function, usable only within that enclosing function. A function defined outside all other functions is a global function, usable anywhere in the program.

// Program 3.11: To illustrate the scope and life of variables and functions #include <iostream> using namespace std; int cb; //global variable void test()//global function since defined above other functions { int cube(int n); //It is a local function cb=cube(x); //Invalid call. x is local to main() cout<<cb; } int main() // beginning of main() function { int x=5; //local variable test(); //valid call since test() is a global function cb=cube(x); //Invalid call. cube() is local to test() cout<<cb; } int cube(int n)//Argument n is local variable { int val= n*n*n; //val is local variable return val; }
Scope & lifeLocalGlobal
VariablesDeclared within a function/block; available only there; memory allocated while active and freed when it completes.Declared outside all functions; available to every function; memory allocated before execution starts and freed when the program ends.
FunctionsDeclared within another function and defined after the calling function; accessible only within that function or block.Declared/defined outside every other function; accessible by all functions in the program.
Table 3.5: Scope and life of variables and functions
Try It Yourself

Function & Parameter Workbench

Pick a predefined-function group or a parameter-passing method, supply the data, then press Run to see how C++ processes it — character by character, string by string, or argument by argument.

abs(x); sqrt(x); pow(x,y);

Enter a number and a power, e.g. -25,2

x = -25, y = 2
Self-Assessment

Question & Answer Review

Tap a question to reveal the answer — a quick way to check understanding of modular programming and functions before moving to Chapter 4.

01What is modular programming, and why is it used for large software projects?
Modular programming is the practice of dividing a large, complex problem into smaller, independent sub-problems (modules), writing a separate program for each, and integrating them into the final software. It is used because it makes large software easier to build, since each module can be developed, tested and reasoned about on its own.
02List two merits and one demerit of modular programming.
Merits include a reduction in program size (repeated logic is written once and reused) and improved re-usability (a function can be reused in other programs later). A demerit is that dividing a problem into properly independent modules, and deciding the order in which they should execute, is itself a challenging design task.
03What is a function in C++?
A function is a named unit of statements in a program that performs a specific task as part of the overall solution. It may accept parameters and may return a value, though neither is compulsory.
04Differentiate between predefined functions and user-defined functions.
Predefined (built-in) functions are ready-made, already written and compiled, with their definitions grouped in header files — e.g. sqrt() or strlen(). User-defined functions are created by the programmer for a specific task, with the name, arguments and body all decided by the programmer.
05Which header file must be included to use getchar() and putchar()?
cstdio (stdio.h in Turbo C++).
06What does cin.getline(str, len, ch) do differently from cin.getline(str, len)?
The three-argument version stops reading as soon as it hits the specified delimiter character ch, or once (len−1) characters have been read, whichever comes first. The two-argument version has no custom delimiter — it simply stops at (len−1) characters or the Enter key.
07Name the header file required to use strlen(), strcpy(), strcat() and strcmp().
cstring (string.h in Turbo C++).
08What value does strcmp("Deepthi","Divya") return, and what does it mean?
It returns a negative value, meaning "Deepthi" is alphabetically lower than "Divya".
09Which function checks whether a character is alphanumeric, and what does it return?
isalnum(char c) — it returns 1 if the character is a letter or a digit, and 0 otherwise.
10What is the difference between the actual arguments and formal arguments of a function?
Actual (original) arguments are the constants, variables or expressions used at the calling place to supply real data to the function. Formal arguments are the variables listed in the function header that receive that data for use inside the function body.
11Why is a function prototype needed when the function definition appears after main()?
Without it, the compiler encounters the function call before it has any information about that function's name, return type or arguments, and reports an error such as "not declared in this scope". A prototype — the function header followed by a semicolon — supplies this information in advance so the call can be verified.
12What are default arguments? What rule governs their position in the parameter list?
Default arguments are formal parameters given an initial value in the function header, used automatically whenever the caller does not supply a value for them. All default arguments must be placed from the right end of the argument list towards the left, since actual arguments fill in the formal ones from left to right.
13Distinguish between Call by Value and Call by Reference in terms of effect on actual arguments.
In Call by Value, only a copy of the actual argument's value is passed, so changes made to the formal argument inside the function never affect the actual argument at the calling place. In Call by Reference, the formal argument shares the very same memory as the actual argument, so any change made inside the function is reflected back in the caller.
14In C++, how do you declare a formal parameter to be passed by reference?
By placing an ampersand (&) between the data type and the parameter name in the function header, e.g. void swap(int &x, int &y) — this makes x and y reference variables (aliases) rather than ordinary variables.
15What is meant by the scope of a variable/function, and how does local scope differ from global scope?
Scope is the part of the program in which a variable or function can be accessed. A local variable or function is declared within a function (or block) and is usable only there, with its memory freed once that function finishes executing. A global variable or function is declared outside every function and is accessible throughout the entire program, existing for as long as the program runs.