C++ Function Examples: Abbasova Fatimə's Code Explained

by Alex Johnson 56 views

Let's explore a series of C++ function examples provided by Abbasova Fatimə. This collection demonstrates basic programming concepts, including arithmetic operations, geometric calculations, and conditional statements. Each example is presented with a problem statement and a corresponding C++ solution. Let's dive in!

1. Calculating the Average of Three Numbers

In this first example, we define a function to calculate the average of three numbers. Calculating the average is a fundamental arithmetic operation used in various fields, from statistics to data analysis. The edediOrta function takes three integer arguments, x, y, and z, and returns their average. This function showcases a simple yet effective way to perform calculations in C++.

#include <iostream>
using namespace std;

int edediOrta(int x, int y, int z) {
    return (x + y + z) / 3;
}

int main() {
    int a, b, c;
    cout << "3 ededi daxil edin: ";
    cin >> a >> b >> c;
    cout << "Ededi orta: " << edediOrta(a, b, c) << endl;
    return 0;
}

This program prompts the user to enter three numbers, then calls the edediOrta function to compute the average. The result is displayed on the console. The main function handles user input and output, while the edediOrta function encapsulates the core calculation logic. Understanding how to create and use functions like this is crucial for writing modular and maintainable code. Functions allow you to break down complex tasks into smaller, more manageable units, making your code easier to read, debug, and reuse.

2. Area and Perimeter of a Square

This example demonstrates how to calculate the area and perimeter of a square using functions. Geometric calculations are common in many applications, including graphics, engineering, and game development. The sahe function calculates the area of a square, while the perimetr function calculates its perimeter. Both functions take the side length of the square as an argument.

#include <iostream>
using namespace std;

int sahe(int  teref) {
    return teref * teref;
}

int perimetr(int teref) {
    return 4 * teref;
}

int main() {
    int a;
    cout << "Kvadratin terefini daxil edin: ";
    cin >> a;
    cout << "Sahesi: " << sahe(a) << endl;
    cout << "Perimetri: " << perimetr(a) << endl;
    return 0;
}

The program prompts the user to enter the side length of the square, then calls the sahe and perimetr functions to compute the area and perimeter, respectively. The results are displayed on the console. This example illustrates how functions can be used to encapsulate specific calculations, making the code more organized and readable. By separating the calculation logic into functions, you can easily reuse these functions in other parts of your program or in different programs altogether. Code reusability is a key principle of good programming practice.

3. Sum of Two Numbers

Here, we have a simple function to calculate the sum of two numbers. Basic arithmetic operations like addition are fundamental to many programming tasks. The cem function takes two integer arguments, a and b, and returns their sum. This is a straightforward example that demonstrates the basic syntax of function definition and usage in C++.

#include <iostream>
using namespace std;

int cem(int a, int b) {
    return a + b;
}

int main() {
    int x, y;
    cout << "Iki eded daxil edin: ";
    cin >> x >> y;
    cout << "Cem: " << cem(x, y) << endl;
    return 0;
}

The program prompts the user to enter two numbers, then calls the cem function to compute their sum. The result is displayed on the console. While this example is simple, it highlights the essential elements of function definition and invocation. Understanding how to perform basic arithmetic operations within functions is a building block for more complex programming tasks. Mastering the basics is crucial for becoming a proficient programmer.

4. Calculating the Factorial of a Number

This example demonstrates how to calculate the factorial of a number using a function. Factorial calculation is a common operation in mathematics and computer science, often used in probability and combinatorics. The faktorial function takes an integer n as an argument and returns its factorial. The function uses a loop to multiply all integers from 1 to n.

#include <iostream>
using namespace std;

int faktorial(int n) {
    int f = 1;
    for (int i = 1; i <= n; i++) {
        f *= i;
    }
    return f;
}

int main() {
    int eded;
    cout << "Bir eded daxil edin: ";
    cin >> eded;
        cout << eded << " ededinin faktoriali: " << faktorial(eded) << endl;
    }

The program prompts the user to enter a number, then calls the faktorial function to compute its factorial. The result is displayed on the console. This example illustrates how functions can be used to perform more complex calculations using loops and iterative processes. Understanding how to use loops within functions is essential for solving a wide range of programming problems. Iterative processes are fundamental to algorithm design.

5. Sum of Digits of a Three-Digit Number

Here, we define a function to calculate the sum of the digits of a three-digit number. Digit manipulation is a common task in programming, often used in data validation and number processing. The reqemCemi function takes a three-digit integer n as an argument and returns the sum of its digits. The function uses the modulo operator (%) and integer division (/) to extract each digit.

#include <iostream>
using namespace std;

int reqemCemi(int n) {
    int cem = 0;
    cem += n % 10;      
    n /= 10;
    cem += n % 10;      
    n /= 10;
    cem += n;           
    return cem;
}

int main() {
    int eded;
    cout << "3 reqemli eded daxil edin: ";
    cin >> eded;
    if (eded < 100 || eded > 999) {
        cout << "3 reqemli eded daxil edin" << endl;
    } else {
        cout << "Reqemlerin cemi: " << reqemCemi(eded) << endl;
    }
    return 0;
}

