Presentation is loading. Please wait.

Presentation is loading. Please wait.

More on Functions. Assignment 1 Let’s talk … -We always validate to ensure that our program accepts only valid input. - This is done as early in the program.

Similar presentations


Presentation on theme: "More on Functions. Assignment 1 Let’s talk … -We always validate to ensure that our program accepts only valid input. - This is done as early in the program."— Presentation transcript:

1 More on Functions

2 Assignment 1 Let’s talk … -We always validate to ensure that our program accepts only valid input. - This is done as early in the program as possible. - Typically we use While or Do/While; - Also, in testing we want to ensure that the program works for all valid data but rejects invalid data.

3 Example of a Function #include<stdio.h> void fn_give_greeting( ); /* the compiler needs the function prototype for type conversions */ for type conversions */main(){ fn_give_greeting( ); } void fn_give_greeting( ) { printf( “Hello\n”); printf( “Hello\n”); return; return;}

4 To Print the Greeting 3 times… /* This will print Hello 3 times… */ void fnGiveGreeting( ); main(){ fnGiveGreeting( ); } void fnGiveGreeting( ) { printf( “Hello\n”); printf( “Hello\n”); return; return;} Note: The void means the function will not return a value through the return statement and the empty () means the main will not pass any values to the function.

5 Sending a value into the function To Print the Greeting 3 times… #include<stdio.h> void fn_give_greeting(int num); main(){fn_give_greeting(3);} void fn_give_greeting(int num) { int i; for (i=0;i<num;i=i+1) printf( “Hello\n”); printf( “Hello\n”); return; return;} Note: This will print Hello 3 times… Funtion Prototype

6 Sending a value into the function To Print the Greeting 3 times… #include<stdio.h> void fn_give_greeting(int num); main(){fn_give_greeting(3);} void fn_give_greeting(int num) { int i; for (i=1;i<num;i=i+1) printf( “Hello\n”); printf( “Hello\n”); return; return;} Note: This will print Hello 3 times… Function Call

7 Sending a value into the function To Print the Greeting 3 times… #include<stdio.h> void fn_give_greeting(int num); main(){fn_give_greeting(3);} void fn_give_greeting(int num) { int i; for (i=1;i<num;i=i+1) printf( “Hello\n”); printf( “Hello\n”); return; return;} Note: This will print Hello 3 times… The value 3 goes into num

8 Execution of the Function 1)The main “sends” the value 3 into num of the function. 2)Control is passed to the function and main will pause until the function is completed. 3) The function performs it’s task. 4) When the function has completed it’s work, control is returned to main at the line following the call.

9 Returning a value to main() Function that Triples a Value Steps: 1. main() gets a value for the number. 2. main “calls” the function and passes the value for number into num. 3. The function triples num and puts the answer into a local variable called answer. 4. In the return statement, the value for answer is placed into the function name. 5. main( ) then assigns the value in the function name into ans.

10 Example of a Function To Triple a Number double fn_triple(int number) { int answer; int answer; answer = number * 3; answer = number * 3; return answer; return answer;}main(){ int num, ans; printf(“Enter a number: "); scanf("%d", &num); scanf("%d", &num); ans = fn_triple(num); ans = fn_triple(num); printf(" total); printf(" total);}

11 Example of a Function With more Than One Parameter double fn_total_charge(int number_sold, double price_each); main(){ int num; double price, total; printf(“Enter the number of apples and the price: "); scanf("%d %lf", &num, &price); scanf("%d %lf", &num, &price); total = fn_total_charge(num, price); total = fn_total_charge(num, price); printf("%d apples cost $%.2lf\n",num, total); printf("%d apples cost $%.2lf\n",num, total);} double fn_total_charge(int number_sold, double price_each) { double total_cost, tax_rate =.13; double total_cost, tax_rate =.13; total_cost = (number_sold * price_each) * (1 + tax_rate); total_cost = (number_sold * price_each) * (1 + tax_rate); return total_cost; return total_cost;}

12 Your Turn Write a program that passes two values (both doubles) to a function that calculates the total score of an Olympic dive. The first number is difficulty, and the second is for the dive itself. The score is the first number multiplied by the second number. The function returns the calculated score to main().

13 Functions – Guidelines and Rules 1.Each small program (called a function) should handle one task (or function) 2.When coding functions, we attempt to have 55 lines or less so that the programmer can see the entire function on one printed page. 3.The main() program will control the sequence, selection, and looping involving functions. This is done by "calling the functions". 4.When main() calls a function, main will usually tell the function what values to use for certain key variables. This is known as "passing values or variable addresses". 5.The function "sees" only the passed values, variables whose address has been passed, and any locally defined variables.


Download ppt "More on Functions. Assignment 1 Let’s talk … -We always validate to ensure that our program accepts only valid input. - This is done as early in the program."

Similar presentations


Ads by Google