function

24
Function ธธธธธธธ ธธธ ธธธธธ

Upload: zena-kemp

Post on 30-Dec-2015

21 views

Category:

Documents


0 download

DESCRIPTION

Function. ธนวัฒน์ แซ่เอียบ. What is a function. ฟังก์ชันในภาษา C เป็นโปรแกรมที่ถูกออกแบบมาเพื่อใช้แก้ปัญหางานใดงานหนึ่งโดยเฉพาะ ฟังก์ชันจะเปลี่ยน input ให้กลายเป็น output โดยที่ผู้ใช้ไม่รู้ว่าภายในฟังก์ ชันเกิดอะไรขึ้นบ้าง ยกเว้นคนที่เป็นเจ้าของฟังก์ชันนั้น - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Function

Function

ธนวั�ฒน� แซ่เอี ยบ

Page 2: Function

What is a function

• ฟั�งก์�ชั�นในภาษา C เป็�นโป็รแก์รมที่ �ถู!ก์อีอีก์แบบมาเพื่#�อีใชั$แก์$ป็�ญหางานใดงานหน(�งโดยเฉพื่าะ

• ฟั�งก์�ชั�นจะเป็ลี่ �ยน input ให$ก์ลี่ายเป็�น output โดยที่ �ผู้!$ใชั$ไมร! $วัาภายในฟั�งก์�ชั�นเก์/ดอีะไรขึ้(1นบ$าง ยก์เวั$นคนที่ �เป็�นเจ$าขึ้อีงฟั�งก์�ชั�นน�1น

• Output ที่ �ได$จาก์ฟั�งก์�ชั�นสามารถูเป็�น action แลี่ะ value– ตั�วัอียางฟั�งก์�ชั�นที่ � output เป็�น action เชัน printf()– ตั�วัอียางฟั�งก์�ชั�นที่ � output เป็�น value เชัน getchar()

Page 3: Function

Defining a function

#include <stdio.h>

void printMessage (void){

printf ("Programming is fun.\n");}

int main (void){

printMessage (); printMessage ();return 0;

}

FunctionDefinition-occurs ONE time for all-outside other functions

Functioncalls (invocations)-occurs ANY (0-N) times-statement inside (other) functions body

Page 4: Function

Transfer of control flow

printMessage (); printMessage ();return 0;

main

printf

printMesage printf

เม#�อีพื่บค5าส��งก์ารเร ยก์ใชั$ฟั�งก์�ชั�น โป็รแก์รมจะเป็ลี่ �ยนลี่5าด�บไป็ที่ �ฟั�งก์�ชั�น แลี่$วัป็ฏิ/บ�ตั/ฟั�งก์�ชั�นน 1จนจบ หลี่�งจาก์น�1นจะก์ลี่�บไป็ที่ �ฟั�งก์�ชั�นผู้!$เร ยก์ แลี่ะป็ฏิ/บ�ตั/งานตัอีจาก์ค5าส��งก์ารเร ยก์ใชั$ฟั�งก์�ชั�น

Page 5: Function

Function definitions

return-type function-name(argument declarations){ declarations and statements}

General form of function definition:

void printMessage ( void ){

printf ("Programming is fun.\n");}

void printMessage (void){

printf ("Programming is fun.\n");return;

}

return-type arguments

Page 6: Function

Function prototype

• น5าบรรที่�ดแรก์ขึ้อีงก์ารน/ยามฟั�งก์�ชั�น มาเขึ้ ยนเป็�นตั$นแบบขึ้อีงฟั�งก์�ชั�น (ส�งเก์ตัวัาจะม ขึ้$อีม!ลี่ที่ �จ5าเป็�นตั$อีงร! $เก์ �ยวัก์�บฟั�งก์�ชั�นไวั$ที่�1งหมด)

void calculateTriangularNumber (int n)• Input ขึ้อีงฟั�งก์�ชั�นป็ระก์อีบด$วัย พื่าราม เตัอีร�หร#อีอีาร�ก์ เมนตั�

– ในก์ารน/ยามฟั�งก์�ชั�น เร ยก์วัา formal parameters เป็�นชั#�อีส5าหร�บใชั$ภายในฟั�งก์�ชั�น

– ในค5าส��งเร ยก์ฟั�งก์�ชั�น เร ยก์วัา actual parameters •actual parameters สามารถูเป็�นคาคงที่ � ตั�วัแป็ร

หร#อี expression•actual parameters จะถู!ก์หาคา แลี่ะน5าคาที่ �ได$สงตัอี

ให$ก์�บ formal parameter ขึ้อีงฟั�งก์�ชั�นที่ �ส�มพื่�นธ�ก์�น

Page 7: Function

Example: function declaration

#include <stdio.h>

void printMessage (void) ;

int main (void){

printMessage (); printMessage ();return 0;

}

void printMessage (void){

printf ("Programming is fun.\n");}

Function declaration (prototype)(has to be before the calling function)

Function calls

Function definition(can be after the calling function)

Page 8: Function

Examples: function declarations

• ก์ารป็ระก์าศฟั�งก์�ชั�นสามารถูระบ8เพื่ ยงแค type ไมจ5าเป็�นตั$อีงระบ8ชั#�อีก์9ได$

int gcd (int u, int v);Orint gcd (int, int);

void calculateTriangularNumber (int n);Orvoid calculateTriangularNumber (int);

Page 9: Function

Example: arguments

// Function to calculate the nth triangular number#include <stdio.h>void calculateTriangularNumber ( int n ){

int i, triangularNumber = 0;for ( i = 1; i <= n; ++i )

triangularNumber += i;printf ("Triangular number %i is %i\n", n, triangularNumber);

}int main (void){

calculateTriangularNumber (10);calculateTriangularNumber (20);calculateTriangularNumber (50);return 0;

}

