C++ Ch4 (1)
C++ Ch4 (1)
Function
eh E .
el ayn
By B
Learning Objectives
• Pre-defined functions
• User-defined functions
– Defining, declaring and calling
– Default parameters
– recursive functions
– Scope of variables
Introduction
A function is a group of statement that is executed when it is called
Functions are building blocks of the programs.
Named differently in other languages. Ex: procedures,
subprograms, methods
They make the programs more modular and easy to read and
manage. All C++ programs must contain the function main( ).
The execution of the program starts from the function main( ) and
ends in main() function
Reasons for Function
To make the program development more manageable
To remove repetition of codes
For software reusability purpose
For maintainability
For readability
C++ Functions
• C++ Libraries are full of functions for our use
• Two types of functions:
• That return a value (int, float, char,…)
• That do not return a value (void)
• In algebra, a function is defined as a rule or correspondence
between values, called the function’s arguments, and the unique
value of the function associated with the arguments
• Example: f(x) = 2x+5, f(1) = 7, f(2) = 9, and f(3) = 11
– 1, 2, and 3 are arguments
– 7, 9, and 11 are the corresponding values
Predefined/Standard Functions
• Predefined functions
Make sure to use the required
– Part of the C++ language
– Provided in function libraries #include file
• Examples:
abs(x), sin(x), log(x), pow( x, n)
• These functions will return a value
– To be printed cout << sin (x);
– To be assigned y = pow (3, 4.5);
– To be used in an expression 3.14 * sqr(r)
Predefined/Standard Functions
Predefined/Standard Functions
User-defined Functions
• Although C++ is shipped with a lot of standard functions, these functions are
not enough for all users
• Therefore , C++ provides its users with a way to define their own functions
(or user-defined function)
• User-defined functions in C++ are classified into two categories:
• Value-returning functions
– Functions return a value of a specific data type using the return statement,
which we will explain shortly.
• Void functions
Syntax:
<return_type>FunctionName(<parameter-list>);
• The prototype above yields the following information to the
compiler:
– func is the function name
– The function is called with two arguments: the first
argument is of type int, the second of type double
– The return value of the function is of type long
Function Declaration (Prototype)
• A function declaration is simply the function's header, followed by a
semicolon
• A function declaration is like a variable declaration;
• its purpose is simply to provide the compiler with all the information
it needs to compile the rest of the file
• The compiler does not need to know how the function works (its
body).
• It only needs to know the function's name, the number and types of
its parameters, and its return type
• This is precisely the information contained in the function's header.
Function Definition
A function that includes the body of the function
A function definition is the complete function: header and body
• Parameter list
– Comma separated list of arguments
– Data type needed for each argument
#include <iostream>
using namespace std;
int add( int, int);
int square(int x)
int add( int, int, int);
int main() { {
int a, b,c; return (x*x);
cin>>a>>b>>c; }
add(a,b,c);
add(a,b); float square (float x)
return 0; } {
int add( int x, int y) { return (x*x);
int result= x+y;
}
cout<<result<<endl; }
int add( int x, int y, int z) {
int sum= x+y+z;
cout<<sum<<endl; }