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

FPCPP C2

The document discusses functions in C++. It defines functions as blocks of code that perform specific tasks and have advantages like reusability and modularity. There are two types of functions - built-in functions from libraries and user-defined functions. Built-in functions are called by including their header file and passing arguments. User-defined functions require declaration, definition, and calling. Functions are organized using parameters, arguments, scopes, and pass by value vs reference.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

FPCPP C2

The document discusses functions in C++. It defines functions as blocks of code that perform specific tasks and have advantages like reusability and modularity. There are two types of functions - built-in functions from libraries and user-defined functions. Built-in functions are called by including their header file and passing arguments. User-defined functions require declaration, definition, and calling. Functions are organized using parameters, arguments, scopes, and pass by value vs reference.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

1

FUNDAMENTALS OF

2
PROGRAMMING IN C++
FUNCTIONS
Functions 2
 A good plan for designing algorithm is
 to break down the task into smaller subtasks,
 Then decompose each of these subtasks into smaller sub-subtasks, and so forth.
 Eventually, the subtasks become so small that they become trivial to implement.
 This method is called top-down design( mostly divide and conquer.)
 The same is applied to a program by
 breaking the program's task into subtasks
 and solve these subtasks by sub-algorithms as functions.
• In C++, programs are organized into functions.
• A program must have at least one function, called main function.
… 3
• A function is a block of codes(actually related)grouped together to perform a specific task
in a program.
• Grouping related codes to perform a specific task will have the following advantages:
• enhance readability,
• ease of maintenance,
• allows reusability, – define once and use whenever necessary.
• allows to unit test the code,
• one way of putting the principle of abstraction into effect
• and etc.
Categories… 4
• Functions, based on how they occurred into the program, can be categorized as:
1. Built-in (predefined) functions
• are functions that comes with the compiler as a package in some library.
2. User defined functions
• are functions that explicitly defined by the programmers for their own purpose.
 If that’s how the functions appeared in a program, then the remaining question is:
 How could one use a built-in functions? and
 How could one define a function?
Using built-in functions 5
• In order to use a built-in function:
1. Include the library file, where the function is pre-defined.
2. Invoke( or call) the function from the main and
3. Pass arguments accordingly.
 Example: #include <cmath>
#include <iostream>
using namespace std;
int main(){
int a;
cout<<“Enter your number:”;
cin>>a;
cout<<“square root =“<<sqrt(a)<<endl;
return 0;
}
…for type casting 6
• Type casting is a technique used to convert the type of a certain data to other types.
• Type casting can be done:
1. Implicitly by the compiler
• The compiler automatically convert a type of a data into another type.
• Implicit casting is done from lowest order to the highest order types only.
• Done when assigning values only.

2. Explicitly by the programmer


• This is done by the programmer by using the general syntax:
• static_cast<type>(expression);
• type(expression);
• Or (type) expression; are all possible but the first one is the safest.
User-defined functions 7
• C++ programming language (others too)
• allows programmers to define their own functions to organize their codes in a program.
• Once the functions has been defined, they can be used by calling just like the built-ins’.
• To create a user-defined a function, consider the following points:
• name to your function, input to your function and output from your function.
• Defining and using a function involves:
• Function Declaration
• Function Definition and
• Function Calling
Declaration 8
 A function declaration describes how the function should be invoked.
 It tells you everything you need to know in order to write a call to the function:
 the name of the function,
 the type, order and number of parameters it will accept as input
 the type of the value the function would return as a result.
 Declarations specifies:
 Name of a function - is a unique identifier for the function.
 A return type - states the data type of a value the function returns.
 Void used when a function returns nothing at all.
 A list of parameters (signatures) - are contracts specifying the input to a function.
… 9
 Function declaration (prototype) is needed
 only when the function definition is done after the main function.
 Syntax:
data_type Fun_name(data_type Param_name);
 Note:
 Function declaration must be placed before the main function.
 Function declaration is a must,
 If the function definition is specified after the point where the function is invoked.
Definition 10
 A function definition describes how the function computes the value it returns.
 It consists of a function header followed by a function body.
 The function header is a declaration without a semicolon.
 The function body contained with executable set of instructions.
 Contains
 A logic to compute and returns the expected output value whenever it’s invoked.
 When the function is invoked:
 The arguments on the call plugged into the parameters in the definition, and the logic computes that into
an output.
… 11
 Syntax:
data_type Fun_name(data_type Param_name){
/*
Statements
*/
}
Invocation 12
 In order to be executed a function must be invoked (called):
 directly from inside of the main function or
 Indirectly from the main function
 Calling a function means making the instruction of the function to be executed.
 A function call consists of
 the function name followed by the call operator brackets “()” inside which zero or more comma-