Page 10: Function

Arguments and local variables

• ตั�วัแป็รที่ �น/ยามภายในฟั�งก์�ชั�นเร ยก์วัา local variables– ตั�วัแป็รแบบน 1สามารถูเขึ้$าถู(งได$เฉพื่าะฟั�งก์�ชั�นน 1เที่าน�1น

ฟั�งก์�ชั�นอี#�นไมสามารถูเขึ้$ามาใชั$ได$ – ถู$าม ก์ารก์5าหนดคาเร/�มตั$นให$ก์�บตั�วัแป็ร แตัลี่ะคร�1งที่ �ม ก์าร

เร ยก์ฟั�งก์�ชั�นจะก์5าหนดคาเร/�มตั$นน 1ให$ก์�บตั�วัแป็รที่8ก์คร�1ง• Formal parameters เหม#อีนก์�บ local variables• Scope ค#อีพื่#1นที่ �ที่ �สามารถูเขึ้$าถู(งตั�วัแป็รได$

– Scope ขึ้อีง local variables and formal parameters อีย!ภายในฟั�งก์�ชั�นเที่าน�1น

Page 11: Function

exercise

• จาก์ตั�วัอียางก์อีนหน$าน 1 ภายในฟั�งก์�ชั�น calculateTriangularNumber (10); บรรที่�ดส8ดที่$าย คาขึ้อีง n, I, triangularNumber ม คาเที่าก์�บเที่าไร

• จาก์ตั�วัอียางก์อีนหน$าน 1 ภายในฟั�งก์�ชั�น calculateTriangularNumber (20); บรรที่�ดส8ดที่$าย คาขึ้อีง n, I, triangularNumber ม คาเที่าก์�บเที่าไร

• จาก์ตั�วัอียางก์อีนหน$าน 1 ภายในฟั�งก์�ชั�น calculateTriangularNumber (50); บรรที่�ดส8ดที่$าย คาขึ้อีง n, I, triangularNumber ม คาเที่าก์�บเที่าไร

Page 12: Function

Example: arguments

#include <stdio.h>void gcd (int u, int v){

int temp;printf ("The gcd of %i and %i is ", u, v);while ( v != 0 ) {

temp = u % v;u = v;v = temp;}

printf ("%i\n", u);}int main (void){

gcd (150, 35);gcd (1026, 405);gcd (83, 240);return 0;

}

ถู$าตั$อีงม parameter มาก์ก์วัา 1 ตั�วัตั$อีงป็ระก์าศ type แตัลี่ะตั�วัให$ชั�ดเจน

Page 13: Function

Example: arguments are passed by copying values !

#include <stdio.h>void gcd (int u, int v){

int temp;printf ("The gcd of %i and %i is ", u, v);while ( v != 0 ) {

temp = u % v;u = v;v = temp;}

printf ("%i\n", u);}int main (void){

int x=10,y=15;gcd (x, y);printf(“x=%i y=%i \n”,x,y);return 0;

}

x แลี่ะ y คาคงเด/ม ไมม ก์ารเป็ลี่ �ยนแป็ลี่ง

u แลี่ะ v สามารถูถู!ก์ก์5าหนดคาใหม

ในฟั�งก์�ชั�นได$

Page 14: Function

Example: arguments are passed by copying values !

Ex จงแสดงส/�งที่ �พื่/มพื่�อีอีก์ที่างจอีภาพื่

#include <stdio.h>void multiplyBy2 (float x){

printf(“parameter at start: %.2f, at %p \n”,x, &x);

