Function Part 2

Estimated reading: 7 minutes 109 Views

Example 1:

#include <iostream>

using namespace std;

double avg(int m1, int m2, int m3) 
{
    return double(m1 + m2 + m3) / 3;
}

int main()
{
    int mm1, mm2, mm3;
    cout << "Enter your marks" << endl;
    cin >> mm1 >> mm2 >> mm3;
    cout << "avg = " << avg(mm1, mm2, mm3) << endl;
    
    return 0;
}

This code defines a function called avg(), which takes three integer parameters (m1, m2, and m3) and returns the average of those three parameters as a double value.

The function works by first converting the integer parameters to double values using the double() function. This is necessary because the average of three integers is a fractional number, and we want the function to return a double value.

Next, the function calculates the sum of the three parameters and divides that sum by 3. This gives us the average of the three parameters.

Finally, the function returns the average value.

The program works as follows:

  1. The program declares three integer variables mm1mm2, and mm3 to store the marks entered by the user.
  2. The program prompts the user to enter their marks and reads the marks into the three variables.
  3. The program calls the avg() function to calculate the average of the three marks.
  4. The program prints the average to the console.

Example 2:

#include <iostream>

using namespace std;

int max(int n1, int n2, int n3) 
{
    int mx = n1;
    if (mx < n2)
        mx = n2;
    if (mx < n3)
        mx = n3;
    return mx;
}

int min(int n1, int n2, int n3) 
{
    int mn = n1;
    if (mn > n2)
        mn = n2;
    if (mn > n3)
        mn = n3;
    return mn;
}

int main()
{
    cout << "max = " << max(100, 200, 300) << endl;
    cout << "min = " << min(100, 200, 300) << endl;
    
    return 0;
}

This code defines two functions called max() and min()which both take three integer parameters (n1, n2, and n3) and returns the largest and the smallest of those three parameters.

The first function works by first initializing a variable called mx to the value of the first parameter, n1. Then, the function checks if mx is less than the second parameter, n2. If it is, the function assigns the value of n2 to mx. Next, the function checks if mx is less than the third parameter, n3. If it is, the function assigns the value of n3 to mx. Finally, the function returns the value of mx, which is the largest of the three parameters.

The second function works by first initializing a variable called mn to the value of the first parameter, n1. Then, the function checks if mn is greater than the second parameter, n2. If it is, the function assigns the value of n2 to mn. Next, the function checks if mn is greater than the third parameter, n3. If it is, the function assigns the value of n3 to mn. Finally, the function returns the value of mn, which is the smallest of the three parameters.

Here is the output of the program:

min = 100
max = 300

There are two ways to define a function in a C++ program:

  1. Function declaration: This is a prototype of the function that tells the compiler about the function’s name, return type, and parameters. The function declaration must be placed before the first call to the function.
  2. Function definition: This is the actual code that implements the function. The function definition can be placed anywhere in the program, but it is generally placed at the end of the program.

For example, here is a function declaration and definition:

// Function declaration
int add(int a, int b);

// Function definition
int add(int a, int b) {
  return a + b;
}

The function declaration tells the compiler that there is a function called add() that takes two integer parameters and returns an integer value. The function definition provides the actual code that implements the function.

You can also arrange function declarations and definitions in different ways in your code. For example, you can:

  • Declare all of your functions at the beginning of your program and then define them at the end of your program.
  • Declare and define your functions in the order that they are called in your program.
  • Declare your functions in a header file and define them in a source file.

The best arrangement for your code will depend on the specific needs of your program.

Here is an example of how to use function declarations and definitions in a C++ program:

// Function declaration
int add(int a, int b);

int main() {
  // Function call
  int sum = add(1, 2);

  // Print the sum
  cout << "The sum is: " << sum << endl;

  return 0;
}

// Function definition
int add(int a, int b) {
  return a + b;
}

Of course you should pay a massive attention to the sequential arrangement of your program.

The sequential arrangement of functions in a C++ program is important for two main reasons:

  • Readability: A well-organized program is easier to read and understand. By arranging functions in a logical order, you can make it easier for other programmers to follow the flow of your code.
  • Maintainability: A well-maintained program is easier to update and fix bugs in. By arranging functions in a logical order, you can make it easier to find and fix problems in your code.

Note !

The main function is the entry point for all C++ programs. It is the first function that is called when the program is executed. The main function is responsible for initializing the program, calling other functions, and returning a value to the operating system.

The main function is important for several reasons:

  • It is the starting point for the program execution.
  • It is responsible for initializing the program.
  • It controls the flow of the program by calling other functions.
  • It returns a value to the operating system when the program terminates.

Without the main function, the program would not be able to start executing.

Here is an example of a simple main function in C++:

int main() {
  // Initialize the program
  // ...

  // Call other functions
  // ...

  // Return a value to the operating system
  return 0;
}

This main function initializes the program, calls other functions, and returns a value of 0 to the operating system.

You can use the main function to control the flow of your program by calling other functions. For example, you could write a function to calculate the sum of two numbers and then call that function from the main function.

You can also use the main function to return a value to the operating system. This value is used to indicate whether the program terminated successfully or not.

The main function is a very important part of any C++ program. It is the starting point for the program execution, and it controls the flow of the program.

Example 3:

#include <iostream>

using namespace std;

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

void print()
{
    cout << "Hello" << endl;
}

void xy()
{
    cout << 5 * 10 << endl;
}

int main()
{
    cout << "Nothing" << endl;
    return 0;
}

int f2()
{
    return 5;
}

This code defines four functions: avg(), print(), xy(), and f2(). The avg() function takes three integer parameters and returns the average of those three parameters. The print() function prints the string “Hello” to the console. The xy() function prints the product of 5 and 10 to the console. The f2() function returns the integer 5.

The main function in the code simply prints the string “Nothing” to the console and then returns 0.

When you run this code, the following output will be printed to the console:

Nothing

This is because the only function that is called from the main function is the cout << "Nothing" << endl; statement.

You can use the other functions in the code by calling them from the main function or from other functions.

Share this

Function Part 2

Or copy link

CONTENTS
English