Lecture 7: Function Arguments and Memory
Monday, February 20, 2012[Up] [Previous Lecture] [Next Lecture]
Reading Assigned: 3.5-3.6
Homework Due: Homework 02
Homework Assigned: Homework 03 -- assigned last class
Classwork Assigned: None
Topics Covered:
- Notice about nano and macs
- Homework 2 review - questions
- Reminder about the utility of functions
- Allows us to break up problems into smaller pieces
- Effective programs break problems into the smallest reasonable pieces
- Allows us to write code once and use it multiple times
- Function arguments
- Allow us to pass values to a function to modify its behavior
- Create variables that only exist inside the function
- Function example with arguments:
/* File: func_example2.c Name: D. Sheets Username: dsheets Date: 02/19/12 Description: A function example with arguments */ #include <stdio.h> /* A function to add two integers */ int my_add(int number1, int number2) { /* Create a variable to store the result */ int result; /* Print what we are doing */ printf("I will add %d and %d\n", number1, number2); /* Add the two input arguments and store the addition in result */ result = number1 + number2; /* Return the value of adding two integers */ return(result); } /* Main entry point for program */ int main(void) { int value1 = 10; int value2 = 27; int result = 0; /* Print out results value before function call */ printf("Before addition, result is %d\n", result); /* Call our function, passing in two values */ result = my_add(value1, value2); /* Print out results value after function call */ printf("After addition, result is %d\n", result); return(0); }
- Definitions of functions that take arguments are made up of of the following elements:
- return type
- function name
- (
- argument1 type
- argument1 name
- ,
- argument1 type
- argument1 name
- ,
- etc...
- {
- Code of function
- }
- Functions that return values should end with "return(value)" where value is a constant or variable
- Functions that don't return values (ie. void) should end with "return()"
- Functions can take many arguments (upper limit is 31 or 127 or more depending on compiler)
- Memory when using functions
- Variables defined in one function cannot be accessed from another function, including main
void function1(void) { int value = 10; } int main(void) { int test = 0; function1(); /* ERROR The following line is an error and will fail to compile! */ test = value; }
- Variables can share the same name in different functions, but they occupy different memory
void function2(void) { int value = 10; } int main(void) { int value = 0; function1(); /* The following printout will still print "0" */ printf("value is now %d\n", value); }
- Once you leave a function, variables defined in that function are gone
- The names of variables passed into functions do not need to match the argument name
- When values are passed into functions as arguments, a copy is made, so the variables passed in is not changed
- Arguments can be variables, constants, or expressions, eg.
void function3(int arg) { printf("Passed value %d\n", arg); } int main(void) { int test = 37; /* All of the following function calls are valid */ function3(test); function3(10); function3(test + 10); function3((test + 10) % 3); return(0); }
- Variables defined in one function cannot be accessed from another function, including main