CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs...
-
Upload
irene-parker -
Category
Documents
-
view
219 -
download
2
description
Transcript of CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs...
![Page 1: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/1.jpg)
CS1201: PROGRAMMIN
G LANGUAGE 2
FUNCTIONS
![Page 2: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/2.jpg)
OVERVIEW
• What is a Function?• Function Prototype Vs Decleration• Highlight Some Errors in Function Code• Parameters Vs Arguments• Method Passing• Function Recursion• Function Overloading
![Page 3: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/3.jpg)
Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU
FUNCTIONS
3
![Page 4: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/4.jpg)
WHAT IS A FUNCTION?
• A function groups number of program statements into a unit and gives it a name.
• Functions are used to reduce program size.• Any sequence of instructions that appear more than once in a
program is a candidate for being made into function.• Functions can be called several times in the same program,
allowing the code to be reused.
![Page 5: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/5.jpg)
FUNCTIONS
Value returning functions:
functions that have a return type.
These functions return a value of a specific data type using the return statement.
Void functions:
functions that do not have a return type.
These functions do not use a return statement to return a value.
Just like variables you must tell the compiler about the functions: Declaration before it is called Definition before it is called
![Page 6: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/6.jpg)
USER DEFINED FUNCTIONS
C++ programs usually have the following form:
// include statements // function prototypes // main() function // function definitions
![Page 7: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/7.jpg)
FUNCTIONS INPUT AND OUTPUT
![Page 8: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/8.jpg)
FUNCTION DEFINITION A function definition has the following syntax:
<type> <function name>(<parameter list>){
<local declarations> <sequence of statements> }
For example: Definition of a function that computes the absolute value of an integer: int absolute(int x){ if (x >= 0) return x; else return -x; }
![Page 9: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/9.jpg)
FUNCTION CALL
A function call has the following syntax: <function name>(<argument list>)
Example: int distance distance = absolute(-5);
![Page 10: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/10.jpg)
ARGUMENTS AND PARAMETERS
• one-to-one correspondence between the arguments in a function call and the parameters in the function definition.
int argument1;double argument2; // function call (in another function, such as main) result = thefunctionname(argument1, argument2);
// function definition int thefunctionname(int parameter1, double parameter2){// Now the function can use the two parameters// parameter1 = argument 1, parameter2 = argument2}
![Page 11: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/11.jpg)
ABSOLUTE VALUE CODE#include <iostream>using namespace std;int absolute (int);// function prototype for absolute()int main(){ int num, answer; cout << "Enter an integer (0 to stop): "; cin >> num;
while (num!=0){ answer = absolute(num); cout << "The absolute value of " << num << " is: " << answer << endl; cin >> num; }
return 0; } // Define a function to take absolute value of an integer int absolute(int x){ if (x >= 0) return x; else return -x; }
![Page 12: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/12.jpg)
FUNCTION PROTOTYPE (DECLARATION)
• The function prototype declares the input and output parameters of the function.
• The function prototype has the following syntax: <type> <function name>(<type list>);
• Example: A function that returns the absolute value of an integer is: int absolute(int);
![Page 13: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/13.jpg)
FUNCTION PROTOTYPE (DECLARATION)
• Declaration tells the compiler that at some point later we will present a function called absolute
• Notice that the function declaration is terminated by a semicolon
• Function declarations are also called prototypes
![Page 14: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/14.jpg)
DIFFERENT FUNCTION COMPONENTS
![Page 15: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/15.jpg)
FUN
CTIO
N S
YN
TAX
15
![Page 16: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/16.jpg)
FUNCTION DEFINITION
• The function body is composed of the statements that make up the function.
• The function definition can be placed anywhere in the program after the function prototypes.
• If a function definition is placed before main(), there is no need to include its function prototype.
![Page 17: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/17.jpg)
FUNCTION DEFINITION• The decelerator must agree with the prototype: It must use the same
function name, have the same argument types in the same order (if there are arguments), and have the same return type.
• Notice that the decelerator is not terminated by a semicolon
• When the function is called, control is transferred to the first statement in the function body.
• The other statements in the function body are then executed, and when the closing brace is encountered, control returns to the calling program.
![Page 18: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/18.jpg)
ABSOLUTE VALUE (ALTERNATIVE)Note that it is possible to omit the function prototype if the function is placed before it is
called.#include <iostream>using namespace std;int absolute(int x){ if (x >= 0) return x; else return -x; } int main(){
int num, answer; cout << "Enter an integer (0 to stop): "; cin >> num;
while (num!=0){ answer = absolute(num); cout << "The absolute value of " << num << " is: " << answer << endl; cin >> num; }
return 0; }
![Page 19: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/19.jpg)
EXAMPLES:1. Write a Function larger, which returns the larger of the two
given integers.
2. Write a Function Square, which returns the square of the given integer.
3. Write a function number_type. The function should output the number and message saying whether the number is positive, negative, or zero.
19
![Page 20: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/20.jpg)
EX
AM
PLE
: WITH
RE
TUR
N
VALU
E
Double larger ( double x , double y ){
double max;if ( x >= y )
max = x;else
max = y;return max;
}……..Cout << “The larger of 5 and 6 is “ << larger(5 , 6) << endl;……….
20
Solution Ex 1
![Page 21: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/21.jpg)
EX
AM
PLE
: WITH
RE
TUR
N
VALU
E
int square (int x){
return x*x;}int main ( ){
int number;cout<<"Enter any number to Calculate the square
of this number ";cin>>number;cout<<endl;cout<<"the square of "<<number<<" is "
<<square(number)<<endl;return 0;
}
21
Solution Ex 2
![Page 22: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/22.jpg)
EX
AM
PLE
: WITH
OU
T R
ETU
RN
VALU
E
Void number_type ( int x){
if ( x > 0 )cout << x << “ is positive.”
<< endl;else if ( x < 0 )
cout << x << “ is negative.” << endl;
elsecout<< x << “is a
zero.”<<endl;}……..Number_type( 5 );……….
22
Solution Ex 3
![Page 23: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/23.jpg)
FINDING ERRORS IN FUNCTION CODE
int sum(int x, int y){
int result;result = x+y;
}
this function must return an integer value as indicated in the header definition (return result;) should be added
![Page 24: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/24.jpg)
FINDING ERRORS IN FUNCTION CODEint sum (int n){ if (n==0)
return 0;else
n+sum(n-1);}
the result of n+sum(n-1) is not returned; sum returns an improper result, the else part should be written as:- else return n+sum(n-1);
![Page 25: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/25.jpg)
FINDING ERRORS IN FUNCTION CODE
void f(float a);{
float a;cout<<a<<endl;
}; found after function definition header. redefining the parameter a in the function
void f(float a){
float a2 = a + 8.9;cout <<a2<<endl;
}
![Page 26: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/26.jpg)
FINDING ERRORS IN FUNCTION CODE
void product(void){
int a, b, c, result;cout << “enter three integers:”;cin >> a >> b >> c;result = a*b*c;cout << “Result is” << result;return result;
}
According to the definition it should not return a value , but in the block (body) it did & this is WRONG. Remove return Result;
![Page 27: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/27.jpg)
METHODS OF PASSING
There are 3 primary methods of passing arguments to functions:
pass by value, pass by reference, and pass by address. ( will be addressed later)
![Page 28: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/28.jpg)
PASSING ARGUMENTS BY VALUE• By default, arguments in C++ are passed by value.• When arguments are passed by value, a copy of the argument is passed
to the function.• Because a copy of the argument is passed to the function, the original
argument can not be modified by the function.
![Page 29: CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1ad47f8b9ab05997268e/html5/thumbnails/29.jpg)
ADV. AND DISADV. OF PASSING BY VALUE
A D V A N T A G E S
Arguments passed by value can be: variables (eg. x), literals (eg. 6), or expressions (eg. x+1)
or (eg foo()). Arguments are never
changed by the function being called, which prevents side effects.
D I S A D V A N T A G E S
Copying large structs or classes can take a lot of time to copy, and this can cqause a performance penalty, especially if the function is called many times.