Download presentation
Presentation is loading. Please wait.
Published byDayna Johnston Modified over 9 years ago
1
Function
2
Outline Intro. Functions Examples of Functions Prototypes of a Functions Local and Global Variables
3
Introduction One large program under main() has the disadvantages as er rors are harder to find and debug (Large amount of codes). Divide and Conquer is a solution to the aforementioned problem. The original big program is divided into many small problems (Modules). The idea is to solve the original problem by successively solving each small problem. The final solution is combined and formed in the main() program.
4
Functions in C 4 BOSS (calling func) WORKER (called func) 1. Give an assignment (call) 3. Report the results (return) 2. Work (computing) main worker1worker3worker2 worker4worker5
5
Functions in C C Standard Function Basic functions in the C-Standard Library provide fundamental elements for programming such as input/output (scanf/printf in stdio.h), square rooting (sqrt in math.h) printf() #include 5 FUNCTIONS IN C C STANDARD LIBRARY C STANDARD LIBRARY PROGRAMMER DEFINED FUNCTION
6
Functions in C Similarly we can write our own function to perform specific task. This function is called Programmer- Defined Function. Can be called as many times as desired. 6
7
Examples of functions 7 #include int main( ) { int i; for(i = 1; i <= 10; i++) printf("%d ", i * i); printf("\n”); return 0; } #include int square(int); int main( ) { int i; for(i = 1; i <= 10; i++) printf("%d ", square(i)); printf("\n”); return 0; } int square(int y) { return(y*y); }
8
8 x square( ) y COPY ARGUMENTS COPY RETURN- VALUE y * y printf #include int square(int); int main( ) { int i; for(i = 1; i <= 10; i++) printf("%d ", square(i)); printf("\n”); return 0; } int square(int y) { return(y*y); } Examples of functions
9
To declare a function 9 return-type function-name (parameterlist) { declarations; statements; } return-type-value: the type of values returned by the function, e.g., int, float, char, void function-name: the name of the function parameter-list: the list of variables passed by the caller to the function in the exact same order declarations: internal variables used within the function statements: set of commands within the function
10
Different examples of functions #1 void func_name () {statements ….. } #2 void func_name (param-list) {statements ….. } #3 Non-void func_name() {statements ….. } #4 Non-void func_name(param_list) {statements ….. } * non-void refers to a non-void type such as int, float, char, etc.
11
Example 1: no param_list, return-void 11 void main() { my_print(); } void my_print() { printf(“Hello world”); } mainmy_print 1. call 2. Print 3. return
12
Example 2: param_list, return (void) 12 void main() { my_print(2); } void my_print (int x) { printf(“%d”, x); } mainmy_print 1. Call(2) 2. Print 3. Return
13
Example 3: no param_list, non-void return 13 void main() { char ch; ch = my_print(); printf(“%c\n”, ch); } char my_print(void) { char lch; printf(“Enter your character: ”); scanf(“%c”, &lch); printf(“\n”); return lch; } mainmy_print 1. Work 2. Print 3. Return lch
14
Example 4: param_list, non-void return 14 void main() { char ch; ch = my_print(5); printf(“%c\n”, ch); } char my_print(int x) { char lch; printf(“Enter your character: ”); scanf(“%c”, &lch); while (x > 0) { printf(“%c”, lch); x--; } printf(“\n”); return lch; } mainmy_print 1. Work(5) 2. Print 3. Return lch
15
Prototype of function 15 #include int square(int); void main( ) { int x=3; printf(“Square of %d = %d\n”, x, square(x)); } int square(int y) { return y * y; } Prototype is usually listed ahead of main(). It is a way to let other programs aware of the param_list and return-type of the function. It is extremely useful during the code- checking step when the project is being built (compiled). int square(int); //This is an example of function prototype PROTOTYPE CALLING DEFINITION Checking sequence Without prototype, this would have been an error
16
Function Prototype 16 #include int maximum(int, int, int);/*function prototype */ int main() { int a, b, c; printf(“Enter three integers: “); scanf(“%d%d%d”, &a, &b, &c); printf(“Maximum is: %d\n”, maximum(a, b, c); return 0; } /* Function maximum definition */ int maximum(int x, int y, int z) { int max = x; if(y > max) max = y; if(z > max) max = z; return max; } int maximum(int, int, int);
17
Function Prototype 17 #include int maximum(int x, int y, int z) { int max = x; if(y > max) max = y; if(z > max) max = z; return max; } int main() { int a, b, c; printf(“Enter three integers: “); scanf(“%d%d%d”, &a, &b, &c); printf(“Maximum is: %d\n”, maximum(a, b, c); return 0; } In this case, no prototype is needed in the main program. Since, maximum function is defined (being checked) before main(). However, this order is unpopular. Defining main() first is the prefered style.
18
Local and Global Variables 18 Local Variables are created inside a function. They are only recognized internally. Other functions usually do not know them. Local variables are destroyed immediately at the end of calling the function. Global Variables are created outside functions. They can be recognized by every functions. Only exception is in a function who declares local variables of the exact same name.
19
Local and Global Variables 19 #include int ans = 0; int inc_one(int);/* function prototype */ void main() { ans = inc_one(3); printf(“Answer is %d\n”, ans); } /* function definition: return x+1 */ int inc_one(int x) { int ans, b; b = 2; ans = x + b; return ans; } ans is a global variable declared outside the main function. Note that main can use it right away. x, ans, b are local variables inside inc_one function. ans in this function is different from the global ans declared earlier.
20
Local and Global Variables 20 #include void my_func(); //prototype of my_func void main() { int x=3; printf(“Main: Before call function x=%d\n”, x); my_func(); //call my_func printf(“Main: After call function x=%d\n”, x); } void my_func() { int x; x=2; printf(“My_func: x=%d\n”, x); } (local) x inside the main function is different from the (local) x inside the my_func. x is 3 inside main(), but x is 2 inside my_func. Main: Before call function x=3 My_func: x=2 Main: After call function x=3
21
Local and Global Variables 21 #include void my_func(); //prototype of my_func int x; void main() { printf(“Main: Before call function x=%d\n”, x); my_func(); //call my_func printf(“Main: After call function x=%d\n”, x); } void my_func() { x=2; printf(“My_func: x=%d\n”, x); } x in this example is a global variable. Changing x inside my_func also affects the value of x used in main. Main: Before call function x=3 My_func: x=2 Main: After call function x=2
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.