separated arguments appear.
 The number, type and order of arguments should match the number of function parameters.
Exercises 13
 …
Parameters Vs Arguments 14
 Parameters of a function are:
 placeholders for the input to a function, or
 contracts that should be fulfilled to use a function, or
 list of variables used by the function to perform its task.
 Arguments to a function are:
 actual values plugged-in to parameters while calling a function.
 While invoking a function:
 the order, type and number of arguments must match with the corresponding parameters defined
in the function declaration(or definition).
 Arguments can be plugged-in to parameters:
• Either using pass by value or pass by reference techniques.
Pass by value 15
 In this way:
 The copy of the value of arguments plugged-in into parameter variables.
 In call by value technique,
 The change made inside a function would not affect the arguments.
 Calling by value
 is the default invocation technique, that we have been using till this point.
Pass by reference 16
 In this way:
 The address (or reference) of argument variables plugged-in into parameter variables.
 In pass by reference technique,
 The change made inside a function would affect the arguments
Global Vs. local variables 17
 Everything defined at the program scope level (outside functions) is said to have a global
scope.
 Meaning, that the entire program knows each variable and has the capability to change any of
them.
 Global variables are visible (“known”) from their point of declaration down to the end of the
program.
 Each block in a program defines a local scope.
 Thus the body of a function represents a local scope.
 The parameters of a function have the same scope as the function body.
 Variables defined within a local scope are visible to that scope only.
 Hence, a variable need only be unique within its own scope.
 Local scopes may be nested, in which case the inner scope overrides the outer scopes.
… 18
 A local scope overrides the global scope.
 Having a local variable with the same name as a global variable makes the latter inaccessible inside
the local scope.
int x;
void fun1(int x)
{
//…
}
 The global x is inaccessible inside fun1(), because it is overridden by the local x parameter.
 The scope operator ‘::’ resolves the global variable access in the local scope.
Automatic Vs. static variables 19
 The terms automatic and static:
 describes what happens to local variables when a function returns to the calling procedure.
 If the variables are automatic:
 their content will be erased when their function ends.
 Local variables are automatic by default.
 A variable explicitly can be declared as automatic using keyword auto.
 E.g. all variables below are automatic:
int main()
{
int x;
float y;
auto int z;

}
… 20
 Static variables retains their value after the end of their function.
 Global variables are static by default.
 A local variable can be made static using a key word static while declaration.
 Therefore, if a local variable is static, it too retains its value when its function ends-in case this
function is called a second time.
 E.g. j and k are static but i is automatic. int main()
{
int i;
static float j;
static int k;

}
Inline functions 21
 Frequent function calling:
 can lead to considerable performance penalty due to overheads associated with calling a function.
 The overhead can be avoided by defining an inline function.
 Inline function:
 expanded in a line it has been called from.
 or the whole code of the function being called is going to be inserted at the point of inline function
call.
 A function could not be inlined:
 If it is recursive, contains loops or the function size is too large.
Default Arguments 22
 Parameters can have default values.
 If a function with default arguments is called without passing arguments,
 then the default parameters are used.
 However, if arguments are passed while calling the function, the default arguments are
ignored.
 If there are default arguments
 They must be the rightmost (trailing) arguments in a function’s parameter list.
Arrays as parameter 23
 At some moment we may need to pass an array to a function as a parameter.
 In C++
 pass by value a complete block of memory as a parameter is not possible,
 but it is allowed to pass by address.
 Therefore, passing arrays as a parameter is by default pass by reference.
 To pass arrays as parameter, the function definition looks like:
void procedure (int arg[])
 In order to pass to this function an array declared as:
int myarray [40];
 It would be enough with a call like this:
procedure (myarray);
 Note: the name of the array holds the address of the first element in the array.
Function Overloading 24
 Overloaded functions are functions that have the same name, but different signature.
 Signature of a function is the number, order and type of its arguments.
 In C++, two functions can have the same name if the number and/or type of arguments passed
is different.
 These functions having the same name but different arguments are known as overloaded functions.
 The main advantage of function overloading is consistency and readability of the program.
Recursive Functions 25
 A recursive function is a type of function that consists a call to itself inside its definition.
 Simply, a function calling itself is called recursive function.
 A recursive function constitutes two blocks:
 The base case- a condition to end the recursive call.
 The recursive case- where the function calls itself.
 Generally, recursive implementation is slower than iteration.

You might also like