04 0 revisit terms and concepts of functions

23
Terms and Concepts of Functions | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: August 2006 Slide 1 Revisit Terms and Concepts of Functions by Jumail Bin Taliba Faculty of Computer Science & Information System

Upload: madzani-nusa

Post on 10-Nov-2014

1.194 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: 04 0 Revisit Terms And Concepts Of Functions

Terms and Concepts of Functions | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 |Last Updated: August 2006 Slide 1

Revisit Terms andConcepts of Functions

byJumail Bin Taliba

Faculty of Computer Science & Information System

Page 2: 04 0 Revisit Terms And Concepts Of Functions

Terms and Concepts of Functions | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 |Last Updated: August 2006 Slide 2

Page 1

AVRG (average, 10, 5, N)

Start

Stop

ReadN

Printaverage

Page 2

AVRG ( result,n1, n2,n3)

Return

sum = n1+ n2+n3

result = sum/3

How a function works?Example: What is the output of the following flowchart when the input is N = 6

average

10

5

N=6

Sum = 10 + 5 + 6

average = 21/3

Output:Average: 7

This flowchart calculates the average of three numbers

Page 3: 04 0 Revisit Terms And Concepts Of Functions

Terms and Concepts of Functions | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 |Last Updated: August 2006 Slide 3

Thinking in “function way”Example: Create a flowchart to calculate the average of three numbers

Page 1

AVRG (result, n1, n2,n3)

Start

Stop

Readn1, n2 , n3

Printresult

Page 2

AVRG ( result,n1, n2,n3)

Return

sum = n1+ n2+n3

result = sum/3

End terminal must be a “Return”

Start terminal for afunction is different.Do not use “Start”

The detail of how the function should work is put into another flowchart.

This flowchart is known as a function-definition

This part is known as a function-call

At this point, we only focus on whatwe are going to do. But, how it is going to be done, we focus later.

This part is called body of function. It is nothing much differentfrom main flowchart

This flowchart calculates the average of three numbers

Page 4: 04 0 Revisit Terms And Concepts Of Functions

Terms and Concepts of Functions | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 |Last Updated: August 2006 Slide 4

Function Name and Parameters

Page 1

AVRG (result, n1, n2,n3)

Start

Stop

Readn1, n2 , n3

Printresult

Page 2

AVRG ( R, a, b,c)

Return

sum = a+ b+c

R = sum/3

AVRG is the function name

Objects enclosed by ( ) – result, n1, n2, n3 - are called parameters

Parameters used in a function-callare called actual parameters

result, n1, n2, n3 are actual parameters

Parameters used in a function-definitionare called formal parameters

R, a, b, c are formal parameters

Each formal parameter represents an actual parameter according to its order:

R represents result,a represents n1,b represents n2,c represents n3

The name of an actual parameter may bedifferent from its formal parameter

This flowchart calculates the average of three numbers

Page 5: 04 0 Revisit Terms And Concepts Of Functions

Terms and Concepts of Functions | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 |Last Updated: August 2006 Slide 5

Page 1

AVRG (result, n1, n2,n3)

Start

Stop

Readn1, n2 , n3

PrintR

Page 2

AVRG ( R, a, b,c)

Return

sum = n1 + n2 + n3

R = sum/3

In a function-definition, you should only use formal parameters – R, a, b, c

You shouldn’t use actual parameters

This is wrong!n1, n2, n3 are actual parameters.Should use a, b, c instead.

This is wrong!R is an formal parameters.Should use result instead.

Page 6: 04 0 Revisit Terms And Concepts Of Functions

Terms and Concepts of Functions | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 |Last Updated: August 2006 Slide 6

Page 1

AVRG (average1, n1, n2,n3)

Start

Stop

Readn1, n2 , n3

Printresult

Readn4, n5 , n6

AVRG (average2, n4, n5, n6)

result = (average1 +average2) / 2

Page 2

AVRG ( R, a, b,c)

Return

sum = a+ b+c

R = sum/3

A function may be called more than once

At this time:R represents average1,a represents n1,b represents n2,c represents n3

