Functions in C++ | Topic 13 | VU Insider



The Scenario

In our daily life, we divide our tasks into sub tasks. Consider making a simple table which has four legs and a top. Here our major task is to make a table. Sub tasks are making legs and top. Last task would be to assemble all these to make a table. In the above scenario, we have a difficult task and broken down into smaller pieces (sub tasks).

The same thing can happen while program writing. We may face problems that we don't know how to solve them. So what we do? we divide these type of problems into smaller pieces and we do it until we get a simple sub task.

Defination:

Functions are like sub tasks. They receive some information, do some tasks, and provide results. A function can only run when it is called. Functions are very important in code reusing as we can define the code once and use it as many times you want.

For example, main() is a pre-defined function of C++ language which we used to execute our code. We can create our own functions to perform certain actions or tasks.

Syntax:
return-value-type function-name (argument-list) { statements // Code to be executed }code-box

return-value-type:
It's a keyword indicating whether the function is returning a value or not. For functions that will not return any value, the keyword used for return-value-type is "void". Default keyword is "int".

Categories of Functions

There are two categories of functions:
  1. Functions that return a value
  2. Functions that do not return a value

1: Functions that return a value

Suppose you have a function that calculates square root of an integer. The function will return the square root of integer.

Keyword: int

Syntax:
int myFunction() {
// Code to be executed
}code-box


Example:
int square(int number) {
int result = 0;
result = number * number;
return result;
}code-box


2: Functions that do not return a value

Suppose you have a function which prints some information on the screen. That function is bot supposed to return any value to the calling program.

Keyword: void

Syntax:
void myFunction() {
// Code to be executed
}code-box


Example:
void day() {
cout << "Today is Friday!";
//Not returning a value but an information
}code-box


How to call a function in C++?

As we learned before that a function can be called multiple times. We just need to provide function name and its arguments (without data type) to call it.

Let's see an example.

Example:
void day() {
cout << "Today in Friday!\n";
}
int main() {
day(); // calling day() function
day();
day();
return 0;
}code-box


The output will be;
Today is Friday!
Today is Friday!
Today is Friday!code-box


Declaration & Definition of a Function

A C++ function consist of two parts:
  • Declaration: the function's name, return type, and parameters.
  • Definition: the body of the function (code to be executed)


Difference between Declaration & Definition:
// Function declaration
void myFunction();

// main
int main() {
myFunction(); // call the function
return 0;
}

// Function definition
void myFunction() {
cout << "Today is Friday!";
}code-box

Post a Comment

Previous Post Next Post