Download presentation
Presentation is loading. Please wait.
1
1 Modularity In “C”
2
2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound statement
3
3 Example-1: //Write a function to display your info to screen. void display_myinfo (void) { printf (“Programmer: Good student \n”); printf (“ID: 123-23-2323 \n”); printf (“Course: CIT105 \n”); }
4
4 Invoke the function display_myinfo from main(): main() { display_myinfo(); } See computer demo
5
5 Example-2: Write a function to compute the volume of a cubic box: float computeVolume (float side) { float volume; volume = side * side * side; return volume; }
6
6 Another way to write the same function: #include float computeVolume (float side) { return side * side * side; } Dummy name
7
7 // Invoke the function: int main() { float length, volume; printf(“ Enter side of the cube:”); scanf (“%f”, &length); // Invoke the function, store the result in vol: volume = computeVolume( length); // display the output: print f(“volume is = %f “, volume); }
8
8 // big picture: float computeVolume (float side) { return side * side * side; } int main() { float length, volume; printf(“ Enter side of the cube:”); scanf (“%f”, &length); // Invoke the function: volume = computeVolume( length); print f(“volume is = %f “, volume); return 0; }
9
9 Important: You may not write the code for a function inside another function functions cannot be nested inside each other. You may invoke functions from other functions.
10
10 Scope Rules: Definition: Scope of a variable is the segment of code where the variable is accessible. Scope of an identifier depends on where in the program it is declared. Scope of the variables declared in a function, is limited to that function.
11
11 Different Scopes in C: Local Scope: Variables declared in a function, have local scope. They can only be used locally within that function. Such variables are referred to as local variables. Global Scope: Variables declared outside the functions have global scope. They can be accessed in all the functions that appear after their declaration in the program. Must be used ONLY when absolutely necessary.
12
12 Cont… Block Scope: Variables declared within a block { … } can be accessed only inside that block of code. Such variables have block scope. Avoid declaring block scope variables.
13
13 Scope of a function: Scope of a function is from the point of its definition or declaration until the end of the program. In order to invoke the functions from main(), you need to write the functions’ code before main() in the code window . But main() is the driver of the program, and should appear before any other function!
14
14 Function Prototypes: One line declaration of the function. It is the first line of the function followed by ; It provides the compiler with important information about the function: Function’s name, Return type, # of parameters, Data type of parameters.
15
15 Examples: Prototype of the function display_myinfo(): void display_myinfo (void); Prototype of the function computeVolume(): float computeVolume (float side); Or float computeVolume (float);
16
16 Arrangement of functions in the program: #include #define … // function prototypes int main() { … } // code for other functions
17
17 Talk about lab04
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.