function

26
functi on

description

function. เนื้อหาที่สอนในวันนี้. ฟังก์ชันคืออะไร ? ส่วนประกอบของฟังก์ชัน โครงสร้างของการเขียนฟังก์ชัน Parameters (ตัวแปรที่รับมา) Return Value (ค่าที่ต้องส่งกลับ) function prototyping local variable & call by value. ฟังก์ชันคืออะไร ?. - PowerPoint PPT Presentation

Transcript of function

function

#07 function 2

เนื้��อหาที่สอนื้ในื้วั�นื้นื้� ฟั�งก์�ชั�นื้คื�ออะไร ? ส�วันื้ประก์อบของฟั�งก์�ชั�นื้ โคืรงสร�างของก์ารเขยนื้ฟั�งก์�ชั�นื้ Parameters (ตั�วัแปรที่ร�บมา) Return Value (คื�าที่ตั�องส�งก์ลั�บ) function prototyping local variable & call by

value

#07 function 3

ฟั�งก์�ชั�นื้คื�ออะไร ?ก์ลั%�มของคื&าส�งที่ที่&างานื้เป'นื้อ(สระจาก์คื&า

ส�งก์ลั%�มอ�นื้ๆ ตั�วัฟั�งก์�ชั�นื้เขยนื้อย+�นื้อก์บลั,อก์ใดๆ มตั�วัแปรของตั�วัเอง (local variable)ร+�จ�ก์เฉพาะในื้ฟั�งก์�ชั�นื้นื้��นื้

