0% found this document useful (0 votes)
12 views11 pages

Functions

Uploaded by

zainmajeed2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views11 pages

Functions

Uploaded by

zainmajeed2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Basic Level

Function with No Parameters and No Return Value

void greet() {

cout << "Hello, World!" << endl;

Explanation: Prints a greeting message.

Usage: greet();

Notes: This function does not take any input and does not return
any value.

Function with No Parameters and an Integer Return Value

int getFive() {

return 5;

Explanation: Returns the integer 5.


Usage: int number = getFive();

Notes: return passes the value 5 back to where the function was
called.

Function with One Integer Parameter

void printSquare(int num) {

cout << "Square of " << num << " is " << num * num << endl;

Explanation: Calculates and prints the square of num.

Usage: printSquare(4);

Notes: num is passed as an argument when calling the function.

Function with Integer Return Value and One Integer Parameter

int doubleValue(int num) {

return num * 2;

}
Explanation: Doubles the value of num and returns the result.

Usage: int result = doubleValue(6);

Notes: The returned value can be stored in a variable.

Function with Two Integer Parameters and Integer Return Value

int add(int a, int b) {

return a + b;

Explanation: Returns the sum of a and b.

Usage: int sum = add(3, 5);

Notes: This function takes two inputs and returns their sum.

Function with One Character Parameter

void displayCharacter(char ch) {

cout << "Character: " << ch << endl;

}
Explanation: Displays a character.

Usage: displayCharacter('A');

Notes: Useful for working with single characters.

Function with Character Return Value and No Parameters

char getLetterA() {

return 'A';

Explanation: Returns the character A.

Usage: char letter = getLetterA();

Notes: Returns a character when called.

Function with Integer Return Value and Boolean Parameter

int boolToInt(bool flag) {

return flag ? 1 : 0;

}
Explanation: Converts a boolean value to 1 or 0.

Usage: int result = boolToInt(true);

Notes: true becomes 1 and false becomes 0.

Function with No Parameters, Returns Current Year

int getCurrentYear() {

return 2024;

Explanation: Returns a fixed value representing the current year.

Usage: int year = getCurrentYear();

Notes: Useful for providing a constant value.

Function with One String Parameter and No Return

void greetUser(string name) {

cout << "Hello, " << name << "!" << endl;

}
Explanation: Greets the user by name.

Usage: greetUser("Alice");

Notes: Takes a string and prints a personalized greeting.

Intermediate Level

Function that Returns the Larger of Two Integers

int max(int a, int b) {

return (a > b) ? a : b;

Explanation: Returns the larger of two integers.

Usage: int larger = max(3, 7);

Notes: Uses the conditional operator ? :.

Function with Single Integer Parameter that Returns its Absolute Value

int absolute(int num) {

return (num < 0) ? -num : num;


}

Explanation: Returns the absolute value of num.

Usage: int absVal = absolute(-10);

Notes: Negative values are converted to positive.

Function with Integer Return Value to Calculate Power of a Number

int power(int base, int exponent) {

int result = 1;

for (int i = 0; i < exponent; i++) {

result *= base;

return result;

Explanation: Calculates base raised to exponent.

Usage: int result = power(2, 3);

Notes: Uses a for loop to compute the power.


Function that Returns the Minimum of Two Integer Values

int min(int a, int b) {

return (a < b) ? a : b;

Explanation: Returns the smaller of two integers.

Usage: int smaller = min(3, 7);

Notes: Uses the conditional operator to determine the minimum.

Function with Single Integer Parameter that Checks if It’s Even

bool isEven(int num) {

return (num % 2 == 0);

Explanation: Checks if num is even.

Usage: bool result = isEven(4);

Notes: Returns true for even numbers and false for odd numbers.
Function with Integer Return Value and a Parameter to Calculate
Factorial

int factorial(int n) {

int result = 1;

for (int i = 1; i <= n; i++) {

result *= i;

return result;

Explanation: Computes factorial of n.

Usage: int fact = factorial(5);

Notes: Uses for loop to calculate factorial.

Function to Check if a Character is a Vowel

bool isVowel(char ch) {

ch = tolower(ch);

return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');


}

Explanation: Checks if a character is a vowel.

Usage: bool result = isVowel('A');

Notes: Converts uppercase to lowercase for consistent checking.

Function to Return Square of a Number as Double

double square(double num) {

return num * num;

Explanation: Returns the square of a double.

Usage: double result = square(3.5);

Notes: Accepts and returns double type.

Function to Find GCD of Two Integers

int gcd(int a, int b) {

while (b != 0) {
int temp = b;

b = a % b;

a = temp;

return a;

Explanation: Uses the Euclidean algorithm to find GCD.

Usage: int result = gcd(36, 60);

Notes: Implements an iterative approach.

Function to Convert Celsius to Fahrenheit

double celsiusToFahrenheit(double celsius) {

return (celsius * 9 / 5) + 32;

Explanation: Converts Celsius to Fahrenheit.

Usage: double fahrenheit = celsiusToFahrenheit(25);

Notes: Uses temperature conversion formula.

You might also like