0% found this document useful (0 votes)
7 views29 pages

Lecture 5

Lecture notes on hardware
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)
7 views29 pages

Lecture 5

Lecture notes on hardware
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/ 29

Programming

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:

void sayHello ( ) { void printAmount ( double amt ) {


cout << “Hello, “; cout << “Amount: ” << amt;
cout << ”welcome to Programming }
class”;
}
Function Call
• A function call is a statement that causes the function to
execute.
• When a function is called, the program leaves the current section of
code and moves to the line of code which begins the function.
• After executing the function code, execution goes back to the line of
code where the function call was made.
• Any data computed and returned by the function replaces the
function in the original line of code where the function call was
made.
Function Call
• To call a function, use the void printMessage ( ) {

function name followed by ( ) cout << “This is a function”;

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 Prototypes using namespace std;


// function prototypes
void first ( );
void second ( );
int main ( ){
cout << “Inside function, main.\n”;
first ( ); //function call
second ( ); //function call
cout << “Back to function main.\n”;
return 0; }
void first ( ){
cout << “Inside function, first.\n”;
}
void second ( ){
cout << “Inside function, second.\n”;
}
Passing Data into a function
• When a function is called, the program may send values into
the function.
• Values sent into a function are called arguments.
double results = pow (2.0, 4.0);
• Variables in the function that hold the values passed as
arguments are parameters.
void printAmount ( double amt ) {
cout << “The amount is ” << amt;
}
Function with Parameters
#include <iostream>
using namespace std;
displayValue ( 19 ); //function call
// function prototype
void displayValue ( int );

int main ( ){ void displayValue ( int num ){


cout << “Passing value to displayValue.\n”; cout << “Value is ” << num << endl;
displayValue ( 19 ); //function call }
cout << “Back to main function.\n”;
return 0;
}
void displayValue ( int num ){
cout << “Value is ” << num << endl;
}
#include <iostream>
Function with Parameters using namespace std;

// 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

total = sum (val1, val2);


15

25

int sum ( int num1, int num2 ) {

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>

Local Variable Example using namespace std;

void test( ); function prototype


Function main
int main( ) {
int num = 1; //Local variable
int num = 1; This num variable is
cout << “Function main \n”;
only visible in main.
cout << “Local variable: “<< num;
test( ); //function call
return 0;
}
Function test void test( ) {
int num = 20; //Local variable
int num = 20; This num variable is
cout << “Function Test \n”;
cout << “Local variable: “<< num;
only visible in test.
return 0;
}
#include <iostream>
Global Variable Example using namespace std;

void test( ); function prototype


Int num = 12; //Global variable

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;
}

You might also like