ตั(ดตั�อก์�บ main program , ฟั�งก์�ชั�นื้อ�นื้ผ่�านื้ parameter

ที่&าไมตั�องเขยนื้เป'นื้ฟั�งก์�ชั�นื้โปรแก์รมขนื้าดใหญ่�เก์(ดข�อผ่(ดพลัาดได�

ง�ายง�ายในื้ก์ารเขยนื้, ก์ารตัรวัจสอบแก์�ไข (debug)

นื้&าก์ลั�บมาใชั�ใหม�ได� (reusable)

#07 function 4

#include stdio.hvoid main(){ … (บล็�อกของ main)}

โปรแก์รมปก์ตั(#include stdio.hvoid fn1 ( int a ){ … (บล็�อกของ fn1)}int fn2 ( void ){ … (บล็�อกของ fn2)}void main(){ … (บล็�อกของ main)}

เขยนื้เป'นื้ฟั�งก์�ชั�นื้

#07 function 5

ข��นื้ตัอนื้ก์ารเรยก์ใชั�ฟั�งก์�ชั�นื้เรยก์ใชั�งานื้ฟั�งก์�ชั�นื้ พร�อมที่��งส�งคื�าให�

ฟั�งก์�ชั�นื้ย�ายไปที่&างานื้ฟั�งก์�ชั�นื้ที่ถู+ก์เรยก์ใชั�จนื้

จบฟั�งก์�ชั�นื้คื�นื้คื�าผ่ลัลั�พธ์�ให�ผ่+�ที่เรยก์ใชั�

Boss asks worker to complete task

Worker gets information, does task, returns result

Information hiding: boss does not know details

เชั�นื้ ฟั�งก์�ชั�นื้ fabs() ในื้ math.h ซึ่5งเป'นื้ฟั�งก์�ชั�นื้ในื้ก์ารหาคื�าส�มบ+รณ์�ของตั�วัเลัขจ&านื้วันื้จร(ง

#07 function 6

ตั�วัอย�าง ฟั�งก์�ชั�นื้ fabs() ในื้ math.h ซึ่5งเป'นื้ฟั�งก์�ชั�นื้ในื้ก์ารหาคื�าส�มบ+รณ์�ของตั�วัเลัขจ&านื้วันื้จร(ง

เม�อตั�องก์ารหาคื�าส�มบ+รณ์� : เรยก์ใชั�งานื้ฟั�งก์�ชั�นื้ fabs() โดยส�งตั�วัเลัขที่ตั�องก์ารหาคื�าส�มบ+รณ์�ให�ฟั�งก์�ชั�นื้ฟั�งก์�ชั�นื้ fabs() ร�บตั�วัเลัข , คื&านื้วัณ์

คื�าส�มบ+รณ์� , ส�งผ่ลัลั�พธ์�ก์ลั�บได�ผ่ลัลั�พธ์�จาก์ฟั�งก์�ชั�นื้เป'นื้คื�าส�มบ+รณ์�

ของตั�วัเลัขที่ส�งไปตั�องก์ารหาคื�าส�มบ+รณ์�อก์คืร��ง : เรยก์ใชั�

ฟั�งก์�ชั�นื้ fabs()โดยส�งคื�าใหม�ให�แก์�ฟั�งก์�ชั�นื้ ผ่ลัลั�พธ์�ที่ได�จะเปลัยนื้แปลังตัามคื�าที่ส�งไป

#07 function 7

ตั�วัอย�าง function

ตั�วัอย�าง function

function fabs()

main

r1 = fabs(4.5)

start

stop

start

stop

คำ�นวณห�คำ��สั�มบ�รณ�

……

r2 = fabs(-3.1)

4.5 4.5-3.1 3.1

#07 function 8

สั�วนประกอบของฟั�งก�ชั�นfunction name : ชั�อของฟั�งก์�ชั�นื้ที่

ส�วันื้อ�นื้ๆ จะใชั�ในื้ก์ารเรยก์ใชั�งานื้function body : ก์ลั%�มคื&าส�งที่เขยนื้

ข5�นื้เพ�อให�ฟั�งก์�ชั�นื้ที่&างานื้ตัามที่ตั�องก์ารparameters : คื�าที่ส�งให�ก์�บฟั�งก์�ชั�นื้

อาจไม�มก์,ได� เชั�นื้ ฟั�งก์�ชั�นื้ clrscr()return value : คื�าที่ฟั�งก์�ชั�นื้ส�งก์ลั�บ

ให�ก์�บส�วันื้ที่เรยก์ใชั�อาจไม�มก์,ได� เชั�นื้ ฟั�งก์�ชั�นื้ clrscr()

#07 function 9

ตั�วัอย�างprintf ( “You are %d

years old” , year ) ;function name : printffunction body : ก์ลั%�มคื&าส�งในื้ก์าร

พ(มพ�ตั�วัอ�ก์ษรออก์ที่างหนื้�าจอ (อย+�ในื้ไฟัลั� stdio.h)

parameters : format ในื้ก์ารพ(มพ�ข�อคืวัาม (ข�อคืวัามภายใตั�เคืร�องหมายคื&าพ+ด) + ผ่ลัลั�พธ์�ของ expression ที่ตั�องก์ารพ(มพ�ออก์มา parameters ที่ส�งให�ฟั�งก์�ชั�นื้จะอย+�

ภายใตั�วังเลั,บreturn value : ไม�ม (คืวัามจร(งม แตั�

ไม�จ&าเป'นื้ตั�องใชั�)

#07 function 10

โคืรงสร�างของก์ารเขยนื้ฟั�งก์�ชั�นื้return-value-type function-name ( parameter-list ){

function-body ;return (expression); //

คื�าที่ตั�องก์ารส�งก์ลั�บ}

return-value-type function-name ( parameter-list ){

function-body ;return (expression); //

คื�าที่ตั�องก์ารส�งก์ลั�บ} int user_abs

( int n ){if ( n < 0){

n = -1 * n ;}return ( n ) ;

}

ตั�วัอย�าง

#07 function 11

Parameters (คื�าที่ได�ร�บมา)ส�วันื้ที่ก์&าหนื้ดตั�วัแปรเพ�อร�บคื�าที่ส�งให�ก์�บ

ฟั�งก์�ชั�นื้ โดยจะก์&าหนื้ดในื้วังเลั,บที่อย+�หลั�งจาก์ชั�อของฟั�งก์�ชั�นื้

ชันื้(ดของข�อม+ลัที่ผ่+�เรยก์ใชั�จะส�งมาเม�อเรยก์ใชั�ฟั�งก์�ชั�นื้นื้� ตั�องเป'นื้ชันื้(ดเดยวัก์�นื้ ก์�บที่ก์&าหนื้ดไวั�ในื้ส�วันื้ parameters

ฟั�งก์�ชั�นื้ร�บคื�าเพยงคื�าเดยวั หร�อหลัายคื�าก์,ได�

ถู�าไม�ตั�องก์ารร�บคื�า : ใชั�คื&าส�ง void int single_para

( int n )int multi_para

( float x , int a )int no_para ( void )

#07 function 12

Return Value (คื�าที่ส�งก์ลั�บ)ส�วันื้ที่ก์&าหนื้ดคื�าที่ฟั�งก์�ชั�นื้จะส�งก์ลั�บให�

ส�วันื้ที่เรยก์ใชั� ตั�องก์&าหนื้ด 2 ตั&าแหนื้�งที่ ส�มพ�นื้ธ์� ก์�นื้ คื�อส�วันื้ที่อย+�หนื้�าชั�อฟั�งก์�ชั�นื้ (return-value-type) ชันื้(ดข�อม+ลัของคื�าที่จะส�งก์ลั�บ เชั�นื้ int, float, long เป'นื้ตั�นื้

ส�วันื้ที่อย+�หลั�งคื&าส�ง return (ในื้ body of function) ตั�วัแปรหร�อ expression ที่ตั�องก์ารส�งก์ลั�บตั�องมผ่ลัลั�พธ์�เป'นื้ข�อม+ลัชันื้(ดเดยวัก์�นื้ก์�บชันื้(ดของ return-value-type

ส�งคื�าก์ลั�บจาก์ฟั�งก์�ชั�นื้ได� เพยง 1 คื�า เที่�านื้��นื้

#07 function 13

ถู�าไม�ตั�องก์ารส�งคื�าก์ลั�บ ในื้ส�วันื้ return-value-type ใชั�คื&า

ส�ง void ในื้ function body ไม�ตั�องมคื&าส�ง return

int return1 ( int n ){ … … return ( n ) ;}

void no_return (int n ){

float xxx; …}

float return2 ( int n ){

float xxx; … return ( xxx ) ;}

double return3 ( int n ){ double yyy; … return ( yyy * 3.0 ) ;}

#07 function 14

#include <stdio.h>double fac(double x){ int i; double r=1; for (i=1;i<=x;i++) r=r*i; return r;}void main( ){ double x , r ; printf(“input number : ”) ; scanf(“ %f ”,&x) ; r = fac(x) ; printf(“ %.f! = %.f ”, x, r);}

#include <stdio.h>double fac(double x){ int i; double r=1; for (i=1;i<=x;i++) r=r*i; return r;}void main( ){ double x , r ; printf(“input number : ”) ; scanf(“ %f ”,&x) ; r = fac(x) ; printf(“ %.f! = %.f ”, x, r);}

ฟั�งก�ชั�น fac ใชั�ในก�ร คำ�นวณห� factorial

ของคำ��ที่� ร �บม� โดยร�บคำ��เป%นตั�วแปรชัน(ด double แล็ะเม) อคำ�นวณเสัร�จจ+งสั�งผล็ล็�พธ์�กล็�บให�สั�วนที่� เร�ยกใชั� โดยสั�งคำ��กล็�บเป%นตั�วแปรชัน(ด double เชั�นก�น

ฟั�งก�ชั�น fac ใชั�ในก�ร คำ�นวณห� factorial

ของคำ��ที่� ร �บม� โดยร�บคำ��เป%นตั�วแปรชัน(ด double แล็ะเม) อคำ�นวณเสัร�จจ+งสั�งผล็ล็�พธ์�กล็�บให�สั�วนที่� เร�ยกใชั� โดยสั�งคำ��กล็�บเป%นตั�วแปรชัน(ด double เชั�นก�น เร�ยกใชั�ง�นฟั�งก�ชั�น fac โดยสั�งคำ��เป%นตั�วแปรชัน(ด double แล็ะคำ��ที่� ร �บกล็�บจ�กฟั�งก�ชั�นเป%นตั�วแปรชัน(ด double

เร�ยกใชั�ง�นฟั�งก�ชั�น fac โดยสั�งคำ��เป%นตั�วแปรชัน(ด double แล็ะคำ��ที่� ร �บกล็�บจ�กฟั�งก�ชั�นเป%นตั�วแปรชัน(ด double

ตั�วัอย�าง #1

ตั�วัอย�าง #1

#07 function 15

#include <stdio.h>#include <conio.h>sum (int a , int b){ return a + b ;}void main( ){ int x; clrscr( ); x = sum(5,sum(4,5)); printf(“x = %d“ , x);}

#include <stdio.h>#include <conio.h>sum (int a , int b){ return a + b ;}void main( ){ int x; clrscr( ); x = sum(5,sum(4,5)); printf(“x = %d“ , x);}

ก�รเร�ยกใชั�ฟั�งก�ชั�นสั�ม�รถสั�งคำ��arguments เป%นคำ��คำงที่� , expression หร)อตั�วแปรก�ได�แตั�คำ��ที่� สั�งไปตั�องเป%นข�อม�ล็ชัน(ดเด�ยวก�บที่� เร�ก�หนดไว�ในฟั�งก�ชั�น

ก�รเร�ยกใชั�ฟั�งก�ชั�นสั�ม�รถสั�งคำ��arguments เป%นคำ��คำงที่� , expression หร)อตั�วแปรก�ได�แตั�คำ��ที่� สั�งไปตั�องเป%นข�อม�ล็ชัน(ดเด�ยวก�บที่� เร�ก�หนดไว�ในฟั�งก�ชั�น

ถ��ไม�ม� return_type จะถ)อว�� เป%น int โดยปร(ย�ย

ถ��ไม�ม� return_type จะถ)อว�� เป%น int โดยปร(ย�ย

คำ��ที่� สั�งกล็�บโดยใชั�คำ�สั� ง return จะเป%นตั�วแปรหร)อ expression

ก�ได� แตั�จะตั�องเป%นข�อม�ล็ชัน(ด เด�ยวก�บที่� ตั�วแปรใน main คำอย

ร�บอย�� (x)

คำ��ที่� สั�งกล็�บโดยใชั�คำ�สั� ง return จะเป%นตั�วแปรหร)อ expression

ก�ได� แตั�จะตั�องเป%นข�อม�ล็ชัน(ด เด�ยวก�บที่� ตั�วแปรใน main คำอย

ร�บอย�� (x)

ชั) อตั�วแปรที่� ร �บไม�จ�เป%นตั�องเป%นชั) อเด�ยวก�บตั�วแปรที่� สั�ง แตั�ตั�องเป%นข�อม�ล็ชัน(ดเด�ยวก�น

ชั) อตั�วแปรที่� ร �บไม�จ�เป%นตั�องเป%นชั) อเด�ยวก�บตั�วแปรที่� สั�ง แตั�ตั�องเป%นข�อม�ล็ชัน(ดเด�ยวก�น

ตั�วัอย�าง #2

ตั�วัอย�าง #2

#07 function 16

#include <stdio.h>#include <conio.h>void printMsg(void) {

printf(“inside function \n”);}void main() { clrscr();

printf(“inside main \n”);printMsg();printf(“inside main \n”);

}

#include <stdio.h>#include <conio.h>void printMsg(void) {

printf(“inside function \n”);}void main() { clrscr();

printf(“inside main \n”);printMsg();printf(“inside main \n”);

}

ตั�วัอย�าง #3

ตั�วัอย�าง #3

บ�งฟั�งก�ชั�นไม�จ�เป%นตั�องม�ก�รร�บคำ��หร)อสั�งคำ��กล็�บ ข+1นอย��ก�บจ2ดประสังคำ�ของแตั�ล็ะฟั�งก�ชั�น

บ�งฟั�งก�ชั�นไม�จ�เป%นตั�องม�ก�รร�บคำ��หร)อสั�งคำ��กล็�บ ข+1นอย��ก�บจ2ดประสังคำ�ของแตั�ล็ะฟั�งก�ชั�น

เม) อไม�ม�ก�รร�บหร)อสั�งคำ��กล็�บ เวล็�เร�ยกใชั�ง�นฟั�งก�ชั�นก�ไม�จ�เป%นตั�องม�สั�วนที่� สั�งคำ��หร)อตั�วแปรเพ) อม�ร�บคำ��กล็�บ

เม) อไม�ม�ก�รร�บหร)อสั�งคำ��กล็�บ เวล็�เร�ยกใชั�ง�นฟั�งก�ชั�นก�ไม�จ�เป%นตั�องม�สั�วนที่� สั�งคำ��หร)อตั�วแปรเพ) อม�ร�บคำ��กล็�บ

#07 function 17

function prototypingชั�อของฟั�งก์�ชั�นื้ที่เราจะเรยก์ใชั� จะตั�องถู+ก์

ประก์าศก์�อนื้ก์ารใชั�งานื้เสมอ ( อย+�บรรที่�ดบนื้ของส�วันื้ที่จะเรยก์ใชั� )

ก์ารประก์าศฟั�งก์�ชั�นื้ เรยก์วั�า function prototyping

เหม�อนื้ก์�บก์ารประก์าศชั�อตั�วัแปรก์�อนื้ที่จะเรยก์ใชั�งานื้ ตั�วัแปรนื้��นื้

ก์ารเขยนื้ฟั�งก์�ชั�นื้ ที่สมบ+รณ์�ข5�นื้มาถู�อวั�าเป'นื้ก์ารที่&า function prototyping ไปในื้ตั�วั

แตั�ถู�าเราย�งไม�เขยนื้ฟั�งก์�ชั�นื้ เราจ&าเป'นื้ตั�องประก์าศให�คืนื้อ�นื้ได�ร�บร+�ก์�อนื้

#07 function 18

#include <stdio.h>double fac(double x){ int i; double r = 0 ; for (i=1;i<=x;i++) r = r * i ; return r;}void main( ){ float x , r ; printf(“input number : ”) ; scanf(“ %f ”,&x) ; r = fac(x) ; printf(“ %.f! = %.f ”, x, r);}

#include <stdio.h>double fac(double x){ int i; double r = 0 ; for (i=1;i<=x;i++) r = r * i ; return r;}void main( ){ float x , r ; printf(“input number : ”) ; scanf(“ %f ”,&x) ; r = fac(x) ; printf(“ %.f! = %.f ”, x, r);}

#include <stdio.h>double fac(double x);void main( ){ float x , r ; printf(“input number : ”) ; scanf(“ %f ”,&x) ; r = fac(x) ; printf(“ %.f! = %.f ”, x, r);}double fac(double x){ int i; double r = 0 ; for (i=1;i<=x;i++) r = r * i ; return r;}

#include <stdio.h>double fac(double x);void main( ){ float x , r ; printf(“input number : ”) ; scanf(“ %f ”,&x) ; r = fac(x) ; printf(“ %.f! = %.f ”, x, r);}double fac(double x){ int i; double r = 0 ; for (i=1;i<=x;i++) r = r * i ; return r;}

#07 function 19

#include <stdio.h>#include <conio.h>sum (int a , int b){ return a + b ;}void main( ){ int x; clrscr( ); x = sum(5,sum(4,5)); printf(“x = %d“ , x);}

#include <stdio.h>#include <conio.h>sum (int a , int b){ return a + b ;}void main( ){ int x; clrscr( ); x = sum(5,sum(4,5)); printf(“x = %d“ , x);}

#include <stdio.h>#include <conio.h>sum (int , int );void main( ){ int x; clrscr( ); x = sum(5,sum(4,5)); printf(“x = %d“ , x);}sum (int a , int b){ return a + b ;}

#include <stdio.h>#include <conio.h>sum (int , int );void main( ){ int x; clrscr( ); x = sum(5,sum(4,5)); printf(“x = %d“ , x);}sum (int a , int b){ return a + b ;}

#07 function 20

#include <stdio.h>#include <conio.h>void printMsg(void) {

printf(“inside function\n”);}main() {

clrscr();printf(“inside main \n”);printMsg();printf(“inside main \n”);

}

