C++ Functions

In This Section You'll Learn About Functions in C++.

  • You will Learn what function is?
  • Syntax of a function in c++.
  • Difference between declaration and definition of a function
  • How To define a function and how to call a function in c++?

What is function in a c++ program?

A function is a group of statement that together perform some task, Every C++ Program has at least one function that is main() How many function you can create in a program is up to you the more task your program is performing the more it will contain a functions but again it's up to you how you divide your program into functions depend upon your program structure and task the program is performing.

Syntax of a function

return_type function_name(parameter 1, parameter 2,...){
//function body
}

What is Difference between declaration and definition of a function?

function declaration part tell the compiler about return type of the function, name of the function and it's parameter(s). function definition part define actual body of the function in the declaration you don't have to provide body of the function and also it's not necessary to declare a function before defining it in c++ you can define a function without declaring it.

Return_type of a function.

A function may return a value and the return type define the data of the value that is return from a function, some function may not return any value in that case the return type of the function is void.

Name of a function.

Every function has a name together a function name with parameter list and data type constitute the function signature.

Parameter(s) of a function.

A function may take parameter(s) in that case if you call a function you need to pass values for that parameters.
You'll see in this tutorial how we can declare a function and then define it and how can you define a function without declaring it let see example's of different type of function you can write in c++ one by one.

Example 1: Function declaration and definition

//function with return type is void and no parameters
#include <iostream>
using namespace std;
//function declaration
void test();

//function definition
void test(){

cout<<"I'm test() function.";
}

// Every Program should have main function from where the program execution is start
int main(){
// calling of the test() function you don't have to provide return type while calling a function
test();
}

Example 2: function with no declaration.

// example program of a function that accept two parameters and calculate and display power of a number


#include <iostream>

using namespace std;
//function that take parameter(s)
//function that calculate power of any number
void power(int number, int pow){
int result = 1;
if(pow == 0){
cout<<"1";
}else if(pow == 1){
cout<<number;

}else{

for(int i = 0; i < pow; i++){
result *= number;
}
cout<<result;
}
}

int main(){
int no = 2;
int p = 4;
power(no, p);
}

Example 3: Function that return a value

Till now you have seen functions that only display some value or a string but some a function should return a value in this example you'll see a function that return some value.

#include <iostream>

using namespace std;

//function that return square of a number
int square(int number){
return number * number;
}

int main(){
//As the function is returning a value you can either out put it into console like below
// or you can store this value in a variable for other use
cout<<square(3);
}


No comments:

Post a Comment

What is Cplusplus? Introduction To C++

What is C++? The Simple Answer to this question is c++ is a computer programming language. What is computer programming language and wh...