0% found this document useful (0 votes)
7 views26 pages

CP Chapter 4

Chapter Four discusses the concept of functions in programming, particularly in C++. It explains the purpose of functions, their syntax, how to declare and define them, and the methods of passing arguments. Additionally, it covers function overloading, overriding, and recursion with examples to illustrate these concepts.

Uploaded by

tagesseabate887
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)
7 views26 pages

CP Chapter 4

Chapter Four discusses the concept of functions in programming, particularly in C++. It explains the purpose of functions, their syntax, how to declare and define them, and the methods of passing arguments. Additionally, it covers function overloading, overriding, and recursion with examples to illustrate these concepts.

Uploaded by

tagesseabate887
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

Chapter Four

Function and Passing argument


to function

5/9/2024 Lidiya T. (MSc) 1


What is function?
 Function is group of program statements that can act
on data and return a value.
 It is a block of instructions that is executed when it is
called from some other point of the program.
 Every C++ program has at least one function, main
().

5/9/2024 Lidiya T. (MSc) 2


Why we use functions?
 It is used to write programs in a more modular way.
 Small functions promote software reusability.
 Programs should be written as collections of small
functions
 This makes the programs easier to
 Write
 Debug
 Maintain and modify

5/9/2024 Lidiya T. (MSc) 3


Syntax/format of function
declaration
 Datatype Func_name ( argument1, argument2, ...)
statement;
or
 Datatype Func_name ( argument1, argument2, ...)
{
statement1;
statement2; …
}

5/9/2024 Lidiya T. (MSc) 4


Cont.
Where:
 Datatype is the type of data returned by the function
 Func_name is the name by which it will be possible
to call the function
 arguments (can be specified)
 Each argument consists of a type of data followed by
its identifier like in a variable declaration (for
example, int x)
 The arguments act within the function like any other
variables

5/9/2024 Lidiya T. (MSc) 5


Cont.
 They allow passing parameters to the function when it
is called
 The different parameters/arguments are separated by
commas.
 Statement is with in the function's body:
 It can be a single instruction or
 A block of instructions in this case it must be
delimited by a curly brackets{}

5/9/2024 Lidiya T. (MSc) 6


Function prototype declaration
 The common approach to declare functions is at the
beginning of the program.
 The function declaration (function prototype) tells
the compiler that later in the program a function
introduced in the declaration is going to be used.
 Syntax of function prototyping is:
Datatype function_name();

5/9/2024 Lidiya T. (MSc) 7


Example
 void getnumber( ); //this is a function prototype
declaration.
 Void shows that the function doesn’t have a return
type.
 Function declaration is terminated with semicolon.
 If the function has arguments, then they should be
indicated in the declaration.

5/9/2024 Lidiya T. (MSc) 8


Function Definition
 A function definition consists of two parts: interface
and body.
 The interface of a function (also called its prototype)
specifies how it may be used.
 It consists of three entities:
 The function name (this is simply a unique identifier).
 The function parameters (also called its signature).
This is a set of zero or more typed identifiers used for
passing values to and from the function.
 The function return type (this specifies the type of
value the function returns).
5/9/2024 Lidiya T. (MSc) 9
Example
void getnumber ( ) //function header
{
int x;
cout<< “Enter Number \n”; //function
cin>>number; body
cout<<“The number you entered is:”<<number;
}

5/9/2024 Lidiya T. (MSc) 10


Calling functions
 Calling functions is invoking it to execute.
 The function call involves the function name,
followed by parentheses and semicolon.
 The function call is similar to its declaration except
that the return type is not mentioned.
 The call is terminated by semi colon.

5/9/2024 Lidiya T. (MSc) 11


Overall function Examples
#include<iostream>
using namespace std;
void add ( );//function prototype declaration
void sub ( );//function prototype declaration
void main () {
char ch;
cout <<" What do you want ? A: Add, S: subtract \n";
cin>>ch;

5/9/2024 Lidiya T. (MSc) 12


Cont.
switch(ch)
{
case 'A': // A for addition
add ( ) ; //function calling
break;
case 'S' : // S for subtraction
sub ( ); // function call
break;
}
}
5/9/2024 Lidiya T. (MSc) 13
Cont.
void add()
{
int a,b,c;
cout<<“Enter two numbers \n";
cin>> a>>b;
c = a + b;
cout <<" The sum is \t "<< c;
}

5/9/2024 Lidiya T. (MSc) 14


Cont.
void sub ( ) {
int a,b,c;
cout<<"Enter two numbers \n";
cin>>a>>b;
cout<<"the difference is \t "<<a-b;
}

5/9/2024 Lidiya T. (MSc) 15


Parameter/ Argument passing in c++
 Argument passing is the process of initializing the
storage of a function parameters with the values of
the function call arguments.
 Parameter passing can be either by value or by
reference.

5/9/2024 Lidiya T. (MSc) 16


Parameter passing by value
 It is a passing mechanism in which the copy of the
values contained in the arguments of the calling
function are assigned in the corresponding parameters
of the called function.

5/9/2024 Lidiya T. (MSc) 17


Example
int fact(int n)
{
int i, p=1;
for( i=1; i<=n; i++)
p=p* i;
return p;
}
void main()
{
int n;
cout << “enter a natural no’’;
cin>>n;
cout<<“The fact of”<<n<<“is”<<fact(n); }
5/9/2024 Lidiya T. (MSc) 18
Parameter passing by reference
 It is a means of communication in which the function
is allowed to have a direct access (use & change) to
the values of the variables (arguments) defined in the
calling function.
 The ampersand (&) is used to indicate that the
function parameter is passed by reference.

5/9/2024 Lidiya T. (MSc) 19


Example
void fact(int n, int &p)
{
int i;
for( i=1; i<=n; i++)
p = p * i;
}
void main()
{
int n, p=1;
cout << “enter a natural no’’;
cin>>n;
fact(n, p);
cout<<The factorial of <<n<<“is”<<p; }
5/9/2024 Lidiya T. (MSc) 20
Function Overloading
 Defining multiple functions with the same name is
said to be function overloading.
 Two different functions can have the same name if
the prototypes of their arguments are different.
 i.e. you can give the same name to more than one
function if they have either a different number of
arguments or different data types in their arguments.
 Example:
 Void f(int i)
 Void f(float f)
 Void f(float f, int i)
5/9/2024 Lidiya T. (MSc) 21
Function Overriding
 Is when two/more function can have different
function name but they can have the same
parameter/argument and data type.
 Example:
int add(int x, int y)
int sub(int i, int j)

5/9/2024 Lidiya T. (MSc) 22


Recursion Function
 A function which calls itself is said to be recursive.
 Take the factorial problem, for instance, which is
defined as:
Factorial of a positive number n is n times the factorial
of n-1.

5/9/2024 Lidiya T. (MSc) 23


Example
#include<iostream>
int Factorial (unsigned int n);
int main()
{
int n;
cout<<"Enter a positive number";
cin>>n;
cout<<"!"<<n<<"=:"<<Factorial(n);
return 0;
}
5/9/2024 Lidiya T. (MSc) 24
Example
int Factorial (unsigned int n)
{
if (n = = 0)
return 1;
else
return (n*(factorial(n -1));
}

5/9/2024 Lidiya T. (MSc) 25


5/9/2024 Lidiya T. (MSc) 26

You might also like