Sie sind auf Seite 1von 10

CHAPTER 6 FUNCTIONS

Objectives

Introduces function How to create and use the user-defined functions How to pass values and return values to called function.

TCS1013 INTRODUCTION TO PROGRAMMING

CHAPTER 6

6.1 Function It is also named as program building block A function is a self-contained program segment that carries out specific, well-defined task. 3 types of functions: 1.Main function main() 2.Library Functions clrscr(); strcpy();

3.User-defined functions
void print(); char *process(); int calculate (float a, int b); A function definition has two principle components: Example: main () {..} head head body

- function name and a set of parentheses ( ) . - The parentheses may or may not contain something.

Body

- starts immediately after the closing parentheses of function name must be enclosed by braces { } . - Contains one or more statement.

Every function has a name. Function name are made up and assigned by the user based on similar rule that applies to naming variables. All function names have one set of parentheses immediately following them. This helps you differentiate them from variables. contain something. The parentheses may or may not

Braces must enclose the body of each function, starting immediately after the closing parentheses of the function name. 2

Lecturer: Wannoraini Abdul Latif

TCS1013 INTRODUCTION TO PROGRAMMING

CHAPTER 6

This means that a block containing one or more statements makes up the body of each function.

6.2 Main function


6.3 Library function

Every C++ program consists of one or more functions. One of these functions must be named as main(). The main() function is always the first to execute. Must exist in each program.

Built-in routines that manipulates number, strings, output, etc.

6.4 User-defined functions

User-defined functions are the functions that the user writes. Syntax: ReturnDataType functionName(argumentList) { /*Any C++ statements/ }

Where: FunctionName - it is the functions name - similar rules as naming the variable ReturnDataType argumentList - data type of the item returned by the function. - represents the data type and variable name of the arguments.

1.

There are four types of user-defined functions.

Function without returnDataType and argumentList Syntax:


FunctionName() { /*function body*/ }

Example:

void message () void message () { { cout<<WELCOME; cout<<WELCOME; } }

OR

void message (void) void message (void) { { cout<<WELCOME; cout<<WELCOME; } }

Lecturer: Wannoraini Abdul Latif

TCS1013 INTRODUCTION TO PROGRAMMING

CHAPTER 6

2.

Function with returnDataType and without argumentList. Syntax:


ReturnDataType functionName() { /*function body*/ return value; }

Example :
int count () { int a, b, total; cout<<Enter first number :; cin>>a; cout<<Enter second number :; cin>>b; total = a + b; return total; }

3.

Function without returnDataType and with argumentList Syntax:


FunctionName(argumentList) { /*function body*/ }

Example:
void print (int age) { cout<<My age is <<age years old; }

4.

Function with returnDataType and argumentList Syntax:


ReturnDataType functionName(argumentList) { /*Function body*/ return value; }

Example: Lecturer: Wannoraini Abdul Latif


int count (int a, int b) { int total; total = a + b; return total; }

TCS1013 INTRODUCTION TO PROGRAMMING

CHAPTER 6

6.5

Function Prototype

be used. Syntax:

Sometimes the word prototype is named as a model. Function prototypes are used to define the user-defined functions before it can

ReturnDataType functionName(argumentList);

The simplest way is to rewrite your head of user-defined functions, put them before main() function and ends with semi-colon(;) Example:
/*function prototype*/ #include <iostream.h> void message (); /*function prototype*/ main () { /*main function body*/ } void message () { /*function body*/ }

All functions must match their prototype. Prototype protects you from programming errors. It will be more structured and therefore easier for us to read code. It allows the C++ compiler to check the syntax of function calls. If you break any prototypes rules, the compiler informs you of the problem and you can correct it. Without the prototype, the program would compile but the result would be wrong.

Lecturer: Wannoraini Abdul Latif

TCS1013 INTRODUCTION TO PROGRAMMING

CHAPTER 6

types.

You should prototype every function in your program except main(). It is because to define which function that follows, their return types and their parameter

problems.

By having prototype of these functions, you ensure that bad values cannot be passed to the functions. If someone attempts to pass incorrect value, C++ catches the

6.6 Calling function and called function The name of your user-defines function must be called following an order of the statements inside the body that will be executed.


function. Example:

Generally the primary function that controls functions order calls is named as a calling function The functions controlled by the calling function is named as the called

