function includestdio h includemath h void main printfsquare

  • Slides: 31
Download presentation
ฟงกชน function

ฟงกชน function

ฟงกชนมาตรฐาน #include<stdio. h> #include<math. h> void main() { printf(“square root 4 =”); printf(“%fn”, sqrt(4));

ฟงกชนมาตรฐาน #include<stdio. h> #include<math. h> void main() { printf(“square root 4 =”); printf(“%fn”, sqrt(4)); printf(“ 5 power 3 =”); printf(“%f”, pow(5, 3)); } Square root 4 = 2. 000000 5 power 3 = 125. 000000

ฟงกชน ทผใชกำหนด #include<stdio. h> void showmenu() { printf(“==== MENU ====nn”); printf(“a) Say Hellon”); printf(“b)

ฟงกชน ทผใชกำหนด #include<stdio. h> void showmenu() { printf(“==== MENU ====nn”); printf(“a) Say Hellon”); printf(“b) Say Good Byen”); printf(“Select a or b : n”); } void main() { printf(“Beginn”); showmenu(); printf(“ENDn”); } Begin ==== MENU ==== a) Say Hello b) Say Good Bye Select a or b : END

รปแบบของฟงกชนกำหนดเอง ประกอบดวย • สวนหว 2 สวน (Function header) type function_name (type arg 1, type

รปแบบของฟงกชนกำหนดเอง ประกอบดวย • สวนหว 2 สวน (Function header) type function_name (type arg 1, type arg 2, …) • สวนตว (Function body) { variable declaration; statements; [ return ] }

รปแบบของฟงกชนกำหนดเอง Return = สงคากลบ Parameters = รบคาเขา type function_name (type arg 1, type arg

รปแบบของฟงกชนกำหนดเอง Return = สงคากลบ Parameters = รบคาเขา type function_name (type arg 1, type arg 2, …) { [ variable declaration; ] statements; สงคากลบ [ return value; ] }

แบบท 4: มการรบคาเขา มการสงคากลบ type function_name (type arg 1, type arg 2, … )

แบบท 4: มการรบคาเขา มการสงคากลบ type function_name (type arg 1, type arg 2, … ) { statements; return value; }

Global variable

Global variable

Local variable

Local variable

#include <stdio. h< int function_add (int x, int y( } printf("x = %d ,

#include <stdio. h< int function_add (int x, int y( } printf("x = %d , y = %d n", x, y; ( return x+y; { main() } int value 1 =1, value 2 = 2; printf("result : x + y = %dn“, function_add(value 1, value 2; ( ( {

#include <stdio. h< void oddeven (void; ( void negpos(void; ( int num; void main

#include <stdio. h< void oddeven (void; ( void negpos(void; ( int num; void main (void( } printf("enter number; (": scanf("%d", &num; ( oddeven; () negpos; () getchar; () { void oddeven (void( } if (num % 2( printf("Number is oddn; (" else printf("Number is evenn; (" { void negpos( void( } if (num < 0 ( printf("number is negativen; (" else printf("number is positiven; (" {

#include <stdio. h< #define PI 3. 14159 float area (float; ( void main (void(

#include <stdio. h< #define PI 3. 14159 float area (float; ( void main (void( } float radius; printf("Enter radius of sphere; (": scanf("%f", &radius; ( printf("area of sphere is %. 2 f", area(radius; (( { float area (float rad( } return 4*PI*rad; {

#include <stdio. h< float power 3)float; ( void main (void( } int i; for

#include <stdio. h< float power 3)float; ( void main (void( } int i; for (i=1; i <=10; i++) printf(“%dt%. 0 fn, " i, power 3(i; ( ( { float power 3)float x( } return x *x* x; { 1 2 3 4 5 6 7 8 9 10 1 8 27 64 125 216 343 512 729 1000

สรางฟงกชนทำไม ? #include<stdio. h> main() { int i; int scores[ARRAYMAX]; float percents[ARRAYMAX]; int g_driver,

สรางฟงกชนทำไม ? #include<stdio. h> main() { int i; int scores[ARRAYMAX]; float percents[ARRAYMAX]; int g_driver, g_mode; int i, left, top, wide, bottom, deep; for (i = 0; i < ARRAYMAX; i++) { printf("n. Enter score between 0 and %d: ", MAX); scanf("%d", &scores[i]); } for (i = 0; i < ARRAYMAX; i++) percents[i] = ((float) scores[i]) / MAX; printf("nnnt. SCOREt. PERCENT"); for (i = 0; i < ARRAYMAX; i++) printf("n%d. t%dt%3. 0 f", i + 1, scores[i], (percents[i] * 100)); getch(); detectgraph(&g_driver, &g_mode); initgraph(&g_driver, &g_mode, ". . \bgi"); wide = (int)((getmaxx()) / ((ARRAYMAX * 2 ) + 1)); bottom = getmaxy() - 20; deep = (int) (wide / 4); left = wide; for (i = 0; i < ARRAYMAX; i++) { top = (bottom) - ((int)(p[i] * 300)); bar 3 d(left, top, (left + wide), bottom, deep, 1); left += (wide * 2); } Input and prepare data Display data in graph Draw graph

สรางฟงกชนทำไม ? #include<stdio. h> void input_data(float); void display_data(float); void make_graph(float); main() { Int float

สรางฟงกชนทำไม ? #include<stdio. h> void input_data(float); void display_data(float); void make_graph(float); main() { Int float I, scores[ARRAYMAX]; percents[ARRAYMAX]; input_data(percents); display_data(percents); make_graph(percents); Main function } void input_data(float p[]) { …. …. Input and prepare data } void display_data(float p[]) { …. …. Display data in graph } void make_graph(float p[]) { …. …. } Draw graph

สรางฟงกชนทำไม ? #include<stdio. h> void input_data(float); void display_data(float); void make_graph(float); main() { Int float

สรางฟงกชนทำไม ? #include<stdio. h> void input_data(float); void display_data(float); void make_graph(float); main() { Int float I, scores[ARRAYMAX]; percents[ARRAYMAX]; input_data(percents); display_data(percents); make_graph(percents); Main function } void input_data(float p[]) { …. …. Input and prepare data } void display_data(float p[]) { …. …. Display data in graph } void make_graph(float p[]) { …. …. } Draw graph