When comes to this:R represents average2,a represents n4,b represents n5,c represents n6

This flowchart calculates the average of three numbers

Page 7: 04 0 Revisit Terms And Concepts Of Functions

Terms and Concepts of Functions | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 |Last Updated: August 2006 Slide 7

Input and Output Parameters

Each function parameter may act as:

• Inputdata to be processed by the function

• Outputthe result produced by the function

• Both

Page 8: 04 0 Revisit Terms And Concepts Of Functions

Terms and Concepts of Functions | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 |Last Updated: August 2006 Slide 8

Page 1

AVRG (result, n1, n2,n3)

Start

Stop

Readn1, n2 , n3

Printresult

Page 2

AVRG ( R, a, b,c)

Return

sum = a+ b+c

R = sum/3

Function call:

result is the output parameter. n1, n2, n3 are the input parameters.

Function definition:

R is the output parametera, b, c are input parameters

This flowchart calculates the average of three numbers

Page 9: 04 0 Revisit Terms And Concepts Of Functions

Terms and Concepts of Functions | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 |Last Updated: August 2006 Slide 9

Page 1 Page 2

EXCHG( x, y)

Return

hold = x

x = y

y = hold

Start

Stop

Printp, q

EXCHG (p, q)

Readp, q

Function call:

p and q act as both input and output parameters.

Function definition:

x and y act as both input and output parameters

This flowchart exchanges or swaps the value of x and y each other

Page 10: 04 0 Revisit Terms And Concepts Of Functions

Terms and Concepts of Functions | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 |Last Updated: August 2006 Slide 10

Page 2

AVRG ( R, a, b,c)

Return

sum = a+ b+c

R = sum/3

If there is only one output parameter, the flowchart may “RETURN” the result

Example: let’s take a look again at the function that calculates the average of three numbers.

Original function flowchart:

Page 2

AVRG ( a, b,c)

Return R

sum = a+ b+c

R = sum/3

Since it has only one output, the output may be “RETURN”

The output parameter (R) is removed from the formal parameter list

and the result is return

Page 11: 04 0 Revisit Terms And Concepts Of Functions

Terms and Concepts of Functions | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 |Last Updated: August 2006 Slide 11

Since the function-definition has been modified, the way of the function to be called will also be changed

Original main flowchart: Modified main flowchart:

Page 1

AVRG (result, n1, n2,n3)

Start

Stop

Readn1, n2 , n3

Printresult

Page 1

result =AVRG (n1, n2, n3)

Start

Stop

Readn1, n2 , n3

Printresult

Now, result is notanymore a parameterof the function-call

Page 12: 04 0 Revisit Terms And Concepts Of Functions

Terms and Concepts of Functions | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 |Last Updated: August 2006 Slide 12

Writing a C Program is a systematic task

Problem

Flowchart

Intermediate code-prepare a rough code-

Complete code- add details -

Converting function flowcharts to C Code

Page 13: 04 0 Revisit Terms And Concepts Of Functions

Terms and Concepts of Functions | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 |Last Updated: August 2006 Slide 13

Converting flowchart to C code is almost straight forward

Example: multiplying two numbers

Start

Stop

Read ARead B

Display theResult C

Calculate ResutC=A*B

void main (void){ scanf(&A);

scanf(&B);

C = A * B;

printf(C);

}

Intermediate C Code

But!, the program still cannot be executed.It is not completed yet. This code is calledan intermediate code.

Page 14: 04 0 Revisit Terms And Concepts Of Functions

Terms and Concepts of Functions | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 |Last Updated: August 2006 Slide 14

You will get some errors

Error 1: Call to undefined function ‘scanf’The compiler doesn’t recognize ‘scanf’

Error 2: Undefined symbol ‘A’The program is trying to use a variable A but has never been registered. Compiler doesn’t recognize the variable

Page 15: 04 0 Revisit Terms And Concepts Of Functions

Terms and Concepts of Functions | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 |Last Updated: August 2006 Slide 15

Fixing the errors and completing the program

This line will help the compiler to recognize words ‘scanf’ and ‘printf’. File stdio.h contains the information of those functions and some others.

