Modular C Programming & Function #include printNewLine() { printf(“\n”); } main() { printf(“C programming is easy”); printNewLine(); printf(“But, you still fail!”); } C programming is easy But, you still fail! Guess what the output is We have used a function in the program. The function’s name is printNewLine(). This function serves to display a new line.
Modular C Programming & Function #include printNewLine() { printf(“\n”); } main() { printf(“C programming is easy”); printNewLine(); printf(“But, you still fail!”); } During the execution, the program starts in main function. When reach statement printNewLine(), the statement(s) defined within the function will be executed. When it finishes, it will return back to the main function and execute the next statement.
Modular C Programming & Function #include printNewLine() { printf(“\n”); } main() { printf(“C programming is easy”); printNewLine(); printf(“But, you still fail!”); } Terminology Function Definition or Function Implementation Function call
Modular C Programming & Function # include printSquare() { printf(“**\n”); } printTriangle() { printf(“ *\n”); printf(“***\n”); } main() { printTriangle(); printSquare(); printTriangle(); } * *** ** * *** Exercise: What’s the output?
Modular C Programming & Function Game Menu 1 – Red Alert 2 – War Craft Enter your choice: 1 Your CMPB164 already Red Alert Play next time when it is no longer Red Alert Exercise: What’s the output if user input 1? What’s the output if user input 2? Game Menu 1 – Red Alert 2 – War Craft Enter your choice: 2 War already started in Palestine You no longer need a simulated one What if user input 3? Game Menu 1 – Red Alert 2 – War Craft Enter your choice: 3 Can you read English?
Modular C Programming & Function #include int add_Numbers(int num1, int num2) { return num1+num2; } main() { int result; result = add_Numbers(1, 2) printf(“Value returned by the function is %d “, result); } Value return by the function is 3 Guess what the output is
Modular C Programming & Function int add_Numbers(int num1, int num2) { return num1+num2; } This function has 2 formal parameters – variable num1 of type int variable num2 of type int This function is expected to return a value of type int. In this case it return the sum of num1 and num2. Formal parameters of a function are variables declared in the format parameter list within the brackets ( ) right after the function’s name. They are used to send values to the function, return values from it, or both.
Modular C Programming & Function main() { int result; result = add_Numbers(1, 2) printf(“Value returned by the function is %d “, result); } When the main function calls add_Numbers(), it passes value 1 and 2 through num1 and num2 respectively into the function. add_Numbers() then return the sum of num1 and num2 to its caller – main function and this value is then assigned to result
Modular C Programming & Function In general, function defination or implementation has the following format ReturnDataType FunctionName (FormalParameterList) { CompoundStatements } Formal parameters of a function are variables declared in the format parameter list within the brackets ( ) right after the function’s name. They are used to send values to the function, return values from it, or both. Return Data Type specifies the type of the data which the function shall return back to the caller. Both of these features serve to establish data communication between a function and its caller.
Modular C Programming & Function #include int square(int num) { return num*num; } main() { int result; result = square(4); printf(“Square of 4 is %d “, result); } Square of 4 is 16 Guess what the output is
Modular C Programming & Function #include int power(int base, int exponent) { int i, temp; temp = 1; for (i=1; i<=exponent; i++) temp *= base; return temp; } main() { int result; result = power(2, 8); printf(“2 to the power of 8 is %d “, result); } 2 to the power of 8 is 256 Guess what the output is
Modular C Programming & Function #include printSymbol(char symbol, int count) { int i; for (i=1; i<=count; i++) printf(“%c”,symbol); } main() { int i; for (i=1; i<=3; i++) { printSymbol(‘*’, i); printSymbol(‘\n’, 1); } * ** *** Guess what the output is This function print the specified character for count times
Modular C Programming & Function #include printSymbol(char symbol, int count) { int i; for (i=1; i<=count; i++) printf(“%c”,symbol); } printRectangle (char symbol, int height, int width) { int h; for (h=1; h<=height; h++) printSymbol(symbol, width); } main() { printRectangle(‘#’, 3, 4); } #### Guess what the output is