The program prompts the user to enter a three-digit number, then calls the reqemCemi function to compute the sum of its digits. The result is displayed on the console. The program also includes input validation to ensure that the user enters a valid three-digit number. This example demonstrates how functions can be used to perform specific data manipulations and how to validate user input. Data validation is crucial for ensuring the correctness and reliability of your programs.

6. Area and Circumference of a Circle

This example demonstrates how to calculate the area and circumference of a circle using functions. Circle calculations are common in geometry and physics. The sahe function calculates the area of a circle, while the cevre function calculates its circumference. Both functions take the radius of the circle as an argument.

#include <iostream>
using namespace std;

int sahe(int r) {
    return 3.14 * r * r;
}

int cevre(int r) {
    return 2 * 3.14 * r;
}

int main() {
    int r;
    cout << "Dairenin radiusunu daxil edin: ";
    cin >> r;
    if (r < 0) {
        cout << "Radius menfi ola bilmez!" << endl;
    } else {
        cout << "Dairenin sahesi: " << sahe(r) << endl;
        cout << "Dairenin cevresi: " << cevre(r) << endl;
    }
    return 0;
}

The program prompts the user to enter the radius of the circle, then calls the sahe and cevre functions to compute the area and circumference, respectively. The results are displayed on the console. The program also includes input validation to ensure that the radius is not negative. This example illustrates how functions can be used to encapsulate specific calculations and how to validate user input. Input validation is an important aspect of robust program design.

7. Checking if a Number is Even or Odd

Here, we have a function to check if a number is even or odd. Even/odd checks are common in programming, often used in conditional logic and data processing. The cutTek function takes an integer n as an argument and prints whether it is even or odd.

#include <iostream>
using namespace std;

void cutTek(int n) {
    if (n % 2 == 0)
        cout << "Eded cutdur" << endl;
    else
        cout << "Eded tekdir" << endl;
}

int main() {
    int eded;
    cout << "Bir eded daxil edin: ";
    cin >> eded;
    cutTek(eded); 
    return 0;
}

The program prompts the user to enter a number, then calls the cutTek function to check if it is even or odd. The result is displayed on the console. This example demonstrates how functions can be used to perform conditional checks and how to print different outputs based on the result. Conditional logic is a fundamental concept in programming.

8. Displaying the Larger of Two Numbers

This example demonstrates how to display the larger of two numbers using a function. Comparison operations are common in programming, often used in sorting and searching algorithms. The boyukunuGoster function takes two integer arguments, a and b, and prints the larger of the two.

#include <iostream>
using namespace std;

void boyukunuGoster(int a, int b) {
    if (a > b)
        cout << a << " boyukdur" << endl;
    else if (b > a)
        cout << b << " boyukdur" << endl;
    else
        cout << "Her iki eded beraberdir" << endl;
}

int main() {
    int a,b ;
    cout << "Iki eded daxil edin: ";
    cin >> a >>b;
    boyukunuGoster(a,b);  
    return 0;
}

The program prompts the user to enter two numbers, then calls the boyukunuGoster function to display the larger of the two. The result is displayed on the console. This example illustrates how functions can be used to perform comparison operations and how to print different outputs based on the result. Comparison operators are essential for implementing various algorithms.

9. Checking if a Number is Positive, Negative, or Zero

Here, we have a function to check if a number is positive, negative, or zero. Sign checks are common in programming, often used in conditional logic and data processing. The yoxlaEded function takes an integer n as an argument and prints whether it is positive, negative, or zero.

#include <iostream>
using namespace std;

void yoxlaEded(int n) {
    if (n > 0)
        cout << "Eded musbetdir" << endl;
    else if (n < 0)
        cout << "Eded menfidir" << endl;
    else
        cout << "Eded sifirdir" << endl;
}

int main() {
    int n;
    cout << "Bir eded daxil edin: ";
    cin >> n;
    yoxlaEded(n);
    return 0;
}

The program prompts the user to enter a number, then calls the yoxlaEded function to check its sign. The result is displayed on the console. This example demonstrates how functions can be used to perform conditional checks and how to print different outputs based on the result. Conditional branching is a fundamental concept in programming.

10. Greeting a User by Name

This example demonstrates how to greet a user by name using a function. String manipulation and user interaction are common in programming, especially in interactive applications. The salam function takes a string ad as an argument and prints a greeting message.

#include <iostream>
#include <string>
using namespace std;

void salam(string ad) {
    cout << "Salam, " << ad << endl;
}

int main() {
    string ad;
    cout << "Adinizi daxil edin: ";
    cin >> ad;
    salam(ad); 
    return 0;
}

The program prompts the user to enter their name, then calls the salam function to greet them. The result is displayed on the console. This example illustrates how functions can be used to perform string manipulation and how to interact with the user. User interaction is an important aspect of application development.

These examples provided by Abbasova Fatimə offer a comprehensive introduction to basic C++ programming concepts. From arithmetic operations to geometric calculations and conditional statements, each example demonstrates how functions can be used to encapsulate specific tasks and make code more organized and reusable. By understanding these fundamental concepts, you can build a solid foundation for more advanced programming topics.

For more information on C++ programming, visit the C++ Reference website.