Curriculum
Course: Learn C++
Login

Curriculum

Learn C++

Text lesson

Introduction to Functions in C++

In this lesson, you will learn.

  • What is a Function
  • Different Forms of Function
  • Adding a Function to a C++ Program
  • Examples

 

What is Function?

A function is a self-contained block of statements or code segments that is used to perform a particular task.

For example: You can create a function named ‘factorial()’ and inside it, you can write a logic to find the factorial of any number.

Here is the syntax to define a function in C++.

Syntax

 

Here,

  • The return type will be a valid data type e.g. int, float, double, void, etc.
  • The function name is the name of the function followed by a list of arguments. The arguments are not mandatory. The function may or may not have arguments.
  • The body of the function defines the functionality/code of a function.

 

Different Forms of Functions

Functions in C++ can be of different forms.

1. Function without arguments and without returning a value.

// 1. Function without arguments 
// and without returning value
void starline() 
{ 
	// Function body 
}

 

2. Function with arguments and without returning a value.

// 2. Function with arguments and 
// without returning a value.

void findsum(int x, int y) 
{
	cout<<"Sum: "<<(x+y);
}

 

3. Function without arguments and with returning a value.

/* 3. Function without arguments and with 
returning a value */
int getnumber() {
	int num; 
	cout<<"Enter any number";
	cin>>num;
	return num;
}

 

4. Function with arguments and with returning a value.

/* 4. Function with arguments and with 
returning a value */

float multiply(float x, float y)  
{
	return (x*y);
}

 

Adding a Function to the C++ Program

Here, we are adding a function show() in our C++ program.

Syntax

#include<iostream>
using namespace std;
void show(); // Function prototype
int main()
{
    ...............
    //code
    show(); //Function Call
    ...............
    //code
}
void show()
{
    //Body of function
}

 

The following figure shows the flow of the code.

 

 

Example -1: Printing a Data Types Table Using Function

//FunctionTablePrintEx1.cpp
#include <iostream>
using namespace std;

void starline(); //function declaration (prototype)

int main(){
   starline(); //call to function
   cout << "Data type Range" << endl;

   starline(); //call to function
   cout << "char -128 to 127" << endl
     << "short -32,768 to 32,767" << endl
     << "int System dependent" << endl
     << "long -2,147,483,648 to 2,147,483,647" << endl;

   starline(); //call to function
   return 0;
}

//starline() - function definition
void starline() //function declarator
{
   for(int j=0; j<45; j++) //function body
      cout << '*';
   cout << endl;
}

 

Output

*********************************************
Data type Range
*********************************************
char -128 to 127
short -32,768 to 32,767
int System dependent
long -2,147,483,648 to 2,147,483,647
*********************************************

 

Understanding the Function Declaration (Prototype)

The function prototyping describes the function template to the compiler by giving details like the number, type of arguments, and the type of return value is also sometimes referred to as the function signature.

Example:

void starline();

The declaration tells the compiler that at some later point we plan to present a function called starline and terminate it with a semicolumn.

 

Eliminating the Declaration

We can remove the function declaration and can place the function definition before the function call.

Example

#include 
using namespace std; 
// starline() //function definition
void starline()
{
   for(int j=0; j<45; j++)
   cout <<"*";
   cout << endl;
}
int main(){
   ------------
   ------------
   starline();// Function call
   ------------
}


 

It is a simplistic approach and makes the program short, there is no need to provide the function declaration but it is less flexible.

In the case of multiple functions, the programmer has to arrange all functions like each one appears before a function call.

Many programmers prefer to put main() function first in the list because it is the entry point of the execution and the first function to be called.

So generally, we use the first approach i.e. declaration of function prototype before using it.

 

 


End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

 

 

There are no reviews yet. Be the first one to write one.

 

 

Submit a Review

 

Layer 1
Login Categories