#include <stdio.h>#include <conio.h>void printMsg(void) {

printf(“inside function\n”);}main() {

clrscr();printf(“inside main \n”);printMsg();printf(“inside main \n”);

}

#include <stdio.h>#include <conio.h>void main() { void printMsg(void);

clrscr();printf(“inside main \n”);printMsg();printf(“inside main \n”);

}void printMsg(void) {

printf(“inside function\n”);}

#include <stdio.h>#include <conio.h>void main() { void printMsg(void);

clrscr();printf(“inside main \n”);printMsg();printf(“inside main \n”);

}void printMsg(void) {

printf(“inside function\n”);}

#07 function 21

ex7_1.c เขยนื้โปรแก์รมเพ�อคื&านื้วัณ์หาผ่ลัลั�พธ์�ของ

เลัขยก์ก์&าลั�ง xy

ผ่+�ใชั�ก์รอก์ตั�วัเลัขฐานื้ x แลัะเลัขชั�ก์&าลั�ง y จาก์นื้��นื้โปรแก์รมจ5งคื&านื้วัณ์ แลัะแสดงผ่ลัลั�พธ์�ออก์ที่างหนื้�าจอ

ในื้ส�วันื้ของก์ารคื&านื้วัณ์ ให�เขยนื้เป'นื้ฟั�งก์�ชั�นื้ โดยม function prototype ด�งนื้�

