0% found this document useful (0 votes)
9 views27 pages

C++ Functions: Definitions & Usage Guide

This document provides an overview of functions in C++ programming, covering topics such as function definitions, declarations, arguments, return values, reference arguments, overloaded functions, and default arguments. It explains how to declare and call functions, as well as the concept of passing arguments by value and by reference. The document also touches on function overloading, allowing multiple functions with the same name but different parameters.

Uploaded by

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

C++ Functions: Definitions & Usage Guide

This document provides an overview of functions in C++ programming, covering topics such as function definitions, declarations, arguments, return values, reference arguments, overloaded functions, and default arguments. It explains how to declare and call functions, as well as the concept of passing arguments by value and by reference. The document also touches on function overloading, allowing multiple functions with the same name but different parameters.

Uploaded by

ahmdabdi177
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

1

LOGO
C++
Object Oriented
Programming

2
LOGO
Functions

3
LOGO
Overview
You will learn about the following in this
chapter:
 Function definitions and declarations
 Arguments and return values
 Reference arguments
 Overloaded functions
 Default arguments
 Storage classes

4
LOGO
Functions-Introduction
Functions in C++ (and C) are similar to
subroutines and procedures in various
other languages.

5
LOGO
Simple Functions
Our first example demonstrates a simple
function whose purpose is to print a line of
45 asterisks

6
LOGO
The Function Declaration
In the TABLE program, the function
starline() is declared in the line void
starline();
The declaration tells the compiler that at
some later point we plan to present a
function called starline
keyword void specifies that the function
has no return value, and the empty
parentheses indicate that it takes no
arguments.
7
LOGO
Cont…
Function declarations are also called
prototypes.

8
LOGO
Calling the Function
The function is called (or invoked, or
executed) three times from main(). Each of
the three calls looks like this:
starline();
 The function name, followed by
parentheses

9
LOGO
The Function Definition
The definition contains the actual code for
the function.
 Here’s the definition for starline():

10
LOGO
Cont…
 The definition consists of the declarator,
followed by the function body.
 The declarator must agree with the
declaration
 The declarator is not terminated by a
semicolon

11
LOGO
Function Components

12
LOGO
Eliminating the Declaration
To eliminate the function declaration ,we
simply place the function definition (the
function itself) in the listing before the first
call to the function.

13
LOGO
Cont…

14
LOGO
Passing Arguments to Functions
An argument is a piece of data (an int
value, for example) passed from a
program to the function.
Arguments allow a function to operate with
different values, or even to do different
things, depending on the requirements of
the program calling it.

15
LOGO
Passing Arguments to Functions
Passing Constants

16
LOGO
Cont…

17
LOGO
Returning Values from Functions
When a function completes its execution,
it can return a single value to the calling
program.
Usually this return value consists of an
answer to the problem the function has
solved

18
LOGO
Cont…
Passing Variables

19
LOGO
Reference Arguments
A reference provides an alias—a different
name—for a variable. One of the most
important uses for references is in passing
arguments to functions.
We’ve seen examples of function
arguments passed by value. When
arguments are passed by value, the called
function creates a new variable of the
same type as the argument and copies the
argument’s value into it.
20
LOGO
Cont…
As we noted, the function cannot access
the original variable in the calling program,
only the copy it created.

21
LOGO
Cont…
Passing arguments by reference uses a
different mechanism. Instead of a value
being passed to the function, a reference
to the original variable, in the calling
program, is passed. (It’s actually the
memory address of the variable that is
passed, although you don’t need to know
this.)
The next example, REF, shows a simple
variable passed by reference.
22
LOGO
Cont…

23
LOGO
Overloaded Functions
Declaring more than one functions with
the same name but different set of
arguments and return data types is called
functions overloading.
For example, three functions with the
name “sum” and having different
parameters are declared as
int sum(int, int);
int sum(int, int, int);
double sum(double, double, double);
24
LOGO
Cont…

25
LOGO
What was discussed so far
 Function definitions and declarations
 Arguments and return values
 Reference arguments
 Overloaded functions

26
LOGO
LOGO

You might also like