Topic :Function in C++ Programming: Declaration | Definition | Calling

What is Function?

A Function is a group of statements that perform some task together. Function receive some information, do some process and provide a result. Function break large computing tasks into smaller ones and enable people to build on what others have done instead of starting over from scratch. Every C++ Program has at least one function, which is "main()", and all the most trivial Program can define additional functions.

Actually We are dividing our code into small and separate Modules known as Function. This strategy allows us to think of the programming task more abstractly. Creating modules makes it easier to work with programs, allows several programmers to write or modify modules in the same programs, and enables us to create reusable programs pieces.



function : infobrother

Concept:

In above chair it has a seat back and four legs. Now we need to make a seat, back, and four legs out of wood. the major task is to make a chair, subtasks are, make a seat its back and then fabricate four legs. The legs should be identical. We can fashion one leg and then re-use this prototype, we have to build three more identical legs. The last task is to assemble all these to make a chair. We have a slightly difficult task and have broken down it into simpler pieces. This is the concept of functional design or top-down designing.

When we write a function, it must start with a name, parentheses, and surrounding braces just like with main(). Function are very important in code reusing.



  • There are two types of function:
    » Function that return a value.
    » Function that do not return a value.



Suppose, we have a function that calculates the sum of two numbers such that function will return the sum of Numbers. similarly, we may have a function which displays some information on the screen so this function is not supposed to return any value to the calling program.







Function Declaration:

A function Declaration tells the compiler about a function's name, return type, and parameters. The actual body of the function can be defined separately.
In C++, every function has a header, or Function Declaration, that includes three parts:



 return_type   function_name(  parameter list ); 

In Function Declaration, we have three important things:







Below, there is a function declaration which may calculate sum of two numbers and return result.

 int   sum(  int num1 , int num2 ); 


Parameter names are not important in function declaration only their type is required, so following is also valid declaration:

 int   sum(  int , int ); 


Or we have an function which can display only text. as it not returning any result, it will only show some message. so it's return-type will be void having no parameters, as shown in the given example:

 void   message ( ); 


The function declaration is required when we define a function in one source file and we call that function in another file. or if we define function after "main() function" then declaration of that function before "main() function" is required. because compiler compiles the program from top to bottom. so in the absence of function-declaration cause error when we will try to call the function in "main() function". but if we define the function at the same time with declaration before "main() function" then no need of declaration.



Function Defination:

After declaration of function we need to define the function. we can define function with function-declaration before main() function known as Global declaration or within main() function known as local declaration, or after main() function or in another source file. Here we will define function in same source file.


We will discuss how to define function in another source file in our Libraries tutorial.


The general form of C++ function definition is as follows:


return_type function_name (Parameter  list )
{
	/*function body*/
}


C++ function definition consists of a function header (function declaration or prototype) and a function body. we already discuss about function declaration or header. The function body contains a collection of statements that define what the function does.

Below, there is an example of Function having no return type. We use Void if we don't have any return type.


void message()
{
    cout<<"Welcome to InfoBrother: "<<endl;
    cout<<"I am function to display a message to you."<<endl;
    cout<<"But i won't return any thing to you."<<endl;
}


Calling a Function:

We have Function declaration and Function Definition. Now in order to use a function, we will have to call or invoke that function. When a program calls a function, program control is transferred to the called function. A called function performs the defined task and when it's return statement is executed or when its function-ending closing brace is reached, it returns program control back to the main program.

To call a function, we simply need to pass only function_name with Empty braces (if there is no parameters) or with braces having parameters.

Let's have a simple to know, how we can create function in C++, and how to use them. in this example, we will create a simple program to show a message. this program won't return anything, so we will use Void instead of any return type.



If we are defining the function After the main() method, then we must add Function prototype or function declaration inside the main() method before calling it.


/* Function:  Example: InfoBrother */
 
#include <iostream>
using namespace std;
main()
{
	/*function prototype Or Declaration*/
	void message(); 
	cout<<"Lemme call function here. "<<endl;
	message(); //calling function.	
	return 0;
}
 
//Function Definition:
void message()
{
    cout<<"Welcome to InfoBrother: "<<endl;
    cout<<"I am function to display a message to you."<<endl;
    cout<<"But i won't return any thing to you."<<endl;
}


C++ provides a lot of useful built-in function in different libraries. These function can work like a Template and provide us an easy an short way to solve a problem. we will discuss some of the important function in our upcoming Tutorial.



If we are defining the function Before the main() method, then we don't Need to add Function prototype or function declaration inside the main() method before calling it.


Let's have another useful example of function. Here we will define function before main() function so there will be no need of function declaration or function prototype. This example will take some parameter and return integer as shown in the given example.


/* Function Example - InfoBrother */
#include <iostream>
using namespace std;
 
/*Function declaration & definition.
this function will accept 2 Integers and will 
return the sume of these two integers*/
int sum (int a, int b) 
{
    int result=0; //local variable to store result.
    result = a + b; 
}
 
main() //main function.
{
    int a=150; //1st parameter.
    int b=14; //2nd parameter.
 
	/* 1st way to call function.*/
	cout<<" First way to call function:(passing variable as an parameter) "<<endl;
	cout<<" the sum of a and b is: "<<sum(a,b)<<endl;
 
	/*2nd way to call function*/
	cout<<"\n second way to call function:(passing Number as an parameter) "<<endl;
	cout<<" the sum of a and b is: "<<sum(150,14)<<endl; 
 
return 0;
}


Function Arguments:

If a function is to use arguments, it must declare variables that accept the values of the arguments. These variables are called the formal parameters of the function. The formal parameters behave like other local variables inside the function and are created upon entry into the function and destroyed upon exit.

While calling a function, there are three ways that arguments can be passed to function.







Whenever we Writing a program, always keep in mind that it could be reused at some other time. also, try to write in a way that it can be used to solve some other related problem. The function is very important in case of reusability. We can write some advance function and can include these function in other programs. Function provide us the way through which we can break our code into small pieces and make it more readable and efficient.







I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement