0% found this document useful (0 votes)
9 views

Functions in C++

The document discusses functions in C++, including user-defined functions, built-in functions, function declaration, function definition, function overloading, and provides an example of a function to print a message. Functions are building blocks that perform specific tasks and a program can contain many user-defined and built-in functions.

Uploaded by

iamabbdullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Functions in C++

The document discusses functions in C++, including user-defined functions, built-in functions, function declaration, function definition, function overloading, and provides an example of a function to print a message. Functions are building blocks that perform specific tasks and a program can contain many user-defined and built-in functions.

Uploaded by

iamabbdullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Functions in C++

By: Rizwan Ullah


Class: BSSE (2nd)
Course: Object Oriented Programming
Functions in C++
• A function is a collection of statements that performs a specific task.
A program can have many functions. Each function in the program
must have a unique name. Every program in C++ has at least one
function known as main().

• Functions are the building blocks of any programming language. A


program can be divided into small blocks using functions.
Types of Functions in C++
• Two types of functions.

1. User-defined Functions

• A type of function defined by the programmer is known as user-defined function. Each


user-defined function in a program has a unique name. C++ allows the programmer to
define as many functions as required.

• 2. Built-in Functions

• A type of function that is available as a part of language is known as built-in function or


library function. These functions are predefined in C ++ and are stored in different header
files. The built-in functions are very reliable.
• The built-in functions make programming faster and easier. C++
provides many built- in functions to perform commonly used
operations. For example, clrscr() is a built-in function to clear the
screen. It is part of the header file conio.h. Some examples of library
functions are getch(), log(), and sqrt() etc.
User Defined Functions
• A user-defined function consists of the following
• Function declaration
• Function definition
Function Declaration or Function Prototype
• Function declaration is a model of a function. It is also called function prototype. It provides
information to the compiler about the function to be used in the program. It ends with a
semicolon. Function prototypes are usually placed at the beginning of the source file just
before the main() function. Function declaration consists of the following parts:
• Function name
• Function return type
• Number and types of parameters
Syntax
• The syntax of function declaration is as follows:
• Return-type Function-name (parameters);
Return-type
• It indicates the type of value that will be returned by function. For example, int
is used as return type if the function returns integer value. If the function
returns no value, the keyword void is used.
Function-name
• it indicates the name of function. The rules for specifying a function name is
similar to the rules for declaring a variable. Each function should have a unique
name.
Parameters
• Parameters are the values that are provided to a function when the function is
called Parameters are given in parentheses. If there are many parameters,
these are separated by commas. If there is no parameter, empty parentheses
are used or keyword void is written in the parentheses.
• The parameters are given in two ways:
• Only data types of the parameters are written in prototype as follows:
• int add(int, int);
• Both data types and names of parameters are written in prototype as
follows:
• int add(int a, int b);
Function Definition
• A set of statements that explains what a function does is called
function definition. The function definition can be written at the
following places:
• Before main() function
• After main() function
• In a separate file
• Function declaration is not required if the function definition is
written before main() function. Function declaration is compulsory
if function definition is written after main() function. If function
definition is written in a separate file then it can be used by
including that file in the program using #include preprocessor
directive.
• The function definition consists of two parts:
• 1. Function Header
• The first line of function definition is known as function header. It defines
the function name, return type, and parameter list. It is similar to function
prototype. The difference is that it has variable names for the parameters
and it is not terminated with semicolon. The number of parameters and
sequence parameters in function header and function prototype must be
same. Function header is also called function declarator.
• 2. Function Body
• The set of statements which are executed inside the function is known as
function body. The body of function appears after function declarator and
the statements are written in curly braces {}.
Syntax
• The syntax of function definition is as follows:
Write a program to print a message on the screen
using a function.
• #include<iostream.h>
• #include<conio.h>
• main()
•{
• void Display (void);
• clrscr();
• cout<<”Ok”;
•}
Functions Overloading

• Declaring more than one function with the same name but with different set
of arguments and return data types is called functions overloading.

• For example: four function with the same name ‘sum’ and having different
parameters are declared as:

• int sum(int, int);

• int sum(int, int, int);

• float sum (float, float, float)

You might also like