This tells to register (declare) variables. Compiler only recognizes registered variables.

You may notice some extra things.These are called prompts. They used to let the user knows what is going on while the program is running

Page 16: 04 0 Revisit Terms And Concepts Of Functions

Terms and Concepts of Functions | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 |Last Updated: August 2006 Slide 16

Converting function flowcharts

Problem: Finding the average of three numbers

Flowcharts:

Start

Stop

Readn1, n2 , n3

Av erage(av rg, n1, n2, n3)

Printav rg

Av erage(result, a, b, c)

Return

sum = a + b + c

result = sum / 3

Example:

Page 17: 04 0 Revisit Terms And Concepts Of Functions

Terms and Concepts of Functions | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 |Last Updated: August 2006 Slide 17

Start

Stop

Readn1, n2 , n3

Av erage(av rg, n1, n2, n3)

Printav rg

void main (){ scanf(&n1,&n2,&n3);

Average(&avrg, n1, n2, n3);

printf(avrg);

}

Intermediate code of the main flowchartPreparing the rough code

The ampersand (&)indicates that avrg is the output or resultof the function-call.

Page 18: 04 0 Revisit Terms And Concepts Of Functions

Terms and Concepts of Functions | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 |Last Updated: August 2006 Slide 18

Av erage(result, a, b, c)

Return

sum = a + b + c

result = sum / 3

void Average(*result, a, b, c){

sum = a + b + c;

*result = sum / 3.0;

return;}

Intermediate code of the function flowchartPreparing the rough code

The asterisk (*)indicates that result is an output parameter.

Page 19: 04 0 Revisit Terms And Concepts Of Functions

Terms and Concepts of Functions | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 |Last Updated: August 2006 Slide 19

#include <stdio.h>

void Average(float *result, float a, float b, float c){ float sum;

sum = a + b + c; *result = sum/3.0; return;}

void main (){ float n1; float n2; float n3; float avrg;

printf ("Enter three numbers: “); scanf(“%f%f%f”, &n1, &n2, &n3);

Average(&avrg,n1,n2,n3);

prinf("The average is %f“, avrg);}

Complete source codeAdding details to the rough code. The details are shown by bold texts

Page 20: 04 0 Revisit Terms And Concepts Of Functions

Terms and Concepts of Functions | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 |Last Updated: August 2006 Slide 20

Example: This example is the same as the previous one but this time the result is “return”

Problem: Finding the average of three numbers

Flowcharts:

START

STOP

READn1, n2 , n3

avrg = Average (n1, n2, n3)

Printavrg

Average (a, b, c)

Return result

sum = a + b + c

result = sum / 3

Page 21: 04 0 Revisit Terms And Concepts Of Functions

Terms and Concepts of Functions | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 |Last Updated: August 2006 Slide 21

START

STOP

READn1, n2 , n3

avrg = Average (n1, n2, n3)

Printavrg

void main (){ scanf(&n1,&n2,&n3);

avrg = Average(n1, n2, n3);

printf(avrg);

}

Intermediate code of the main flowchartPreparing the rough code

Page 22: 04 0 Revisit Terms And Concepts Of Functions

Terms and Concepts of Functions | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 |Last Updated: August 2006 Slide 22

Average (a, b, c)

Return result

sum = a + b + c

result = sum / 3

Average(a, b, c){

sum = a + b + c;

result = sum / 3.0;

return result;}

Intermediate code of the function flowchartPreparing the rough code

Notice that, • result is removed from parameter list• “void” is also removed

Page 23: 04 0 Revisit Terms And Concepts Of Functions

Terms and Concepts of Functions | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 |Last Updated: August 2006 Slide 23

#include <stdio.h>

float Average(float a, float b, float c){ float sum; float result;

sum = a + b + c; result = sum/3.0; return result;}

void main (){ float n1; float n2; float n3; float avrg;

printf ("Enter three numbers: “); scanf(“%f%f%f”, &n1, &n2, &n3);

avrg = Average(n1,n2,n3);

prinf("The average is %f“, avrg);}

Complete source codeAdding details to the rough code. The details are shown by bold texts