Building Java Programs - courses.cs.washington.edu...for (int i = 1; i
Functions in C. Consider #include main() { int i; for(i=1; i
-
Upload
ronald-mcgee -
Category
Documents
-
view
217 -
download
2
Transcript of Functions in C. Consider #include main() { int i; for(i=1; i
Consider
#include <stdio.h> main() { int i; for(i=1; i <= 5; i++) { printf("%d ", i*i); } for(i=1; i <= 5; i++) { printf("%d ", i*i); } return 0; }
Our program now is#include <stdio.h> void Print_Squares();main() {Print_Squares();Print_Squares();return(0);} void Print_Squares(void) { int i; for(i=1; i <=5; i++) { printf("%d ", i*i); } }
Example functions
int square(int x) { int square_of_x; square_of_x = x * x; return square_of_x; }Returns the square of an integer
Program syntax• Header files e.g. #include<string.h>• Function declaration i.e. a function header• Syntax type function name( Argument list);• Sometimes argument list is omitted, sometimes
not• main()• Function call i.e. function name followed by
legitimate arguments in parentheses e.g.• sin(30);• After program body full function definition
Example
• //Header files and function declaration• #include <stdio.h>• float doubleit(float x) // or float doubleit()• //Next program body
Program Body
main(){float y;y= doubleit(2.0);printf(“value of y is now %f”,y);}//This is followed by function definition
Note
• Result of function is attached by return to function type indicated by
type name(type1 arg1, type2 arg2, ...) { /* code */ }
Boolean Types
• In computer science, the Boolean or logical data type is a data type, having two values (usually denoted true and false), intended to represent the truth values of logic and Boolean algebra.
• It is named after George Boole, who first defined an algebraic system of logic in the mid 19th century.
Boolean types in programming languages
• In programming languages that have a built-in Boolean data type, such as Pascal and Java, the comparison operators such as '>' and '≠' are usually defined to return a Boolean value. Also, conditional and iterative commands may be defined to test Boolean-valued expressions.
C and Boolean Data Types
• Languages without an explicit Boolean data type, like C90 and Lisp, may still represent truth values by some other data type.
• C uses an integer type, where relational expressions like i > j and logical expressions connected by && and || are defined to have value 1 if true and 0 if false, whereas the test parts of if, while, for, etc., treat any non-zero value as true.
• Indeed, a Boolean variable may be regarded (and be implemented) as a numerical variable with a single binary digit (bit), which can store only two values.
Boolean Types in C after 1999 i.e. Version C99
• The initial standards for the C language (1972) provided no Boolean type; and, to this day, Boolean values are commonly represented by integers (ints) in C programs.
• Some of its dialects, like C99 and Objective-C, provide standard definitions of a Boolean type as an integer type and macros for "false" and "true" as 0 and 1, respectively.
• NOTE: There is NO Boolean type in C -- you should use char, int or (better) unsigned char.
• Unsigned can be used with all char and int types.
Boolean Functions
• Boolean functions are functions that return either TRUE or FALSE. Until recently, since the C-language did not contain any data types to represent a Boolean value., programmers resorted to defining TRUE and FALSE as preprocessing declarations of one and zero.
Integers and Bools• The integer one was used to represent a Boolean value
of TRUE, while an integer value of zero was used to represent a FALSE.
typedef enum { false = 0, true = 1 } bool ; • The Box 1 program demonstrates a simple example of
using a boolean return value from a function to test the passed argument is odd. The BOOL declaration is used to represent a boolean datatype - really an integer datatype.
Box1• Box 1 // Header Files usually come first • #include <stdio.h>• // Preprocessing statements are // usually written after
Header Files • #define TRUE 1 • #define FALSE 0 • #define BOOL int • // Function returns TRUE, if arg is odd • BOOL IsOdd(int arg);
Main• /////////////////////////////////////////////
• // main function int • main()• { int num; • printf("Enter a Number: "); • scanf("%d",&num); • // use Boolean Function • if ( IdOdd(num) == TRUE) • { printf("%d is Odd\n",num); } • else { printf("%d is Even\n",num); } • return 0; } • /////////////////////////////////////////////
• // IsOdd Function • BOOL IsOdd(int arg) • { if(arg%2==0) • { return FALSE; } • else • { return TRUE; } • }
Note• If you noticed the program, the preprocessing define
statements have symbols all of uppercase (ie TRUE, FALSE, BOOL). Preprocessing define statements make programs more readable. But, they can create subtle errors in the compile phase.
• For instance, suppose that the first define statement in the above program was changed to #define TRUE 0;.
• Now, the symbol TRUE would be always translated into 0; everywhere where the symbol is used. This leads to many compiler errors where ever the symol TRUE is in your code.
• .
• Most C/C++ programmers always used uppercase letters for all symbols that are in #define statements. If a code gets compiler errors on statements containing uppercase symbols, then it may be due to preprocessing #define statement errors.
• Notice, that the if-else statement in the main function of the Box 1 program has a conditional test of IsOdd(num)==TRUE. This seems to a be a waste, as the condition can be written simply as IsOdd(num).
• Since the function IsOdd returns a non zero value, the condition is effectively true. There seems to be no need to perform a second equality test.
• But, by introducing the test, the code is more easily read, and errors are cought more easily. Some programmers often do not put this equality test into Boolean functions.