Categories: C++

Functions in C++

What are functions?

A function is a collection of statements that accept input, perform some specific computation, and return output. The idea is to combine some frequently or repeatedly performed tasks into a function so that instead of writing the same code for different inputs, we can call the function.

Advantages of functions:
  • Code Reusability
  • Code optimization
Basic Syntax:
return-type function-name(parameter1, parameter2, ...)
{
    // function-body
}
  • The return type specifies what the function will return. It could be an int, a char, a pointer, or even a class object. Functions that do not return anything are denoted by the symbol void.
  • Function Name: is the name of the function, and it is called by that name.
  • Parameters are variables that hold the values of arguments passed to the function while it is being called. A parameter list may or may not be present in a function.
  • The function body is where the code statements are written.
Declaration:

A function can be declared by enclosing its return type, name, and arguments inside brackets. It notifies the compiler that this function is present. If you want to define a function after the main function in C++, you must declare it first.

Definition:

A function definition defines the function’s body. A function’s declaration and definition can be done concurrently, but it should be done before calling it.

Example:
#include <iostream>
using namespace std;

//declaring the function
int multi(int x, int y);

int main()
{
    int a = 10;
    int b = 20;
    int c = multi (a, b);    //calling the function
    cout << c;
}

//defining the function
int multi (int x, int y)
{
    return (x * y);
}
Output:
200
Calling a function:

When you define a function, you tell it what to do and how to use it; you must call or invoke the function. When a function is called from the main function, control is transferred to the function that was called. The function then goes about its business. When the task is completed, the control is returned to the main function. Methods for calling functions:

  • Call by Value
  • Call by Reference
Call by Value:

You only pass the copies of the variable to the function in this calling method, not the actual argument to the function. Because the variable or arguments are copied, any changes made to the variable in the function have no effect on the actual argument.

Example:
#include <iostream>
using namespace std;

//declaring the function
int multi(int x);

int main()
{
    int a = 10;
    multi (a);    //calling the function
   
    cout <<"a: "<< a;
}

//defining the function
int multi (int x)
{
    return (x * x);
}
Output:
a: 10
Call by Reference:

In this calling technique, you pass the argument’s address or reference, and the function receives the argument’s memory address. In this case, the variable’s actual value changes, or you can say that it reflects the changes back to the variable.

Example:
#include <iostream>
using namespace std;

//declaring the function
void multi(int *x);

int main()
{
    int a = 10;
    multi (&a);    //calling the function
   
    cout <<"a: "<< a;
}

//defining the function
void multi (int * x)
{
    *x= ((*x) * (*x));
}
Output:
a: 100

Note: also read about Storage classes in C++

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago