Sei sulla pagina 1di 36

Introduction to Computer Programming

Lab 06

Functions
t

What we will study today


What are functions? How are they defined ? How are they declared ? What values are passed to functions ? What values do functions return ?

Built in Function
Finding Square Root
#include <cmath> // defines the sqrt() function #include <iostream.h> int main() { // tests the sqrt() function: for (int x=0; x <=9; x++) cout << "\t" << x << "\t" << sqrt(x) << endl; }

Function
A Function is a block of statements, executed every time when it is called. (Or we can say that) A Function can be thought of as a mini Program, where a group of statements are executed when the function is called.

Functions
You've already encountered a few functions: main, cout and cin. The main function is special, in the way that it's called automatically when the program starts. In C++, and other programming languages, you can create your own functions.

Types of functions
Two types of functions: 1. Functions that return a value 2. Functions that do not return a value

A typical Function
Functions have 5 main features: 1. The DATA TYPE is the data type of the RETURN VALUE of the function. 2. The NAME is required as an identifier to the function, so that the computer knows which function is called. Naming of functions follows the same set of rules as the naming of variables. 3. Functions can take ARGUMENTS - a function might need extra information for it to work. Arguments are optional. 4. The function BODY is surrounded by curly brackets and contains the statements of the function. 5. The RETURN VALUE is the value that is passed back to the main program. Functions exit whenever you return a value.

Function Syntax
Function Declaration / Prototype Function Call Function Body

Function prototype
The function prototype is a single line summary of the function Tells the number of inputs and output Tells the type of inputs and output (int, float, double, char). It is normally located at the top of your program listing Usually above the main function and below the #include files definition A function prototype has the form
output Parameter functionName(input parameter, input parameter, );

Function Return type


A function can only return either a single variable OR nothing at all Multiple values are return using pointers and that can be specify in the input parameters. The return type indicates the type of variable (if any) that the function outputs (returns) Return type can be of any type (e.g. int, float, char) OR void (indicating the function does not return anything)

Function Name
A function name can be anything but it is a good idea that the name used tells you what the function does For example if the function used for deleting any student data (use name deleteStudentData) We can declare more than one functions with the same name But there input parameters should be different. For example
void deleteStudentData (void); // delete all the students data
void deleteStudentData (int studentID); // delete the student data with specific ID.

Function Inputs
Most functions will use input values It is possible to write a function that has no inputs Example void deleteStudentData (void); Input values must be separated by commas if there are more than one input value within the brackets () after the function name Example void deleteStudentData (int studentID,float CGPA) Each entry in this list of arguments must identify the input variable type and define a name by which that input can be referred to In the case where there are no inputs to a function the keyword void appears instead of the list of arguments

Example
Consider our function to calculate the factorial of an integer Inputs: A single integer (that we will referred by its name) Function Name: factorial Return value: integer The prototype of this function is: int factorial(int value);

Function Implementation
ALL functions are typically located either before the main function OR after the main function A function implementation is very like the main function itself It starts with the function description (effectively the same as the prototype)

Without the ;
It is bounded by a pair of chain brackets {} (Local) variables can be defined within (at the start of) the function If the function has an output, return command must be present in the function

You cannot refer to a variable that is defined in any other function directly why?

The return command is used as follows: return (variableName); This return command can :
Appear only in a function if it is returning output value in its prototype Causes the function to immediately end and output the value in the single variable being returned You can only return a single variable using the return statement

Declaration of Function
//Prototype / Declaration

return-value-type function-name( argument--type-list) ;


void main ( ) { Statement1; function-name( argument) ; //Call Of Function }

Function body syntax


//Function Body return-value-type function-name( argument-list ) { statements; }

Example 1
#include<iostream.h> void print(void); //-------------Function Declaration int main() { cout<<"\nDisplaying from Main Function"; print(); //-------------Function Call return 0; } void print() //-------------Function Body { cout<<"\n\n*****Displaying From print Funcation****** \n"; }

Example 2
#include < iostream.h > int i ; //---------- Global Variable void f ( void ) ; //----------Function Declaration main ( ) { i = 10 ; cout<< "\n within main i = " << i ; f ( ) ; //-------------Function Call cout<<endl; }

void f ( void ) //-------------Function Body { i = 20 ; cout<< "\n Inside function f , i = " << i ;

Example
int factorial (int value) { int counter=0; int result=1; for (counter=1; counter<=value; counter++) { result= result * counter; } return(result);

Calling the Function


If the function returns a value, you may store the result in a suitable variable type using the assignment (=) operator Examples of function calls would be: variableName=functionOne(); variableName=functionTwo(variableA,variableB); functionThree(variableA,variableB); A function can be called from the main function or from any other function When a function is completed the program returns to the next line after where the function was called from

#include <iostream.h> int factorial (int value); // function prototype void main (void) { int scanValue = 0; cout<<\n Enter the input value:; cin>>scanValue; } int res; res= factorial (scanValue); // function calling cout<<\nThe Result is = << res; res = factorial (10); // function calling cout<<\nThe Result is = << res ;

int factorial (int value) // function implementation { int counter=0; int result=1; } for (counter=1; counter<=value; counter++) { result= result * counter; } return (result);

Example
Consider our example:
int factorial (int value) { int counter=0; int result=1;

void main (void) { 4/29/2012 int result = factorial (4); cout<<The result is<<result; }

24

for (counter=1; counter<=value; counter++) { result= result * counter; } return(result); }

Example
Consider our example:
int factorial (int value) { int counter=0; int result=1;

value = 4 counter = 0

25
4/29/2012

for (counter=1; counter<=value; counter++) { result= result * counter; } return(result); }

Example
Consider our example:
int factorial (int value) { int counter=0; int result=1;

value = 4 counter = 0 result = 1

26
4/29/2012

for (counter=1; counter<=value; counter++) { result= result * counter; } return(result); }

Example
Consider our example:
int factorial (int value) { int counter=0; int result=1;

value = 4 counter = 1 result = 1

27
4/29/2012

for (counter=1; counter <= value; counter++) { result= result * counter; } return(result); }

Example
Consider our example:
int factorial (int value) { int counter=0; int result=1.0;

value = 4 counter = 1 4/29/2012 result = 1 * 1 = 1

28

for (counter=1; counter <= value; counter++) { result= result * counter; } return(result); }

Example
Consider our example:
int factorial (int value) { int counter=0; int result=1;

value = 4 counter = 2 result = 1

29
4/29/2012

for (counter=1; counter <= value; counter++) { result= result * counter; } return(result); }

Example
Consider our example:
int factorial (int value) { int counter=0; int result=1.0;

value = 4 counter = 2 result = 1* 2 = 2

30
4/29/2012

for (counter=1; counter <= value; counter++) { result= result * counter; } return (result); }

Example
Consider our example:
int factorial (int value) { int counter=0; int result=1;

value = 4 counter = 3 4/29/2012 result = 3 * 2 = 6

31

for (counter=1; counter <= value; counter++) { result= result * counter; } return (result); }

Example
Consider our example:
int factorial (int value) { int counter=0; int result=1;

value = 4 counter = 4 4/29/2012 result = 6 * 4 = 24

32

for (counter=1; counter <= value; counter++) { result= result * counter; } return (result); }

Example
Consider our example:
int factorial (int value) { int counter=0; int result=1.0;

value = 4 counter = 5 result = 24

33
4/29/2012

for (counter=1; counter <= value; counter++) { result= result * counter; } return (result); }

34

Advantages of using functions


4/29/2012

Code becomes more readable easy to modifiable by second person. Code becomes more compact and small.

If code is small then the exe file will be also small, which will enable your executable file to fit in a very small RAM.

Lab Task 2
Write a program in C++ that have a function of square(). The user should enter the number and the code should display the square of the given number.

Lab Task 2

(code)

void square(double); --------------Function Prototype void main() { double x; cout <<"\n main(), before calling "; cout<<"\nEnter the number to find square="; cin>>x; square(x); --------------Function Call

} void square(double x) --------------Function Body { x=x*x; cout <<"\n After calling in square(), x = " << x<<endl; }

Potrebbero piacerti anche