cs1201: programming language 2 functions. overview what is a function? function prototype vs...
DESCRIPTION
FUNCTIONS NOUF ALJAFFAN (C) CSC 1201 COURSE AT KSU 3TRANSCRIPT
CS1201: PROGRAMMIN
G LANGUAGE 2
FUNCTIONS
OVERVIEW
• What is a Function?• Function Prototype Vs Decleration• Highlight Some Errors in Function Code• Parameters Vs Arguments• Method Passing• Function Recursion• Function Overloading
Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU
FUNCTIONS
3
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.
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
USER DEFINED FUNCTIONS
C++ programs usually have the following form:
// include statements // function prototypes // main() function // function definitions
FUNCTIONS INPUT AND OUTPUT
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; }
FUNCTION CALL
A function call has the following syntax: <function name>(<argument list>)
Example: int distance distance = absolute(-5);
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}
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; }
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);
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
DIFFERENT FUNCTION COMPONENTS
FUN
CTIO
N S
YN
TAX
15
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.
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.
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; }
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
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
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
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
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
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);
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;
}
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;
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)
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.
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.