Function

A function is a block of code that performs a specific task.

The syntax to declare a function is:

returnType functionName(parameter1,parameter2,......)

{

// Function body

}

Suppose we need to create a program to create a circle and color it. We can create two functions to solve this problem:

  • a function to draw the circle
  • a function to color the circle

Dividing a complex problem into smaller chunks makes our program easy to understand and reusable.

There are two types of function:

  1. Standard Library Functions: Predefined in C++
  2. User-defined Function: Created by users

 

Library Functions

Library functions are the built-in functions in C++ programming.

Programmers can use library functions by invoking the functions directly; they don't need to write the functions themselves.

Some common library functions in C++ are sqrt(), abs(), isdigit(), etc.

In order to use library functions, we usually need to include the header file in which these library functions are defined.

For instance, in order to use mathematical functions such as sqrt() and abs(), we need to include the header file cmath.

 

Example

#include <iostream>

#include <cmath>

using namespace std;

int main() {

    double number, squareRoot;

    number = 49.0;

    squareRoot = sqrt(number);

    cout << "Square root of " << number << " = " << squareRoot;

    return 0;

}

Output:

Square root of 49 = 7

Process returned 0 (0x0)   execution time : 2.622 s

Press any key to continue.


User-defined Function

C++ allows the programmer to define their own function.

A user-defined function groups code to perform a specific task and that group of code is given a name (identifier).

User-defined functions can be in various forms like:

  1. Function with no argument and no return value
  2. Function with no argument but return value
  3. Function with argument but no return value
  4. Function with argument and return value


Function with no argument and no return value

Program

#include <iostream>

using namespace std;

void show_output();

int main() {

    // no argument is passed to the function

    show_output();

    return 0;

}

void show_output() {

 cout << "Hello World!";

}

Output:

Hello World!

Process returned 0 (0x0)   execution time : 2.353 s

Press any key to continue.


Function with no argument but return value

Program

#include <iostream>

using namespace std;

string get_username();// declare a function with no argument and return type string

int main() {

    string user_name = get_username();

    cout << "Hello, " << user_name;

    return 0;

}

string get_username() {

    string name;

    cout << "Enter your user name\n";

    cin >> name;

    return name;

}

Output:

Enter your user name
Prasanjeet

Hello, Prasanjeet
Process returned 0 (0x0)   execution time : 17.962 s
Press any key to continue.


Function with argument but no return value

Program

#include <iostream>

using namespace std;

void show_output(string name);// declare a function with argument and no return type

int main() {

   string name;

    cout << "Enter your name\n";

    cin >> name;

    show_output(name);

    return 0;

}

void show_output(string name)
{
cout << "Hello, " << name;
}

Output:

Enter your name
Prasanjeet

Hello, Prasanjeet
Process returned 0 (0x0)   execution time : 13.789 s
Press any key to continue.


Function with argument and return value

Program

#include <iostream>

using namespace std;

int calculateSum(int,int);// declare a function with argument and  return type

int main() {

   int a, b, sum;

    printf("Enter the first number: ");
    scanf("%d", &a);

    printf("Enter the second number: ");
    scanf("%d", &b);

    sum = calculateSum(a, b);

    printf("The sum of %d and %d is %d\n", a, b, sum);

    return 0;

}

int calculateSum(int num1, int num2) {
    return num1 + num2; // Returning the sum
}

Output:

Enter the first number: 20
Enter the second number: 50
The sum of 20 and 50 is 70

Process returned 0 (0x0)   execution time : 16.324 s
Press any key to continue.



No comments:

Post a Comment