Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.

Similar presentations


Presentation on theme: "Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and."— Presentation transcript:

1 Functions Structured Programming

2 Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and formal parameters Global Variables 2

3 Introduction Functions break large computing tasks into smaller ones. You have used functions in every program that you have encountered so far. ◦ The getch( ) and cout routines are examples of functions. Also you wrote a special function in each and every program ◦ main function 3

4 Introduction contd... Functions performs a specific task and is relatively independent of the remaining code ◦ Example :  cout – Write output to the console  cin– Read an user input 4

5 Defining a Function When defining a function we have to mention : ◦ Function name ◦ The type of value it returns ◦ The arguments it takes void main( ) { } 5 Return type Name Argument List

6 Function Components ComponentPurposeExample Declaration (prototype) Specifies function name,argument types and return value. Alert compiler that function is coming up later Void func( ); callCauses the function to be executed Func( ); DefinitionThe function itself. Contains the lines of code that constitute the function. Void func( ) { ……. }

7 Defining a Function – Function Name Every function has a name, in this example it is main ◦ Function names are very similar to variable names. ◦ You have to follow the same rules  Can have alphabetical characters, digits or _  The name must start with a alphabetical character or _  Name has to be short but meaningful etc,. ◦ Ex.  int Sum()  float Average() 7

8 Defining a Function - Return Type Return type is the data type of the result that the function will send to its caller. If there is no return type we use the keyword void. ◦ Ex.  int main()  void main() 8

9 Defining a Function - Return Type Contd,. ◦ When we complete the return value; statement we pass the control back to the main method and resume the main method from where we stopped. ◦ When calling a function, you must get the return type from the function to a variable of correct type. ◦ Examples: 1.numbers[i] = readValue(i); 2.float avg; avg = calcAvg(numbers); 9

10 Defining a Function – Argument List Argument list are the values you pass into the function. Each item in the argument list is called a formal parameter. ◦ Each item in the argument list have a name and a data type. Function will perform some task with these values such as: ◦ Displaying the value ◦ Calculating a value etc,.  Ex.  int Sum(int Num1, int Num2)  float Average(int Num1, int Num2) 10

11 Function Calling and Execution When calling a function, the values we pass are copied into the formal parameter. Then during the execution time, the function will work with its formal parameter as shown bellow; Calling the Function : int Sum = sum(4,5); Function : int sum(int Num1, Num2) { return (Num1 + Num2); } 11

12 Function : Example 1 #include void printMessage () { cout<<“I Love C Programming !!\n”; } void main ( ) { printMessage (); } 12 I Love C Programming !! _

13 Arguments Arguments are usually inputs to a function. When you are calling a function with an argument list you must prepare the set of arguments before you call the function. Example: int numbers[5]; //Read the values using for loop for ( i = 0; i < 5; ++i ) { numbers[i] = readValue(i); } min = calcMin(numbers); 13 Preparing the arguments

14 Local Variables Look at the following function. 14 int calcMin(int numbers[]) { int i; int min = numbers[0]; for ( i = 1 ; i < 5; ++i ) { if (min > numbers[i]) min = numbers[i]; } return min; }

15 Local Variables contd,. Inside the function we have declared variables such as int i, int min These are called local variables of a function. They are also called as automatic local variables because they are automatically “created” each time the function is called. Their values are local to the function. 15

16 Local Variables contd,. The value of a local variable can only be accessed by the function in which the variable is defined. Its value cannot be accessed by any other function. If an initial value is given to a variable inside a function, that initial value is assigned to the variable each time the function is called. 16

17 Functions - Exercise 1 Write down a function called calcSum which will calculate the Sum of two user given values. 17

18 Function : Example 2 In a program we need to read 5 integers from the user. The integers must be greater than or equal zero and less than or equal to100. If the inputs are valid we must store them to an array and display the minimum, maximum and the average values. 18

19 Execution of Example1 Always execution starts with the main method As a result first part of the program will declare variables Next is the for loop. In every iteration we call int readValue(int index) function. ◦ When readValue is called we stop execution of the main function and control is sent to the readValue. ◦ As a result its statements will get executed 19

20 Formal Parameters in the Argument List When you pass values in an argument list, they are copied into the formal parameters of the function. The function will work with the formal parameters. 20

21 Exercise 2 Implement a program that will read 5 numbers from the user and store the absolute value of each number into an array. 21


Download ppt "Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and."

Similar presentations


Ads by Google