FUNCTION Dong-Chul Kim BioMeCIS CSE @ UTA 12/7/2015 1.
-
Upload
margaretmargaret-ellis -
Category
Documents
-
view
215 -
download
0
Transcript of FUNCTION Dong-Chul Kim BioMeCIS CSE @ UTA 12/7/2015 1.
![Page 1: FUNCTION Dong-Chul Kim BioMeCIS CSE @ UTA 12/7/2015 1.](https://reader036.fdocuments.us/reader036/viewer/2022082613/5697bf7d1a28abf838c84a20/html5/thumbnails/1.jpg)
FUNCTION
Dong-Chul KimBioMeCISCSE @ UTA
04/21/23 1
![Page 2: FUNCTION Dong-Chul Kim BioMeCIS CSE @ UTA 12/7/2015 1.](https://reader036.fdocuments.us/reader036/viewer/2022082613/5697bf7d1a28abf838c84a20/html5/thumbnails/2.jpg)
• A function is a self-contained unit of program code designed to accomplish a particular task.
• Some functions can cause an action (printf, scanf), and others can return a value for your program to use (strlen, strcmp).
• Why use functions?• Makes reuse of code easier• Reduces number of places to update• Hide implementation, letting programmer focus on functionality –
more modular
Functions
![Page 3: FUNCTION Dong-Chul Kim BioMeCIS CSE @ UTA 12/7/2015 1.](https://reader036.fdocuments.us/reader036/viewer/2022082613/5697bf7d1a28abf838c84a20/html5/thumbnails/3.jpg)
• You want to write a program that does the following:• Read in a list of numbers• Sort the numbers• Find their average• Print out their average
#include <stdio.h>int main(void){ int list[10]; readlist(list, 10); sortlist(list, 10); average(list, 10); printavg(list, 10); return 0;}
Example
![Page 4: FUNCTION Dong-Chul Kim BioMeCIS CSE @ UTA 12/7/2015 1.](https://reader036.fdocuments.us/reader036/viewer/2022082613/5697bf7d1a28abf838c84a20/html5/thumbnails/4.jpg)
• What do you need to know about functions?• How to define functions properly?• How to call them up for use?• How to set up communication between functions?
Outline
![Page 5: FUNCTION Dong-Chul Kim BioMeCIS CSE @ UTA 12/7/2015 1.](https://reader036.fdocuments.us/reader036/viewer/2022082613/5697bf7d1a28abf838c84a20/html5/thumbnails/5.jpg)
• Function prototype• Tells the compiler what sort of function your_function() is
• A function call• Causes the function to be executed
• A function definition • Specifies exactly what the function does
Three terms
![Page 6: FUNCTION Dong-Chul Kim BioMeCIS CSE @ UTA 12/7/2015 1.](https://reader036.fdocuments.us/reader036/viewer/2022082613/5697bf7d1a28abf838c84a20/html5/thumbnails/6.jpg)
#include <stdio.h>void printstars(int num); /*Function prototype*/int main(void) {
int numstar;do{printf("Please input an integer (>0):");scanf("%d", &numstar);}while(numstar <= 0);printstars(numstar);/*A function call*/return 0;
}
void printstars(int num) /*A function definition */{
int i;for (i=0; i< num; i++){
printf("*");}printf("\n");
}
Example
![Page 7: FUNCTION Dong-Chul Kim BioMeCIS CSE @ UTA 12/7/2015 1.](https://reader036.fdocuments.us/reader036/viewer/2022082613/5697bf7d1a28abf838c84a20/html5/thumbnails/7.jpg)
• We must let the compiler know about the function prior to using it by either:• Placing the function definition before main• Placing a function declaration (or prototype)
before main and define the function later
Function Declarations
![Page 8: FUNCTION Dong-Chul Kim BioMeCIS CSE @ UTA 12/7/2015 1.](https://reader036.fdocuments.us/reader036/viewer/2022082613/5697bf7d1a28abf838c84a20/html5/thumbnails/8.jpg)
• When defining our own functions, the general form isreturn_type function_name(input_type variable_name)
{
/* something happens here */
}
• Example:int addnumbers(int number1, int number2)
{
int sum = number1 + number2;
return sum;
}
Function Definitions
![Page 9: FUNCTION Dong-Chul Kim BioMeCIS CSE @ UTA 12/7/2015 1.](https://reader036.fdocuments.us/reader036/viewer/2022082613/5697bf7d1a28abf838c84a20/html5/thumbnails/9.jpg)
• The variable names in the function definition do not need to match the names in the function call, but the quantity should match.
• To return a value, we use the return keyword.
• We can declare variables in our function just as we did in main.
• We can call other functions from within our function.
![Page 10: FUNCTION Dong-Chul Kim BioMeCIS CSE @ UTA 12/7/2015 1.](https://reader036.fdocuments.us/reader036/viewer/2022082613/5697bf7d1a28abf838c84a20/html5/thumbnails/10.jpg)
#include <stdio.h>
int f1(int y){
y = y + 1;printf("In f1, y = %d\n", y);return f2(y);
}int f2(int y){
y = y + 1;printf("In f2, y = %d\n", y);return y;
}int main(void){
int x = 15;f1(x);return 0;
}
![Page 11: FUNCTION Dong-Chul Kim BioMeCIS CSE @ UTA 12/7/2015 1.](https://reader036.fdocuments.us/reader036/viewer/2022082613/5697bf7d1a28abf838c84a20/html5/thumbnails/11.jpg)
• The types of variables that we can pass or receive from a function can be any of the types that we declare variables to be–int, float, array (actually, we pass the address of the array), etc.
• What type do we use if we are not passing or not returning anything? Void
• Example:void print2numbers(int number1, int number2)
{
printf("%d + %d is %d\n", number1, number2,
number1 + number2);
}
Return and Input Types
![Page 12: FUNCTION Dong-Chul Kim BioMeCIS CSE @ UTA 12/7/2015 1.](https://reader036.fdocuments.us/reader036/viewer/2022082613/5697bf7d1a28abf838c84a20/html5/thumbnails/12.jpg)
• We need to know the following when using variables in functions:
• The process used in this lecture for providing variable values to our function is called pass by value. When doing so, a copy of the variable is provided.
• Variables declared outside the function are unknown to the function unless we pass them.
• Variables declared within a function block are known only to that function.
Variable Scope