You are on page 1of 31

Functions

Introduction to Computer Programming

Syed Nasir Mehdi


Lecturer in CS
nasirsmehdi@ciitsahiwal.edu.pk
Why functions ?

• Divide and conquer (several small functions are written to


solve a specific part of the problem, instead of writing
one long function that contains all of the statements)
• Code reuse

1A-
2
Parts of function definition
 Return type: The return type is the data type of the value that is
sent from the function.
 Name: You should give each function a descriptive name. Same
rules as variable names
 Parameter list: The program can send data into a function. The
parameter list is list of variables that hold the values being
passed to the function
 Body: The body of a function is the set of statements that
perform the function’s operation. They are enclosed in a set of
braces.
Function call
• A function call is a statement that causes a function to
execute.

Function definition

• A function definition contains the statements that make


up the function.

1A-
4
Function definition
Void Functions

• Funtions which perform one or more statements and


then terminate without returning a value are called void
functions.
• void displayMessage ( )
• { cout << “Hello from the function displayMessage \n”; }
• Function return type is void and has no return statement.
• It simply displays a message and exits.
1A-
6
Calling a function

A function is executed when it is called. Main is called


automatically. Other functions must have a call for
their execution.
When a function call is made the program braches to
that function’s definition and statements in its body
are executed before returning to the main function.
1A-
8
1A-
9
1A-
10
1A-
11
Function Prototype

• The prototype looks similar to the function header,


except there is a semicolon at the end.
• void displayMessage ( ) ;
• It tells the compiler that the function displayMessage has
a void return type and uses no parameters.
• Function prototypes are also called function declarations.
• You must place either function definition or the function
prototypes ahead of all calls to the function otherwise the
program will not compile. 1A-
12
1A-
13
Sending data into a function

• When a function is called, the program may send values


into the function
• Values that are sent into a function are called arguments.
• E.g result=pow (2.0, 4.0);
• By using parameters you can design your own functions
that accept data this way.

1A-
14
• void displayValue (int num)
• { cout << “The value is ” <<num << endl;
•}
• Int variable num is defined inside the parenthesis (int
num)
• The variable num is a parameter. The integer value is
accepted as an argument.
• Parameters are special purpose variables that are defined
inside the parenthesis of the function definition. They are
separate from the arguments of function call.
1A-
15
Some ground rules of functions
• In the function call you must not write the data type of
the argument variable , simply write the variable name.
• displayVaule (int x); //Error
• displayValue ( x ); Page 311
• In the function prototype its not necessary to write the
name of the parameter variable, only its data type is
required.
• void displayValue (int num) ; could be written as
• void displayValue ( int ) ;
• In essence parameter variables are initialized to the value1A-
16

of their corresponding arguments.


Ground rules

• In each function header, each parameter variable in


parameter list must have a data type listed before its
name. e.g
• void showSum (int num1, num2, num3) // Error
• void showSum (int num1, num2, num3)

1A-
17
Function rules

• If you pass an argument whose type is not the same as


the parameter’s type, the argument will be promoted or
demoted automatically. E.g
• displayValue (4.7); // The decimal portion would be trunc
• Like all variables, parameters have a scope. The scope of
a parameter is limited to the body of the function that
uses it.
1A-
18
Function prototypes

• Function prototype eliminates the need to place a


function definition before all calls to the function.
• There are two ways for the compiler to know the
function’s return type, the number of parameters it uses
and the type of each parameter.
• Either place the definition of the function before the
function call or declare the function with a function
prototype.

1A-
19
1A-
20
Passing data by value

• The values that are stored in the parameter variables are


copies of the arguments. Normally, when a parameter’s
value is changed inside a function it has no effect on the
original argument.

1A-
21
1A-
22
Even though the parameter variable myValue is changed in the
changemMe function, the argument number is not modified. myValue
is only a copy of the number variable.
1A-
23
The return statement

• The return statement causes a function to end


immediately.

1A-
24
1A-
25
Returning a value from a function
• A function may send a value back to the part of the
program that called the function.
• Although several arguments may be passed into a
function, only one value may be returned from it.

1A-
26
Defining a value returning function
• You have seen void functions. They don’t return a value.
Its mentioned by key word ‘void’ in the function header.
• Local variable result is defined in the function.

1A-
27
Calling a Value-Returning Function

1A-
28
More about value returning
function

• A value-returning function returns a value of a specific


data type. You can use the function’s return value
anywhere that you can use a regular value of the same
data type.

1A-
29
Returning a boolean value

• Functions may return true or false values.

1A-
30
Thank You!

You might also like