IPC144 – Intro to Functions Agenda: 1 2 3 4 5

Review/Questions – walk4.c Using #define directive More operators Introduction to functions Homework

#define preprocessor directive #define directs the preprocessor to replace all occurrences of the specified macro with the specified value. Syntax: #define NAME value Every occurrence of NAME will be replaced with its value. Example: #define DISCOUNT 5 or #define MAX 10 /* note: there is no need in semicolon at the end */ Example: modified tickets.c #include #define PRICE 79.99 int main() { int child_tickets = 0; int adult_tickets = 0; double total = 0; printf("Enter the number of children:"); scanf("%d", &child_tickets); printf("Enter the number of adults:"); scanf("%d", &adult_tickets);

total = ( child_tickets * PRICE ) * 0.9 + adult_tickets * PRICE; printf("Total before taxes: $%.2lf\n", total); printf("Total including taxes: $%.2lf\n", total * 1.15); return 0; } More Operators -=, +=, *=, /=, %= operators are used when a variable is used on the left and the right side of the formula. Examples: a += 6; b -= 3.5; c *=a; b %= a; x /=a * b + c;

is the same as is the same as is the same as is the same as is the same as

a = a + 6; b = b – 3.5; c = c * a; b = b % a; x = x / (a * b + c);

Excercise: Rewrite the following statements using ( -=, +=, *=, /=, %= ) operators: a) b) c)

a = a * ( b – c % f ); b = b + 4 * 7; c = c – 8;

++ ( increment ) and -- ( decrement ) operators are used when the value of the variable must be incremented or decremented by one. ( pre-increment or prefix ) If ++ operator used before the variable name, then the variable value will be incremented before it is used. Example: int a = 6; printf(“the value of a is %d\n”, ++a); /* will print “the value of a is 7”

*/

( pre – decrement or prefix ) If -- operator is used before the variable name, then the variable value will be decremented before it is used. Example: int a = 6; printf(“the value of a is %d\n”, --a); /* will print “the value of a is 5” */ ( post-increment or postfix ) If ++ operator used after the variable name, then the variable value will be incremented after it is used. Example: int a = 6; printf(“the value of a is %d\n”, a++); /* will print “the value of a is 6”

*/

( post – decrement or postfix ) If – operator is used after the variable name, then the variable value will be decremented after it is used. Example: int a = 6; printf(“the value of a is %d\n”, a -- ); /* will print “the value of a is 6” */

Introduction to Functions A function in C is a part of the program that solves a part of the problem. Used to reduce the complexity of code and avoid the repetition of the same code. Functions in C can be reused when necessary, what makes code shorter and easier to read. There are three components of the function that must be determined when the function is constructed: 1. Name of the function (same rules as for variable names) 2. Number and type of parameters (can be 0 or more) 3. Return type (e.g. int, if the function doesn’t return anything – void)

Syntax: return_type function_name( parameter1, parameter2){

variables declaration; statement(s); return value; }

Example: int main(void) /* int – return value will be integer type, main – name of the function, */ {

/*

(void) – takes no parameters */

char ch; int counter; ch = ‘*’; for(counter = 0; counter < 5; counter ++) printf(“%c”, ch); return 0; } main function receives no parameters, and will return an integer. Example: /* add function takes two arguments (two integers), and returns an integer */ int add(int num1, int num2){ int sum = 0; sum = num1 + num2; return sum; } Example: display function takes two arguments, first argument is integer type, second argument is character type. display function will not return anything. void display( int num, char letter) { int counter; for ( counter = 0; counter < num; counter = counter + 1) printf(“%c”, letter); }

Any C function can be called by another. Example:

#include void display(int num, char letter) { int counter; /* local variable – can be used only inside of display function

*/

for ( counter = 0; counter < num; counter = counter + 1) printf(“%c”, letter); } int main(){ char ch = ‘*’; int number = 5; display(number, ch); return 0; }

display() function is called by main function. Integer “number” and character “ch” were passed to display() function as parameters. Different Types of Functions Function can take values from the calling function or not. Function can return value or not. If a function does not return any value, then we call it void function. When function takes a value from the calling function it is important to remember that only the value of the variable is send. Same thing when function returns a value – only the value is returned. Usually, the main() function is placed first in the code, because it contains the main logic of the program. Then, main() function can use or call other functions to perform specific operations. If a function is defined after the main(), then the function prototype must be written before main(). It will tell the compiler that the function exist and will be defined later. Example 1: #include #define PRICE 0.64 double calculate_cost(int); /* notice that in a function prototype the name of the variable can be omitted */ main(){ int cans = 0; double cost = 0; cans = 25; cost = calculate_cost(cans); /* the value of the cans is sent to the function, which is 25 */ /* a double will be returned and stored in cost variable */

} /* function definition – note that you have to give a name to the parameter received, since it is going to be used in the function */ double calculate_cost( int num_of_cans ){ return num_of_cans * PRICE; } Example 2: /* walk7.c Determine the exact output of the following program */ #include int task_1(int a, int b, char c); main(){ int n1, n2, n3; char ch = 'A'; n1 = 3; n2 = 8; n3 = task_1( n1, n2, ch ); printf("n1 is %d, n2 is %d, n3 is %d, ch is %c\n", n1, n2, n3, ch); } int task_1(int a, int b, char c){ int ret; if(b % a){ /* note if(b % a) is the same as if(b % a != 0) */ ret = b + ( a - b%a); c = 'C'; } else { ret = b; c = 'N'; } return ret; }

Example 3: - fun9.c #include void print_title(int num); /* function prototype */ void underline_title(void); /* function prototype */ int get_number(void); /* function prototype */ void display_result(int num); /* function prototype */ int main(){ int street_number = 70, number = 0; print_title(street_number); underline_title(); number = get_number(); display_result(number); return 0; } /* function definitions */ void print_title(int num) /* function header */ {

printf(“Cummer Valley Middle School\n”); /* function body */ printf(“%d Maxome Avenue\n”, num); } void underline_title(void) /* function header */ { int counter; for(counter = 0; counter < 27; counter ++) /* function body */ printf(“-”); } int get_number(void) /* function header */ { int num; do {

printf ("\nEnter an integer for Times Table ( 1 – 10 ): "); scanf ("%d", &num); } while (num < 1 || num > 10); return num; } void display_result(int num) /* function header */ { int i = 0;

for (i = 1; i