Chapter 4 C++
Chapter 4 C++
Functions
4.1. What are C++ functions?
A function is a set of statements that takes input, does some specific computation, and produces
output. They are used to break down a program into smaller, more manageable parts, making it
easier to understand and maintain. If your program is very bulky, break it into smaller parts so
that each part does a specific job; then call these parts functions.
There are two types of functions in C++, which are built-in functions and user defined
functions.
int main() {
cout << sqrt(10);
// Output: 3.16228
}
We note here that the Library <cmath> has many functions and sqrt is one of them. Other
functions in the same Library include: log(), pow(), sin() and cos().
1
4.4. User defined functions
Function Declaration and Definition
A C++ function consist of two parts: function declaration and function definition.
Example
int sum(int a, int b) {
return(a + b);
}
2
using namespace std;
int main()
int a = 10;
int b = 20;
cout << c;
return (x + y);
Here, initially the function is declared, without body. Then inside main() function it is called, as
the function returns summation of two values, and variable c is there to store the result. Then, at
last, function is defined, where the body of function is specified. We can also, declare & define
the function together, but then it should be done before it is called.
3
4.5.1. Call by Value
In this calling technique we pass the values of arguments which are stored or copied into the
formal parameters of functions. Hence, the original values are unchanged only the parameters
inside function changes.
int main()
int x = 10;
calc(x);
cout<< x ;
void calc(int x)
x = x + 10 ;
In this case the actual variable x is not changed, because we pass argument by value, hence a
copy of x is passed, which is changed, and that copied value is destroyed as the function ends
(goes out of scope). So the variable x inside main() still has a value 10.
Example 2
#include <iostream>
using namespace std;
int larger(int a, int b) {
if (a > b) {
return a;
}
else {
return b;
4
}
}
// Returns the larger of the two values.
int main() {
int bigger1 = larger(17, 42); // call the function larger
int bigger2 = larger(29, -3); // call the function again
int biggest = larger(bigger1, bigger2); // call the
function again
cout << "The biggest is " << biggest << "!!" <<
endl;
return 0;
}
The result from the console is as follows
int main()
int x = 10;
cout<<x ;
}
5
void calc(int *p)
*p = *p + 10;
• factorial of 0 is 1
• factorial of a positive number n is n time the factorial of n-1
The second line clearly indicates that factorial is defined in terms of itself and hence can be
expressed as a recursive function.
A recursive function must have at least one termination condition which can be satisfied.
Otherwise, the function will call itself indefinitely until the runtime stack overflows.
6
Example
// Factorial of n = 1*2*3*…*n
Another example is a function that computes the sum of the first N positive integers 1,2,…,N.
Exercise
1. Write an int function cube() that returns the cube of its single int formal parameter.
2. Write a float function triangle() that computes the area of a triangle using its two formal
parameters h and w, where h is the height and w is the length of the base of the triangle.
3. Write a float function rectangle() that computes and returns the area of a rectangle using its
two float formal parameters h and w, where h is the height and w is the width of the rectangle.
4. Write a function called isEven() that uses the remainder operator(%) to determine whether
an integer is even or not.