ฟั�งก์�ชั�นื้ long find_pow ( long , long ) ;

ก์ารที่&างานื้

ฟั�งก์�ชั�นื้คื&านื้วัณ์หาผ่ลัลั�พธ์�ของ xy

ข�อม+ลัเข�า

เลัขจ&านื้วันื้เตั,มชันื้(ด long แที่นื้ x แลัะ y

ข�อม+ลัออก์

เลัขจ&านื้วันื้เตั,มชันื้(ด long แที่นื้ xy

#07 function 22

#include <stdio.h>void test_var(int a) {

a = 5 ;}main() { int a = 3 ;

printf( “a = %d\n” , a ); test_var(a); printf( “a = %d\n” , a );}

#include <stdio.h>void test_var(int a) {

a = 5 ;}main() { int a = 3 ;

printf( “a = %d\n” , a ); test_var(a); printf( “a = %d\n” , a );}

ผ่ลัก์ารร�นื้โปรแก์รม

a = 3

a = 3

#07 function 23

local variable & call by value

local variableตั�วัแปรที่ประก์าศภายใตั�ฟั�งก์�ชั�นื้ที่%ก์

ฟั�งก์�ชั�นื้ (รวัมถู5งฟั�งก์�ชั�นื้ main) เป'นื้ตั�วัแปรชันื้(ด local variable

