- 4. Func Prog Quiz 1
- 5. Functions w Parameters
- 7. Func. in Header Files
- 10. Func. w Ret. Values Prog. Quiz
- 14. Func. Altering Param. Prog. Quiz
- 16. Func. Pass by Reference
- 18. Arrays as Parameters
- 20. Arrays as Param.
- 21. Function Best Practices
4. Func Prog Quiz 1
/*Goal: write a function called printMessage()
**that prints: Functions!
*/
#include<iostream>
void printMessage();
int main()
{
printMessage();
return 0;
}
void printMessage()
{
std::cout<<"Functions";
}
注意使用之前需要先声明。
5. Functions w Parameters
/*GOAL: Practice writing functions
/*Write a function called printProduct that prints the results of the multiplication
**of two integers.
*/
#include <iostream>
int printProduct(int m1, int m2, int product);
int main()
{
int m1 = 4;
int m2 = 5;
int product;
product = m1 * m2;
printProduct(m1, m2, product);
return 0;
}
int printProduct(int m1, int m2, int product){
std::cout << m1 << " * " << m2 << " = " << product << "\n";
}
4 * 5 = 20
7. Func. in Header Files
一般的做法是将函数的声明,放在hpp头文件中,然后include。
10. Func. w Ret. Values Prog. Quiz
main.hpp
/*Put your functions here.
**The grader will use this file to check your work.
*/
#include<iostream>
void printEquation(int m1, int m2, int sum, char operation)
{
std::cout<<m1<<" "<<operation<<" "<<m2<<" = " <<sum;
}
int sum(int m1, int m2)
{
return m1 + m2;
}
main.cpp
#include "main.hpp"
int main()
{
int m1 = 33;
int m2 = 66;
printEquation(m1, m2,sum(m1, m2),'+');
return 0;
}
14. Func. Altering Param. Prog. Quiz
/*The program is changed to return
**the modified variable 'a'
**
**P.S. This is a totally silly program. Why write a function,
**with all the function overhead, to increment a variable?
**Silly!
*/
#include<iostream>
int increment(int input);
int main()
{
int a = 34;
std::cout<<"Before the function call a = "<<a<<"\n";
a = increment(a);
std::cout<<"After the function call a = "<<a<<"\n";
return 0;
}
int increment(int input)
{
input++;
std::cout<<"In the function call a = "<<input<<"\n";
return input;
}
另一种方式,传递引用改变原始的值:
Passing by reference refers to passing the address of the variable rather than the variable. Then when we make a change in a function, we are changing the value at the address, not the variable. Once the value is changed at its address, any access to that address will retrieve the new value.
/*Goal: Learn to pass variables by reference.
**Once we learn this, we can modify variables in functions,
**and the change will be available outside the scope of the
**modification.
*/
#include<iostream>
void increment(int &input); //Note the addition of '&'
int main()
{
int a = 34;
std::cout<<"Before the function call a = "<<a<<"\n";
increment(a);
std::cout<<"After the function call a = "<<a<<"\n";
return 0;
}
void increment(int &input)//Note the addition of '&'
{
input++; //**Note the LACK OF THE addition of '&'**
std::cout<<"In the function call a = "<<input<<"\n";
}
16. Func. Pass by Reference
main.hpp
#include "main.hpp"
int main()
{
char operation = '/';
float input1 = 9.8;
float input2 = 2.3;
float result;
calculate(input1, input2, operation, result);
printEquation(input1, input2, operation, result);
return 0;
}
main.cpp:
/*The header file for main.cpp*/
#include<iostream>
void calculate(float in1, float in2, char op, float &ans);
void printEquation(float input1,float input2, char operation, float result);
void calculate(float in1, float in2, char op, float &ans)
{
switch(op)
{
case '+': ans = in1 + in2;
break;
case '-': ans = in1 - in2;
break;
case '*': ans = in1 * in2;
break;
case '/': ans = in1 / in2;
break;
default: std::cout<<"Illegal operation\n";
}
}
void printEquation(float input1,float input2, char operation, float result)
{
std::cout<<input1<<" "<<operation<<" "<<input2<<" = "<<result<<"\n";
}
18. Arrays as Parameters
C++ 通过下面三种方式传递array:
void functionName(variableType *arrayName)
void functionName(variableType arrayName[length of array])
void functionName(variableType arrayName[])
/*Goal: Learn to pass arrays to functions*/
#include<iostream>
#include<iomanip>
//Pass the array as a pointer
void arrayAsPointer(int *array, int size);
//Pass the array as a sized array
void arraySized(int array[3], int size);
//Pass the array as an unsized array
void arrayUnSized(int array[], int size);
int main()
{
const int size = 3;
int array[size] = {33,66,99};
//We are passing a pointer or reference to the array
//so we will not know the size of the array
//We have to pass the size to the function as well
arrayAsPointer(array, size);
arraySized(array, size);
arrayUnSized(array, size);
return 0;
}
void arrayAsPointer(int *array, int size)
{
std::cout<<std::setw(5);
for(int i=0; i<size; i++)
std::cout<<array[i]<<" ";
std::cout<<"\n";
}
void arraySized(int array[3], int size)
{
std::cout<<std::setw(5);
for(int i=0; i<size; i++)
std::cout<<array[i]<<" ";
std::cout<<"\n";
}
void arrayUnSized(int array[], int size)
{
std::cout<<std::setw(5);
for(int i=0; i<size; i++)
std::cout<<array[i]<<" ";
std::cout<<"\n";
}
输出:
33 66 99
33 66 99
33 66 99
20. Arrays as Param.
If the array contains the value, return the index where the key is located. If the array does not contain the value, return a “-1”.
/*Goal: create a function that searches an array for
**a given value.
*/
#include<iostream>
int search(int array[], int size, int searchKey);
int main()
{
const int size = 4;
int array[] = {345,75896,2,543};
int searchKey = 543;
std::cout<<"Found at: "<<search(array, size, searchKey);
return 0;
}
int search(int array[], int size, int searchKey)
{
int found = -1;
for(int i=0; i<size;i++)
{
if(array[i] == searchKey)
{
found=i;
}
}
return found;
}
21. Function Best Practices
如果你想定义一个,不能在函数中被修改的参数:
int doubleInput(const int input)
int doubleInput(const int input)
{
int h = input *2;
return h;
}