0% found this document useful (0 votes)
28 views25 pages

Circuit Chapter 3 - 4-1

Functions allow programmers to organize code into reusable blocks. There are two types of functions: library functions declared in header files, and user-defined functions created by the programmer. Functions are declared with a return type, name, and parameters. They are defined with a body between braces. Arguments are passed to functions by value, where the function receives a copy, or by reference, where changes to parameters also affect the arguments. Functions can be overloaded by having different parameters, enabling multiple functions to have the same name. This allows for polymorphism.

Uploaded by

habtamu desalegn
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)
28 views25 pages

Circuit Chapter 3 - 4-1

Functions allow programmers to organize code into reusable blocks. There are two types of functions: library functions declared in header files, and user-defined functions created by the programmer. Functions are declared with a return type, name, and parameters. They are defined with a body between braces. Arguments are passed to functions by value, where the function receives a copy, or by reference, where changes to parameters also affect the arguments. Functions can be overloaded by having different parameters, enabling multiple functions to have the same name. This allows for polymorphism.

Uploaded by

habtamu desalegn
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/ 25

Chapter three

function
Introduction to function
✓ A function is a complete unit of executable code that can perform
specific task
✓ It is a named independent section of a C++ code that performs a
specific tasks and optionally returns value to the calling function.
✓ Function is considered as a fundamental building of the language
Syntax
General form: type-specifier function-name (argument
declaration)
{
body of the function;
return (expression);}
✓ Argument — is a comma separated list of variable names that
receive the values, when function is called, (parenthesis
required even no arguments)
Types of function
✓ There are two types of functions in C++ programming:

✓ Library Functions: are the functions which are declared in the


C++ header files such as ceil(x), cos(x), exp(x), etc.

✓ User-defined functions: are the functions which are created by the


C++ programmer, so that he/she can use it many times. It reduces
complexity of a big program and optimizes the code.
Cont. …
✓ There are two types of functions in C++ programming:

✓ Library Functions: are the functions which are declared in the


C++ header files such as ceil(x), cos(x), exp(x), etc.

✓ User-defined functions: are the functions which are created by the


C++ programmer, so that he/she can use it many times. It reduces
complexity of a big program and optimizes the code.
Cont. …
✓ A function provides a convenient way of packaging a
computational recipe, so that it can be used as often as required.

✓ Therefore, a function is a block of code designed to tackle a


specific problem.

✓ One of the best ways to tackle a problem is to start with the overall
goal, then divide this goal into several smaller tasks

✓ If your program does a lot, break it into several functions. Each


function should do only one primary task.
Cont. …
C++ functions generally adhere to the following rules.

✓ Every function must have a name

✓ Function names are made up and assigned by the programmer


following the same rules that apply to naming variables.

✓ All function names have one set of parenthesis immediately


following them.

✓ The body of each function, starting immediately after parenthesis


of the function name, must be enclosed by braces.
Declaring, defining and calling function

Declaring function
➢ The interface of a function (also called its prototype) specifies how
it may be used. It consists of three entities
➢ The function return type. This specifies the type of value the
function returns. A function which returns nothing should have a
return type void.
➢ 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.
Defining a function

➢ A function definition consists of two parts: interface (prototype) &


body.

➢ The brace of a function contains the computational steps


(statements) that computerize the function. The definition consists
of a line called the decelerator

➢ If function definition is done before the main function, then there is


no need to put the prototype, otherwise the prototype should be
scripted before the main function starts
Calling the function

➢ Using a function involves ‘calling’ it.

➢ 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.

➢ When a function call is executed, the arguments are first


evaluated and their resulting values are assigned to the
corresponding parameters, The function body is then executed.
Finally the return value (if any) is passed to the caller.
Cont. …
Cont. …
Cont. …
Function Parameters and arguments

➢ The parameters of a function are list of variables used by the


function to perform its task and the arguments passed to the
function during calling of a function are values sent to the function
➢ The arguments of function calling can be using either of the two
supported styles in C++: passing by value or passing by reference.
Passing by value
➢ A value parameter receives a copy of only the value of the argument
passed to it.
➢ As a result, if the function makes any changes to the parameters,
this will not affect the argument. For instance
Cont. …

➢ The single parameter of Foo is a value parameter.

➢ As far as this function is concerned, num behaves just like a local


variable inside the function.

➢ When the function is called and x passed to it, num receives a copy
of the value of x.
Cont. …

➢ As a result, although num is set to 0 by the function, this does not


affect x. the program produces the following output:

Num = 0

x = 10

➢ Passing arguments in this way, where the function creates copies of


the arguments passed to it is called passing by value.
Cont. …
Passing by reference

➢ The parameter of Foo is a reference parameter. Num will


correspond to x for this specific program as it x is sent by its
reference and not its value
Cont. …

➢ Any change made on num will be effected to x. Thus, the program


produces the following output: num = 0 x = 0

Global versus local variables

➢ 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.
Cont. …
Overloading function

➢ C++ enables you to create more than one function with the same name.
this is called function overloading

➢ The functions must differ in their parameter list, with a different type of
parameter, a different number of parameters, or both. Here's an example:

➢ Int myFunction(int x, int y);

➢ int myFunction (long x, long y);

➢ int myFunction (long x);

➢ myfunction() is overloaded with three different parameter lists

➢ The first and the second versions differ in the types of the parameter, and
the third differs in the number of parameter.
cont. ..

➢ The return types can be the same or different on overloaded functions.

➢ You should note that two functions with the same name and parameter
list, but different return types, generate a compiler error.

➢ New Term: Function overloading i s also called function polymorphism.


Poly means many, and morph means form: a polymorphic function is
many-formed.

➢ Function polymorphism refers to the ability to "overload" a function with


more than one meaning. By changing the number or type of the
parameters, you can give two or more functions the same function name,
and the right one will be called by matching the parameters used.
Demonstrating function overloading
cont. ..
cont. ..
cont. ..

➢ Analysis: - the double function is overloaded with int ,long, double and float

➢ In the body of the main program, eight local variables are declared.

➢ When double() function is called, the calling function does not distinguish
which one to call. It just pass in an argument, and the correct one is invoked

You might also like