x*=2;printf(“parameter at end: %.2f, at %p \n”,x, &x);

}int main (void){ float y = 7; printf (“y before call: %.2f, at %p \n", y, &y); multiplyBy2 (y);

printf (“y after call: %.2f, at %p \n", y, &y);return 0;

}

Page 15: Function

Example: scope of local variables

Ex จงแสดงส/�งที่ �พื่/มพื่�อีอีก์ที่างจอีภาพื่

#include <stdio.h>void f1 (float x) {

int n=6;printf(“%f \n”, x+n);

}int f2(void) {

float n=10;printf(“%f \n”,n);

}int main (void){ int n=5; f1(3); f2(); return 0;}

Page 16: Function

Returning function results

• ฟั�งก์�ชั�นภาษา C สามารถูสงคาก์ลี่�บได$ เชันreturn expression

• คาที่ �ได$จาก์ expression จะสงก์ลี่�บไป็ให$ฟั�งก์�ชั�น ถู$า type ขึ้อีง expression ไมตัรงก์�บ return type ตัามก์ารป็ระก์าศฟั�งก์�ชั�น ภาษา C จะเป็ลี่ �ยน type ตัามที่ �ได$ป็ระก์าศไวั$ให$

• ร!ป็แบบงายๆขึ้อีง return ค#อี return;

• ก์ารป็ฏิ/บ�ตั/ค5าส��ง return statement จะที่5าให$โป็รแก์รมก์ลี่�บไป็ฟั�งก์�ชั�นผู้!$เร ยก์ที่�นที่

Page 17: Function

Example: function result

จงเขึ้ ยนฟั�งก์�ชั�นหาคาส!งที่ �ส8ดขึ้อีงพื่าราม/เตัอีร�#include <stdio.h>int max (int u, int v){

…}int main (void){

int result;result = max (150, 35);printf ("%i\n", result);result = max (1026, 405);printf ("%i\n", result);printf ("%i\n", gcd (83, 240));return 0;

}

Page 18: Function

Parameter are passed by reference

• ถู$าภายในฟั�งก์�ชั�นเป็ลี่ �ยนแป็ลี่งคาขึ้อีงพื่าราม/เตัอีร�แลี่$วั จะสงผู้ลี่ให$พื่าราม/เตัอีร�ที่ �สงเขึ้$ามาถู!ก์เป็ลี่ �ยนแป็ลี่งคาไป็ด$วัย

• ใชั$ type แบบ pointervoid increment(int *n){

*n=*n+1;}void main(){

int a=2;increment(…);

}

Page 19: Function

Passing arrays as parameters

• A whole array can be one parameter in a function• In the function declaration, you can then omit the specification of

the number of elements contained in the formal parameter array.– The C compiler actually ignores this part of the declaration anyway; all the

compiler is concerned with is the fact that an array is expected as an argument to the function and not how many elements are in it.

• Example: a function that returns the minimum value from an array given as parameter – int minimum (int values[10]);

• We must modify the function definition if a different array size is needed !

– int minimum (int values[]); • Syntactically OK, but how will the function know the actual size of the array ?!

– int minimum (int values[], int numberOfElements);

Page 20: Function

Example: Passing arrays as parameters

// Function to find the minimum value in an array#include <stdio.h>int minimum (int values[10]) {

int minValue, i;minValue = values[0];for ( i = 1; i < 10; ++i )

if ( values[i] < minValue )minValue = values[i];

return minValue;}int main (void) {

int scores[10], i, minScore;printf ("Enter 10 scores\n");for ( i = 0; i < 10; ++i )

scanf ("%i", &scores[i]);minScore = minimum (scores);printf ("\nMinimum score is %i\n", minScore);return 0;

}

Page 21: Function

Example: No size specified for formal parameter array

// Function to find the minimum value in an array#include <stdio.h>int minimum (int values[], int numberOfElements){

int minValue, i;minValue = values[0];for ( i = 1; i < numberOfElements; ++i )

if ( values[i] < minValue )minValue = values[i];

return minValue;}int main (void){

int array1[5] = { 157, -28, -37, 26, 10 };int array2[7] = { 12, 45, 1, 10, 5, 3, 22 };printf ("array1 minimum: %i\n", minimum (array1, 5));printf ("array2 minimum: %i\n", minimum (array2, 7));return 0;

}

Page 22: Function

Array parameters are passed by reference !

• If a function changes the value of an array element, that change is made to the original array that was passed to the function. This change remains in effect even after the function has completed execution and has returned to the calling routine.

• Parameters of non-array type: passed by copying values• Parameters of array type: passed by reference

– the entire contents of the array is not copied into the formal parameter array.

– the function gets passed information describing where in the computer’s memory the original array is located.

– Any changes made to the formal parameter array by the function are actually made to the original array passed to the function, and not to a copy of the array.

Page 23: Function

Example: Array parameters are passed by reference !

#include <stdio.h>void multiplyBy2 (float array[], int n){

int i;for ( i = 0; i < n; ++i )

array[i] *= 2;}int main (void){float floatVals[4] = { 1.2f, -3.7f, 6.2f, 8.55f };

int i;multiplyBy2 (floatVals, 4);for ( i = 0; i < 4; ++i )

printf ("%.2f ", floatVals[i]);printf ("\n");return 0;

}

Page 24: Function

ที่ �มา• conf.dr.ing. Ioana Sora : University of Timisoara, Romania