#include <iostream.h> void message (); main () { message (); } /*function prototype*/ /*calling function*/ /*function called*/

void message () /*called function*/ { cout<<Bye...; }

#include call function from We also can <iostream.h> another function (user-defined function) void call(); void message (); main () { call(); message(); } void call() { message(); } /*function prototype*/ /*calling function*/

Example:

/*calling function*/ /*function call*/

Lecturer: Wannoraini Abdul Latif

void message () /*function called*/ { cout<<Bye...; }

TCS1013 INTRODUCTION TO PROGRAMMING

CHAPTER 6

6.7 Passing Values

function.

Needed when the value of a local variable have to be transferred to another

When a local variable is passed from one function to another, you pass an argument from the first function to the next. The called function receives a parameter from the function that sends it. Function that sends one or more arguments (local variables) to the receiving function Calling function Sender : Argument Function that receives the parameters from the calling function

VALUES: Local variable(s) or constant(s)

Called function Receiving :Argument

6.7.1 Passing by value (by copy) Describe one of the 2 methods by which arguments are passed to receiving function.

When an argument (local variable) is passed by value, the data item is copied to the function.

/*Passing by values*/ Any alteration #include <iostream.h> made to the data item within the function is not carried over into the

calling function. void addition (int, int); parameter Example:

/*function prototype consists by return List*/

main() /*calling function*/ { int A = 3, B = 2; addition (A, B); /*calling function with argument*/ }

Lecturer: Wannoraini Abdul Latif first, int second) void addition (int
{

/*called function with parameter*/

int product; product = first + second; cout<<\n<<first<< + <<second<< = <<product<<endl; }

TCS1013 INTRODUCTION TO PROGRAMMING

CHAPTER 6

In this example value A and B is arguments that return called function to (void addition). Called function (void addition) will return int first and int second as parameter

NOTE The number of actual arguments used to call a function must be similar as the number of formal parameters listed in the function prototype. The order of arguments in the list determines correspondence. The first actual argument corresponds to the first formal parameter, the second actual argument corresponds to the second formal parameter, and so on. Each actual argument must be of data type that can be assigned to the corresponding formal parameter with no unexpected loss of information.

6.7.2 Passing by address (by reference)

When an argument (local variable) is passed by address, the address of a data item is passed to the function.

/*passing by address*/ /*passing by address*/ #include <iostream.h> The contents of that address can be accessed freely #include <iostream.h>

Any change made to the data item will be recognized in both the function and the Must be proceeded calling function. This means user have an ability to change a variable in the called by main () main () asterisk { { function and keep those changes in effect in the calling function.
void change (int *); void change (int *); int local = 3; int local = 3; change (&local); change (&local);

} } Example

void change (int *p) void change (int *p) { { cout<<The value that you send is <<*p); cout<<The value that you send is <<*p); } }

Lecturer: Wannoraini Abdul Latif

TCS1013 INTRODUCTION TO PROGRAMMING

CHAPTER 6

NOTE

& The address of operator * The indirection operator

Example: p = &v v = *p Relationship between p and v (where p = &v and v = *p) p 100 10 (&p) *p 5 The data item represented by v can be accessed by the expression *p. * is indirection operator v 5 100 (&v)

6.8 Return values

Passes back data from a receiving function (called function) to its calling function by putting the return value after the return statement. 9

Lecturer: Wannoraini Abdul Latif

TCS1013 INTRODUCTION TO PROGRAMMING

CHAPTER 6

Syntax: return value;

Calling function Return value

Called function

Example:
#include <iostream.h> float cal (int, int); main() {

Return value is assigned to a

int s, t; float a; cout<<Please Enter Two Number ; cin>> &s >> &t; a = cal(s, t); cout<<Average of two numbers is : <<a; } float cal (int num1, int num2) { float avg; avg = (float)(num1 + num2) / 2; return avg; }

Return data type NOTE

Return a value

#include <iostream.h> char* change (); main () { Do not return global variables because their values are already known char *a; a = change (); Even though a function can receive more than one parameter, it can cout<<The word is : <<a; char* change () char *word = "love";
Return value is Return data type assigned to a

1. through the code. 2. the value by address.

} return one single value to the calling function. If you want to return more than a value, you must pass

Lecturer: Wannoraini Abdul Latif {

10 Output :
Return address word

return (word); }

Das könnte Ihnen auch gefallen