This site is 100% ad supported. Please add an exception to adblock for this site.

CSE 100 - Functions

Terms

undefined, object
copy deck
Examples of Void Functions
void function1(char name, int place)
{ cout<<name<<"is #"<<place<<endl;
}
void funtion2(int al, int mel)
{ cout<<"var3 x var4 = " << mel/al<<endl;
}
void function3(int casey)
{ cout<<casey<<"is the value invar2\n";
}
What Are Function Properties?
1. may be "called"
2. may be passed data called "arguments"
3. will not change the data stored in a received variable unless specifically instructed to do so
4. a "non-void" function may return a value to the calling program
Write a program with a void demofunction that says "in main" "back in main", "in demofuntion", "still in function"
1. #inluce<iostream>
2. using namespace std;
3. void demofunction(void);
4. void main(void)
5. {
6. cout<< "In main\n";
7. demofuntion();
8. cout <<"Back in main\n";
9. }
10 void demofunction(void)
11.{
12. cout<<"in DemoFunction\n";
13. cout<<"Still in function.\n";
14. }
Void Functions

In the given Line #, what is the Output?
6. cout<<"In main\n";
7. demofuntion();
8. cout<<"Back in main\n";
Output:
6. In main
7. (calls function -12) In Demo Function
(calls funtion -13) Still in function
8.Back in main
What Are Common Programming Errors?
1. not initializing variables before use
2. forgetting >> to separate variables in cin
3. applying ++ or -- to an EXPRESSION
What is function declaration?
double Pythagorus(int , double );

- where double, int, and double are: data types allowed only
- identifiers are permitted after int and double in the ()
What is Function Syntax?
Syntax:
function header line function header
{
statements
}

- "statements" are the function body
What is Function Header Syntax?
Syntax
type function_name(parameters )

- no ; after a function header

Example:
double Pythagorus(int a, double b)
What is a Function Definition?
Example:
double Pythagorus(in a, double b)
{
double c;

c = sqrt(a*a + b*b);
return c;
}

- "int a, double b" are type variable
- no ; after header
What is the Function Call (using the function) of...
void main(void)
{
cout<<"The hypotenuse is"
<<Pythagorus(12,5);
}
call function: Pythagorus(12,5);

OUTPUT: the hypotenuse is 13.0
What is an example of a Value Returning Function?
result = square(value);

int square ( int number)
{
return number * number;
}

-number is the value being returned
Write a Program Structure
#include<iostream>
using namespace std;

function prototypes;

void main(void)
{
variable declarations;
statements
[including function calls]
}
function definition(s)
Write the Syntax and an Exampe of a Function Prototype
Syntax:
return type function_name(type);

Example
double Pythagorus(int, double);
Write Examples of Function Prototypes
double Pythagorus(int, double);
void do_stuff(void);
double times_em(int, int, int, int);
double myfunc(double, int);
void print_em(char, double);
Write the Syntax and an Example of Function Calls
Syntax
function_name(arguments)

Example
Pythagorus(var1, var2)
Examples of Function Prototypes:
double Pythagorus(int, double);
void do_stuff(void);
double time_em(int, int, int, int);
double myfunc(double, int);
void print_em(char, double);
Write the Syntax and an Example of a Function Call
Syntax
function_name(arguments)

Example
Pythagorus(var1, var2)
Explain How a Function Call Works for the function
find_max(firstnum, secnum);
find_max(865 , 9090)
in memory, the number 865 is assigned to the function variable firstnum, and 9090 is assigned to the function variable secnum. when the function find_max is called the function goes into memory and takes out 865 and puts it in place for firstnum. it then goes into the memory and takes out 9090 and puts it in place for secnum.
What is the function call for cout << "The hypotenuse ="<<answer<<endl;

cout <<"The Hyportenuse =" << Pythagorus(var1,var2)<<endl;
answer=Pythagorus(var1,var2);
What is the Function Call for

cout<<"the hypotenuse = ""<<answer<<endl;

