0% found this document useful (0 votes)
41 views6 pages

Functions

The document discusses C++ functions through examples. It defines functions to add numbers, check if a number is even or odd, calculate factorials, swap numbers, print triangles, reverse numbers, and convert Celsius to Fahrenheit. Each function is declared with a return type and parameters, defined with a body, and called from main.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views6 pages

Functions

The document discusses C++ functions through examples. It defines functions to add numbers, check if a number is even or odd, calculate factorials, swap numbers, print triangles, reverse numbers, and convert Celsius to Fahrenheit. Each function is declared with a return type and parameters, defined with a body, and called from main.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Cyber.

Fox BASIC OF C++


Functions:

type function name(parameter list);

#include <iostream>

// Function declaration (prototype)

int addNumbers(int a, int b);

int main() {

int num1 = 5;

int num2 = 10;

// Function call

int sum = addNumbers(num1, num2);

std::cout << "Sum: " << sum << std::endl;

return 0;

// Function definition

int addNumbers(int a, int b) {

return a + b;

Q2:

#include <iostream>

// Function declaration

void checkEvenOdd(int num);

1
Cyber.Fox BASIC OF C++
int main() {

int number = 7;

checkEvenOdd(number);

return 0;

// Function definition

void checkEvenOdd(int num) {

if (num % 2 == 0) {

std::cout << num << " is even." << std::endl;

} else {

std::cout << num << " is odd." << std::endl;

Q3:

#include <iostream>

// Function declaration

int factorial(int n);

int main() {

int n = 5;

int fact = factorial(n);

std::cout << "Factorial of " << n << " is " << fact << std::endl;

2
Cyber.Fox BASIC OF C++
return 0;

// Function definition

int factorial(int n) {

if (n <= 1) {

return 1;

} else {

return n * factorial(n - 1);

Q3:

#include <iostream>

// Function declaration

void swapNumbers(int &a, int &b);

int main() {

int x = 5, y = 10;

std::cout << "Before swapping: x = " << x << ", y = " << y << std::endl;

swapNumbers(x, y);

std::cout << "After swapping: x = " << x << ", y = " << y << std::endl;

return 0;

3
Cyber.Fox BASIC OF C++
// Function definition

void swapNumbers(int &a, int &b) {

int temp = a;

a = b;

b = temp;

Q4:

#include <iostream>

// Function declaration

void printTriangle(int rows);

int main() {

int rows = 5;

printTriangle(rows);

return 0;

// Function definition

void printTriangle(int rows) {

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

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

std::cout << "* ";

std::cout << std::endl;

4
Cyber.Fox BASIC OF C++
}

Q5:

#include <iostream>

// Function declaration

int reverseNumber(int num);

int main() {

int number = 12345;

int reversed = reverseNumber(number);

std::cout << "Reversed number: " << reversed << std::endl;

return 0;

// Function definition

int reverseNumber(int num) {

int reversed = 0;

while (num > 0) {

int digit = num % 10;

reversed = reversed * 10 + digit;

num /= 10;

return reversed;

5
Cyber.Fox BASIC OF C++
Q6:

#include <iostream>

// Function declaration

double celsiusToFahrenheit(double celsius);

int main() {

double tempCelsius = 28.5;

double tempFahrenheit = celsiusToFahrenheit(tempCelsius);

std::cout << tempCelsius << " degrees Celsius is equal to " << tempFahrenheit << " degrees Fahrenheit." << std::endl;

return 0;

// Function definition

double celsiusToFahrenheit(double celsius) {

return (celsius * 9.0 / 5.0) + 32.0;

You might also like