MUCLecture 2021 113050379
MUCLecture 2021 113050379
Shaymah Akram
When the function is invoked from any part of the program, it all executes
the codes defined in the body of the function.
Syntax
void myFunction() {
// code to be executed
}
Example Explained
• inside the function (the body), add code that defines what the
function should do.
Example
// Function declaration
void myFunction() {
cout << "I just got executed!";
}
Example
// Function declaration
void myFunction();
// Function definition
void myFunction() {
cout << "I just got executed!";
}
Call a Function
// Create a function
void myFunction() {
cout << "I just got executed!";
}
int main() {
myFunction(); // call the function
return 0;
}
Parameters
Parameters are specified after the function name, inside the parentheses.
You can add as many parameters as you want, just separate them with a
comma:
Syntax
void functionName(parameter1, parameter2, parameter3) {
// code to be executed
}
The following example has a function that takes a string called fname as
parameter. When the function is called, we pass along a first name, which
is used inside the function to print the full name:
Example
void myFunction(string fname) {
cout << fname << " Refsnes\n";
}
int main() {
myFunction("Liam");
myFunction("Jenny");
Computer Skills & Programming II Dr. Shaymah Akram
myFunction("Anja");
return 0;
}
// Liam Refsnes
// Jenny Refsnes
// Anja Refsnes
You can also use a default parameter value, by using the equals sign (=).
Example
void myFunction(string country = "Norway") {
cout << country << "\n";
}
int main() {
myFunction("Sweden");
myFunction("India");
myFunction();
myFunction("USA");
return 0;
}
// Sweden
// India
// Norway
// USA
Multiple Parameters
Inside the function, you can add as many parameters as you want:
Example
void myFunction(string fname, int age) {
cout << fname << " Refsnes. " << age << " years old. \n";
}
int main() {
Computer Skills & Programming II Dr. Shaymah Akram
myFunction("Liam", 3);
myFunction("Jenny", 14);
myFunction("Anja", 30);
return 0;
}
Pass by Reference
You can also pass a reference to the function. This can be useful when
you need to change the value of the arguments:
Example
void swapNums(int &x, int &y) {
int z = x;
x = y;
y = z;
}
int main() {
int firstNum = 5;
int secondNum = 3;
// Call the function, which will change the values of firstNum and
secondNum
swapNums(firstNum, secondNum);
return 0;
}
Return Values
The void keyword, used in the previous examples, indicates that the
function should not return a value. If you want the function to return a
Computer Skills & Programming II Dr. Shaymah Akram
value, you can use a data type (such as int, string, etc.) instead of void,
and use the return keyword inside the function:
Example
int myFunction(int x) {
return 5 + x;
}
int main() {
cout << myFunction(3);
return 0;
}
// Outputs 8
Example
int myFunction(int x, int y) {
return x + y;
}
int main() {
cout << myFunction(5, 3);
return 0;
}
// Outputs 8
Example
int myFunction(int x, int y) {
return x + y;
}
int main() {
int z = myFunction(5, 3);
cout << z;
return 0;
}
// Outputs 8
Computer Skills & Programming II Dr. Shaymah Akram
Return by Reference
Example:
#include <iostream>
using namespace std;
// Global variable
int num;
// Function declaration
int& test();
int main()
{
test() = 5;
int& test()
{
return num;
}
Output
5
In program above, the return type of function test() is int&. Hence, this
function returns a reference of the variable num.
This stores 5 to the variable num, which is displayed onto the screen.
Function Overloading
With function overloading, multiple functions can have the same name
with different parameters:
Example
int myFunction(int x)
float myFunction(float x)
double myFunction(double x, double y)
Consider the following example, which have two functions that add
numbers of different type:
Example
int plusFuncInt(int x, int y) {
return x + y;
}
int main() {
int myNum1 = plusFuncInt(8, 5);
double myNum2 = plusFuncDouble(4.3, 6.26);
cout << "Int: " << myNum1 << "\n";
cout << "Double: " << myNum2;
Computer Skills & Programming II Dr. Shaymah Akram
return 0;
}
Example
int plusFunc(int x, int y) {
return x + y;
}
int main() {
int myNum1 = plusFunc(8, 5);
double myNum2 = plusFunc(4.3, 6.26);
cout << "Int: " << myNum1 << "\n";
cout << "Double: " << myNum2;
return 0;
}
int main () {
// Local variable declaration:
int a, b;
Computer Skills & Programming II Dr. Shaymah Akram
int c;
// actual initialization
a = 10;
b = 20;
c = a + b;
cout << c;
return 0;
}
Output
This will give the output −
30
Global variables are defined outside of all the functions, usually on top of the program.
The global variables will hold their value throughout the lifetime of your program. A
global variable can be accessed by any function.
Example
#include <iostream>
using namespace std;
// Global variable declaration:
int g;
int main () {
// Local variable declaration:
int a, b;
// actual initialization
a = 10;
b = 20;
g = a + b;
cout << g;
return 0;
}
Output
This will give the output −
30
A program can have the same name for local and global variables but the value of a
local variable inside a function will take preference. For accessing the global variable
with same rame, you'll have to use the scope resolution operator.
Example
Computer Skills & Programming II Dr. Shaymah Akram
#include <iostream>
using namespace std;
10
20
C++ ceil()
Return ceiling value of number
The ceil() function in C++ returns the smallest possible integer value which is
greater than or equal to the given argument.
ceil() Parameters
The ceil() function takes a single argument whose ceiling value is computed.
Example 1: ceil() function for double, float and long double types
#include <iostream>
#include <cmath>
int main()
{
double x = 10.25, result;
result = ceil(x);
cout << "Ceil of " << x << " = " << result << endl;
return 0;
}
Output
Ceil of 10.25 = 11
C++ floor()
The floor() function in C++ returns the largest possible integer value which is less
than or equal to the given argument.
floor() Parameters
The floor() function takes a single argument whose floor value is computed.
The floor() function returns the largest possible integer value which is less than or
equal to the given argument.
#include <iostream>
#include <cmath>
int main()
{
double x = 10.25, result;
result = floor(x);
cout << "Floor of " << x << " = " << result << endl;
Computer Skills & Programming II Dr. Shaymah Akram
x = -34.251;
result = floor(x);
cout << "Floor of " << x << " = " << result << endl;
x = 0.71;
result = floor(x);
cout << "Floor of " << x << " = " << result << endl;
return 0;
}
Output:
Floor of 10.25 = 10
Floor of -34.251 = -35
Floor of 0.71 = 0
#include <iostream>
#include <cmath>
int main()
{
int x = 15;
double result;
result = floor(x);
cout << "Floor of " << x << " = " << result << endl;
return 0;
}
Output:
Floor of 15 = 15
Computer Skills & Programming II Dr. Shaymah Akram
C++ round()
Returns integral value nearest to argument
The round() function in C++ returns the integral value that is nearest to the
argument, with halfway cases rounded away from zero.
round() Parameters
The round() function takes a single argument value to round.
#include <iostream>
#include <cmath>
int main()
{
double x = 11.16, result;
result = round(x);
cout << "round(" << x << ") = " << result << endl;
x = 13.87;
result = round(x);
cout << "round(" << x << ") = " << result << endl;
x = 50.5;
result = round(x);
cout << "round(" << x << ") = " << result << endl;
x = -11.16;
result = round(x);
cout << "round(" << x << ") = " << result << endl;
x = -13.87;
result = round(x);
cout << "round(" << x << ") = " << result << endl;
x = -50.5;
result = round(x);
cout << "round(" << x
Computer Skills & Programming II Dr. Shaymah Akram
round(11.16) = 11
round(13.87) = 14
round(50.5) = 51
round(-11.16) = -11
round(-13.87) = -14
round(-50.5) = -51
C++ trunc()
Truncates the decimal part of a number
Truncates a double value after the decimal point and gives the integer part as the
result. The return value and the arguments are of the type double.
trunc() Parameters
The trunc() function takes a single argument whose trunc value is to be computed.
#include <iostream>
#include <cmath>
int main()
{
double x = 10.25, result;
result = trunc(x);
cout << "trunc(" << x << ") = " << result << endl;
x = -34.251;
result = trunc(x);
cout << "trunc(" << x << ") = " << result << endl;
return 0;
}
trunc(10.25) = 10
Computer Skills & Programming II Dr. Shaymah Akram
trunc(-34.251) = -34
------------------------------------------------------------
Output:
C++ exp()
returns exponential (e) raised to a number
exp() Parameters
The exp() function takes a single mandatory argument and can be any value i.e.
negative, positive or zero.
The exp() function returns the value in the range of [0, ∞].
#include <iostream>
#include <cmath>
int main()
{
double x = 2.19, result;
result = exp(x);
cout << "exp(x) = " << result << endl;
return 0;
}
exp(x) = 8.93521
C++ sqrt()
Computes Square Root of a Number
sqrt() Parameters
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x = 10.25, result;
result = sqrt(x);
cout << "Square root of " << x << " is " << result << endl;
return 0;
}
Computer Skills & Programming II Dr. Shaymah Akram
C++ fmax()
returns largest among two arguments passed
The fmax() function in C++ takes two arguments and returns the largest among
them. If one of the argument is NaN, the other argument is returned.
fmax() Parameters
#include <iostream>
#include <cmath>
int main()
{
double x = -2.05, y = NAN, result;
return 0;
}
#include <iostream>
#include <cmath>
int main()
{
double x = 56.13, result;
int y = 89;
return 0;
}
fmax(x, y) = 89
C++ fmin()
returns smallest among two given arguments
The fmin() function in C++ takes two arguments and returns the smallest among
them. If one of the argument is NaN, the other argument is returned.
C++ fabs()
returns absolute value of argument
The fabs() function in C++ returns the absolute value of the argument.
fabs() Parameters
The fabs() function takes a single argument, x whose absolute value is returned.
#include <iostream>
#include <cmath>
Computer Skills & Programming II Dr. Shaymah Akram
int main()
{
double x = -10.25, result;
result = fabs(x);
cout << "fabs(" << x << ") = |" << x << "| = " << result <<
endl;
return 0;
}
C++ remainder()
Returns remainder of x/y
remainder() Parameters
• x - The value of numerator.
• y - The value of denominator.
Computer Skills & Programming II Dr. Shaymah Akram
#include <iostream>
#include <cmath>
int main()
{
double x = 7.5, y = 2.1;
double result = remainder(x, y);
cout << "Remainder of " << x << "/" << y << " = " << result <<
endl;
x = -17.50, y=2.0;
result = remainder(x, y);
cout << "Remainder of " << x << "/" << y << " = " << result <<
endl;
y=0;
result = remainder(x, y);
cout << "Remainder of " << x << "/" << y << " = " << result <<
endl;
return 0;
}
#include <iostream>
#include <cmath>
int main()
{
int x = 5;
double y = 2.13, result;
Computer Skills & Programming II Dr. Shaymah Akram
return 0;
}
C++ pow()
Computes Power a Number
The pow() function computes a base number raised to the power of exponent
number.
pow() Parameters
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
double base, exponent, result;
base = 3.4;
exponent = 4.4;
result = pow(base, exponent);
Computer Skills & Programming II Dr. Shaymah Akram
cout << base << "^" << exponent << " = " << result;
return 0;
}
output
3.4^4.4 = 218.025