ตั�วัแปรจะร+�จ�ก์แลัะใชั�งานื้ได�เฉพาะในื้ฟั�งก์�ชั�นื้ที่ประก์าศตั�วัแปรนื้��นื้เที่�านื้��นื้ ฟั�งก์�ชั�นื้อ�นื้ไม�สามารถูร+�จ�ก์แลัะใชั�งานื้ตั�วัแปรของอก์ฟั�งก์�ชั�นื้หนื้5งได�

ตั�วัแปรที่มชั�อซึ่&�าก์�นื้ในื้คืนื้ลัะฟั�งก์�ชั�นื้ จ5งถู�อวั�าเป'นื้ตั�วัแปรคืนื้ลัะตั�วัก์�นื้

#07 function 24

call by value ในื้ก์ารเรยก์ใชั�งานื้ฟั�งก์�ชั�นื้ในื้ภาษาซึ่

ปก์ตั(แลั�วัคื�าที่ส�งให�ก์�บฟั�งก์�ชั�นื้จะใชั�ก์ารส�งคื�าแบบ call by value

คื�อ ก์าร copy คื�าที่ผ่+�เรยก์ใชั�ฟั�งก์�ชั�นื้ส�งให�ก์�บฟั�งก์�ชั�นื้ ไปย�งตั�วัแปรแบบ local ในื้ฟั�งก์�ชั�นื้ ส�วันื้ parameter list ของฟั�งก์�ชั�นื้

จร(งๆ แลั�วัก์,คื�อก์ารประก์าศตั�วัแปรแบบ local ส&าหร�บฟั�งก์�ชั�นื้นื้��นื้ แลัะนื้&าตั�วัแปรแบบ local ที่ประก์าศมา copy คื�าตั�วัแปรที่ส�งให�ก์�บฟั�งก์�ชั�นื้

#07 function 25

#include <stdio.h>

void test_var(int a)

{

a = 5 ;

}

main()

{

int a = 3 ;

printf( “a = %d\n” , a );

test_var(a);

printf( “a = %d\n” , a );

}

#include <stdio.h>

void test_var(int a)

{

a = 5 ;

}

main()

{

int a = 3 ;

printf( “a = %d\n” , a );

test_var(a);

printf( “a = %d\n” , a );

}

main

a 3

test_var

a 3

11

11

ผ่ลัก์ารร�นื้โปรแก์รม

a = 3a = 3

3333

44

445

22

22

55

55

#07 function 26

#include <stdio.h>int test_var(int x) {

int y = 10 ; x = 5 ; printf( “%d %d\n” , x , y ); return y ;}main() { int x = 3 , y = 6;

printf( “%d %d\n” , x , y ); y = test_var(x); printf( “%d %d\n” , x , y );}

#include <stdio.h>int test_var(int x) {

int y = 10 ; x = 5 ; printf( “%d %d\n” , x , y ); return y ;}main() { int x = 3 , y = 6;

printf( “%d %d\n” , x , y ); y = test_var(x); printf( “%d %d\n” , x , y );}

11

ผ่ลัก์ารร�นื้โปรแก์รม

3 65 10

3 10

3333

44

44

22

22

5566

main

x 3

11

y 6

test_var

x 3 y105

66

55

77

77

10