lecture 4: functions

23
Functions

Upload: vivek-bhargav

Post on 21-Jul-2015

25 views

Category:

Engineering


2 download

TRANSCRIPT

Functions

Advantages

• Modularizes the Code -> Code is more readable

• Change in Internal Implementation of Functions does not propagate further changes in code

• Increases re-usability of code.

Writing Functions

• Return_Type Function_Name ( type_1 variable_1 , type_2 variable_2 , .. )

• In Functions, you can either pass parameters by value or by reference

• Also, you have to declare a function prototype if you are about to make a call to a function which is written below the caller function.

Declaring Functions

Demo #1

• Write a function which takes two integers as arguments and returns the value of the maximum.

Demo #2

• Write a function which squares an integer.

Approach

• Break the problem into Arguments of Function and the Return value of the function.

Here the Argument to the function is a single Integer and the Return value is an Integer as well.

Call By Value

• You CALL your friend to get your mobile number recharged. You give him a slip which has your number written over it. He TAKES ANOTHER (FRESH) SLIP AND COPIES THE NUMBER ON THIS (FRESH) SLIP, goes to the shopkeeper and gets the work done. But, while returning, he met another friend who also asked him for mobile recharge, so he COPIES THE NEW NUMBER ON THE (his own) SLIP NOW. Whose number is written on your slip now ?

• Same is the case in call by value where if you call a function, passing a particular argument by value, then the function TAKES ANOTHER (identical) VARIABLE AND COPIES THE VALUE OF THE ARGUMENT ON THIS VARIABLE.

Value1 Value2 Argument1 Argument2

In Memory

Any changes made to argument1/2 will not change value1/2

So, Even if you do:

This will change Memory contents for “val” not variable

Call By Reference

• You CALL your friend to get your mobile number recharged. You give him a slip which has your number written over it. He TAKES THIS SLIP and goes to the shopkeeper and gets the work done. But, while returning, he met another friend who also asked him for mobile recharge, so he COPIES THE NEW NUMBER ON THE (Your) SLIP NOW. Whose number is written on your slip now ?

• Same is the case with functions. If you pass an argument by Reference then any changes (New Number copied) made to the argument will be reflected.

Call By Reference

• If you want the function to manipulate the values of the arguments.

• For ex: Sorting an array , Reversing a string etc.

• Just use the Reference “&” operator.

So, If you do:

Demo #3

• Write a Function which takes two characters and prints them in Upper Case but the first character after the function call must be in Upper Case while the second character must not be changed.

Approach

• Break the Problem into Arguments of the function and the return value of the function

• Think about the “call type” for each argument as in Call by Value or Call by Reference.

Here the Function should have two arguments both of type “char”. Pass the first argument as reference (i.e. using the & operator) and the second one by value/reference. What about return type ? “void”.Use “return;” in such cases if you want to force the program to return immediately.

Call By Reference

• You can also pass a pointer to the argument if you want the change to be reflected.

Note: What about call type of pvalue here

Call by Value

Arrays are always passed as Reference. ?

• General:

• If function a is calling function b in its’ body then function b should be written above function a. What if both functions are calling each other. Define Function Prototypes at the very beginning of the File. What is a Function Prototype ?

• Return_type function_name (type_1 arg1 , .. );

• Note the semicolon at the end.

Lets’ Tackle Some Real Problem

• Given a string S check if any rotation of S is palindrome or not.

• Rotation of S is defined as follows:

IF S = c1 c2 c3 c4…. ck ck+1 ck+2 …. cn

then ck+1 ck+2 … cn c1 c2 c3 .. ck

is a rotation of S for all 1<=k<n

Approach

• First break the problem into sub-problems:

• What all independent sub-problems need to be solved?->Palindrome Checking

-> String Reversal

->Substring Finding

What about Parameters and Return Types and “call by” of the arguments.

Lets’ see a naïve algorithm for the problem.

How to pass struct objects to functions

• Pass Arguments just like any other data type.

Demo #4

• Write getter and a setter functions for all the members of a struct. Assume the struct has only two attributes string (say name of student), int (student id)

Some Thoughts

• What if we want to return more than one (or more) values (say minimum and maximum both simultaneously in an array)?– Hint: Can we use pointers/array ?

• What if the data types of the values to be returned are different ?– Hint: Can we use structs?

• What if our function has no arguments ?– Hint: Main itself is a function.

– Have you seen it taking arguments ?