cout<<"The hypotenuse = "<<Pythagorus(var1,var2) * 100 << endl;
answer = Pythagorus(var1, var2);

answer = answer *100;
Write a Program Structure for a Square Function and Cube Function
#include<iostream>
using namespace std;

square prototype
cube prototype

Main Function
square call
cube call

square function

cube function
Write the Program Structure for the return type int and the variable type double in the functions square and cube
int square(double); //function protoype
int cube(double); // or declaration

void main(void)
{
double x = 8.3;
cout <<"The square is ""<<square(x)<<'\n';
cout<<"The cube is "<<cube(x) <<endl;
}

int square(double n) // function definition
{
int answer;
answer = n*n;
return answer; //or return n*n;
}

int cube(double n) //function definition
{
int answer;
answer = n*n*n;
return answer; // or return n*n*n;
}
What is the Summary of Functions?
Prototype:
type function_name(parameter types);
example -> double Pythagorus(int, double);

Call
function_name(actual argument names)
Example -> Pythagorus(height, base)

Definition
type function_name(parameter type[formal] + names)
Example:
{
double Pythagorus(int a, double b)
}
When is A Function Overloading?
1. Two or more distinct functions may have the same name
2. The data types of the arguments in the function calls must match those in the prototypes and in the definitions
3. The same function is given multiple definitions or implementations. The correct one is chosen by the compiler, not the programmer
What is an Example of Parameter Function Overloading?
The Functions must differ in their parameter lists. The type and/or number of parameters must be different.

Examples:
double do_Area(double)
int do_Area(double, double)
int do_Area(double, int)
void do_Area(double, double, double)
Are these functions in overload?

void myFunction(double);
int myFunction(double);
No! find out why?
What is the Return Value Policy for Functions?
A function can receive many values, but only ONE value can be DIRECTLY returned
What is up with Return Values? What do they do?
The RETURN statement:
- tells the function you're all through
- tell the function which value to send back to the calling programming
- terminates the function call and returns immediately to the calling program.
Write the Syntax and an Example of the Return Statement
Syntax
return expression;

Examples
return c; return(c);
return hypotenuse return (hypotenuse);
What is a Value Returning Function for result = square(value); ?
int square(int number)
{
return number * number;
}

- if number=20 then the return number=400
right a value returning function for the return data type int and the parameter data type int for the function find_max and the variables int x, and int y. You are looking for the int maximum.
inr find_max(int x, int y)
{
int maximum;

if (x>=y)
return x;
else
return y;
What are the two ways that you can pass data? And how do they differ?
1. Passing by valude
2. Passing by reference

In passing by value, values may be passed into a function, The location of the memory value in main is separate from the memory location of the function.

When you pass by reference, the function may give back several values, this is accomplished using references. In passing by reference there is a single memory locatoin, which is shared.
What does is mean to pass data by value? Give an example.
passing by value:
A COPY of a value is passed from the calling function to the called function.

double Pythagorus(int a, double b)
{
a = a * a;
b = b * b;
double c = sqrt(a + b);
return c;
}
How would you store the values from the call function find_max(num1, num2); to the definition void find_max(int x, int y ) ? What are x and y in the definition?
num1 and num2 will be PASSED to the ARGUMENTS x and y.

x and y are arguments.
Data Type Mismatch:

What are the Value Parameters and the Reference Parameters?
Value Parameters:
implicit type conversion - value of the actual parameter is coerced to the data type of the formal parameter

reference parameters:
no coercion because an address is passed, not a value
a formal parameter is..
a value or a reference
an actual parameter may be..
a variable, constant, or expression type coercion may take place

or a variable only of exact same type as formal
What's Happening in a Call Sequence?
1. memory is allocated
2. parameters are passed
3. transfer of control
What's Happening in a Return Sequence?
1. value of the return is stored
2. memory is deallocated
3. transfer of control
What does and IPO chart include?
1. the call functions that are in main
2. the pre-conditions for the call functions
3. the pseudocode for the call functions hh
4. and the post-conditions for the call functions

Deck Info

40

permalink