Function – Declaration Arguments Return Types

Functions Overview


Function is a set of statements and operations which performs a specific task.Every code has atleast one function (main function) and more can be created for simplifying and reducing the same code over and over again.

Example for a code in c++

[code language=”cpp”]
#include<iostream>
using namespace std;
int myFunction(int x, int y) //declaration of the function and the arguments .
{
int z;
z=x+y;
return z; //returns the integer value z
}

void main() //declaration of main function with no arguments
{
int a,b;
a=2;
b=3;
myFunction(a,b); //calling the function to add the numbers
}
[/code]

Declaration

When creating a new function we need to tell the computer the name, the arguments(if any) and the type of the function which is called as the declaration of that function.

Example :

[code language=”cpp”]
int add(int a,int b)
[/code]

Note 1- Good declarations are always in camelCase as they are more readable, example – declaring a functioname name as myfunctionname is bad coding standard and myFunction name is good.

Arguments

The values given/passed to the function that it may use in it’s operation are known as it’s arguments. A function may or may not have any values in it.

Example:

[code language=”cpp”]
int add(int a,int b) // here a,b are the arguments and int tells us they are integer
[/code]

Return Types

A function may value after it runs completely, the type of value it returns is called the return type, it can be int (integer), float (decimal),char (alphabet of symbols), void (nothing is returned )… etc .

Examples :

[code language=”cpp”]
int add() //returns integer value
float divide()//returns decimal value
void print()//no value is returned
[/code]