Lecture 5
Lecture 5
Lecture 5
Lecture Outline
• Functions
• Function types
• Function declaration
• Calling functions
• Function parameters
• Return statements
• Function prototype
• Local and Global Variables
• Default Arguments
Introduction
• A function is a collection of statements that performs a specific
task.
• Functions are commonly used to break a problem down into
small manageable pieces.
• They are used to break a problem down into small manageable
pieces.
• Instead of writing one long function, several small functions that each
solve a specific part of the problem can be written.
• Functions help simplify programs.
int main(){ int main ( ) {
statement; statement;
statement; statement;
statement; }
statement; void function1 ( ) {
statement; statement;
statement; statement;
statement; }
statement;
void function2 ( ) {
statement; statement;
statement; statement;
statement; }
statement;
void function3 ( ) {
statement;
statement;
statement;
statement;
}
}
Types of Functions
• Built-in or Predefined functions:
• These are functions that come with the programming language to
help perform a task.
• They are readily available for use and are mostly stored in the
Standard Library Function.
• The source code is hidden from the user
• User-defined functions:
• These are created by the programmer to perform a specific task.
• Commonly used to break a program into smaller sections with each
performing a specific task.
Function Definition
• A function definition contains the statements that make up the
function.
• The definition creates the function.
• Function definition has the following parts:
Return type A function can send a value to the part of the program that executed it. The
return type is the data type of the value that is sent from the function.
Function name Use a descriptive name. identifier naming rules applies here.
Parameter list The program can send data into a function. The parameter list is a 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.
Function Definition
return_type name ( formal_parameter, formal_parameter… ) {
statement;
statement;
return value;
}
Function Definition
double add ( int a, int b ) { void ans ( int a, int b ) {
return a+b; if (a > b) {
} cout << a << “ is greater.\n”;
}else{
cout << b << is greater.\n”;
}
int main ( ) { }
cout << “Hello World”;
return 0; void displaymessage ( ) {
} cout << “This is a void function”;
}
Return Type
• If a function returns a value, the type of the value must be
indicated.
int main ( )
• If a function does not return a value, its return type is void:
and ; }
• Example: printMessage ( );
• When called, program executes int main ( ) {
the body of the called function cout << “Hello from main.\n”;
• After execution the function printMessage ( );
terminates and execution returns cout << “Back to main.\n”;
to the calling function. return 0;
• The return type is not listed in the }
function call
Example: Function Call
#include <iostream>
int a ( ) {
using namespace std;
return 5;
int a ( ) { }
return 5;
}
int main( ) {
int main( ) {
int b;
int b;
b = 10; b = 10;
cout << a( ) + b << endl; cout << a( ) + b << endl;
return 0; return 0;
} }
#include <iostream> void first ( ){
using namespace std; cout << “Inside function, first.\n”;
void first ( ){ }
cout << “Inside function, first.\n”;
} void second ( ){
void second ( ){ cout << “Inside function, second.\n”;
cout << “Inside function, second.\n”; }
}
int main ( ){
int main ( ){
cout << “Inside function, main.\n”;
cout << “Inside function, main.\n”;
first ( );
first ( );
second ( );
second ( );
cout << “Back to function main.\n”;
cout << “Back to function main.\n”;
return 0;
return 0;
}
}
Function Prototypes
• A function prototype eliminates the need to place a function
definition before all calls to the function.
• Function prototypes are also known as function declarations.
• Place the function definition before calling the function
definition.
• The prototype looks similar to the function header, except there
is a semicolon at the end.
• Function prototypes are usually placed near the top of a program so
the compiler will encounter them before any function calls
#include <iostream>
// function prototype
add ( num1, num2); int add ( int, int );
int main ( ){
int num1, num2;
int add ( int a, int b ){
cout << “Enter two numbers: ”;
return (a + b); cin >> num1 >> num2;
} cout << “Answer: ” << add ( num1, num2);
return 0;
}
int add ( int a, int b ){
return (a + b);
}
Functions with Parameters
• A parameter can also be called a formal parameter or a formal
argument.
• An argument can also be called an actual parameter or an
actual argument.
Passing by Value
• When an argument is passed into a parameter, only a copy of
the argument’s value is passed.
• This is known as pass by value
• Changes to the parameter do not affect the original argument.
int arg = 6;
compute ( arg )
arg num
5 5
Argument in calling function Parameter in function
#include <iostream>
Pass Data by Value using namespace std;
void changeMe ( int ); //Prototype
int main ( ) {
int num = 14;
cout << “Number is ” << num << endl;
changeMe ( num );
cout << “After change number is ” << num << endl;
return 0;
}
void changeMe ( int val ){
val = 19; //Change the value
cout << “Now the value is ” << val << endl;
}
Returning a Value from a function
• The return statement causes a function to end immediately.
• Statements that follow the return statement will not execute
• A function can return a value back to the statement that called
the function.
• The return statement can be used to return a value from
function to the point of call.
argument
argument
Function Return value
argument
argument
Returning a Value from a Function
int sum ( int num1, int num2, int num3)
{
int results;
results = num1 + num2 + num3;
return results;
}
Returning a Value from a Function
25
35
return num1 + num2;
}
Local and Global Scope Variables
• A local variable is defined inside a function and is not accessible
outside the function.
• Variables defined inside a function are local to that function.
• They are hidden from the statements in other functions, which
normally cannot access them.
• A global variable is defined outside all functions and is
accessible to all functions in its scope.
• A function’s local variables exist only while the function is
executing.
• This is known as the lifetime of a local variable
#include <iostream>
int main( ) {
cout << “Function main \n”;
cout << “Global variable: “<< num;
test( ); //function call
return 0;
}
void test( ) {
cout << “Function Test \n”;
cout << “Global variable: “<< num;
num = 50;
cout << “Change value: “<< num;
return 0;
}
Default Arguments
• Default arguments are passed to parameters automatically if no
argument is provided in the function call.
• A default argument is passed to the parameter when the actual
argument is left out of the function call.
• The default arguments are usually listed in the function
prototype.
void showArea(double = 20.0, double = 10.0);
• The default arguments are only used when the actual
arguments are omitted from the function call.
Overloading Functions
• Two or more functions may have the same name, as long as
their parameter lists are different.
• Sometimes you will create two or more functions that perform
the same operation, but use a different set of parameters or
parameters of different data types.
int square (int ); //function prototype int square (int number ){
double square (double ); //function prototype return number * number;
}
double square (double number ){
return number * number;
}