`
wihoho
  • 浏览: 8945 次
  • 性别: Icon_minigender_1
  • 来自: Singapore
最近访客 更多访客>>
社区版块
存档分类
最新评论

Day 3: Functions

UP 
阅读更多

Why define your own functions?

  • Readability
  • Maintainability
  • Code reuse
#include <iostream>
using namespace std;
int raiseToPower(int base, int exponent) {
  int result = 1;
  for (int i = 0; i < exponent; i = i + 1) {
    result = result * base;
  }
  return result;
}
int main() {
  int threeExpFour = raiseToPower(3, 4);
  cout << "3^4 is " << threeExpFour << endl;
  return 0;
}

 

 

Function delaration should be be set before function invocation.

 

Returning a value

 

  • Up to one value may be returned
  • Must be the same type as the return type
  • No need to be at the end
  • Returns the value as soon as a return statement is executed
Fucntion overloading

void printOnNewLine(int x)
{
    cout << "1 Integer: " << x << endl;
}
void printOnNewLine(int x, int y)
{
    cout << "2 Integers: " << x << " and " << y << endl;
}
 
Function prototypes are generally put into separate header files.

Recursion
  • Functions can call themselves
  • base case
  • recursive step
Pass by value(a) vs pass by reference(&a)

 

0
0
分享到:
评论

相关推荐

    PySpark_Day06:SQL and DataFrames.pdf

    from pyspark.sql.functions import count, groupBy result = iris_df.groupBy('species').agg(count('*').alias('count')) result.show() ``` 这里 `groupBy` 方法用于分组,`agg` 方法用于聚合操作,`count('*')...

    sams teach yourself c++ in 21 days

    Day 3 Variables and Constants Day 4 Expressions and Statements Day 5 Functions Day 6 Basic Classes Day 7 More Program Flow Week 1 in Review Week 2 at a Glance Day 8 Pointers Day 9 References...

    Expert T-SQL Window Functions in SQL Server(Apress,2015)

    Begin using windowing functions like ROW_NUMBER and LAG, and you will discover more ways to use them every day. You will approach SQL Server queries in a different way, thinking about sets of data ...

    10_Days_Of_Javascript_HackerRank:HackerRank 的 10 天 Javascript 挑战的解决方案

    3. **Day 3: Operators and Logic** 这一部分涵盖了算术、比较和逻辑运算符,以及如何使用它们来执行条件检查。理解条件语句,如`if...else`和`switch`,对于编写逻辑复杂的代码至关重要。 4. **Day 4: Functions*...

    PHP.and.MySQL.Recipes.A.Problem-Solution.Approach.1484206061

    Chapter 3: Performing Math Operations Chapter 4: Working with Arrays Chapter 5: Dates and Times Chapter 6: Strings Chapter 7: Files and Directories Chapter 8: Dynamic Imaging Chapter 9: Regular ...

    cheatsheets干货.rar

    Python Cheat Sheet: Functions and Tricks “A puzzle a day to learn, code, and play” → Visit finxter.com computer science

    PHP函数大全(TXT版)

    **3. `getallheaders`** - **定义与用途**:返回所有HTTP头部信息。 - **函数原型**: ```php array getallheaders(void); ``` - **返回值**:返回一个数组,包含了所有HTTP头部及其对应的值。 **4. `virtual...

    Learn.Cplusplus.In.A.DAY.1519318588.epub

    Chapter 3: Variables and Data Types Chapter 4: Operators Chapter 5: Advanced Data Types Chapter 6: Loops and Conditional Statements Chapter 7: Arrays and Pointers Chapter 8: Functions Chapter 9: ...

    英文原版-Programming Python Programming Guide For Beginners Learn In a Day 2nd Edition

    When you purchase Python: Programming Language for Beginners - Learn In A Day, you'll find out the importance of this language, and how to begin improving your skills! These fun and easy tips ...

    12天的azure学习清单

    #### DAY-3:探索Azure数据服务 - **Azure SQL Database**:完全托管的关系型数据库服务,支持SQL Server和MySQL兼容性。 - **Azure Cosmos DB**:全球分布式的多模型数据库服务,支持多个API接口(SQL、MongoDB、...

    python-Day07.rar

    - **类**:类是创建对象的蓝图,它定义了对象的属性(data members)和方法(functions)。 - **对象**:类的实例,通过`__init__`方法初始化对象属性。 - **继承**:一个类可以从另一个类继承属性和方法,实现...

    Sams Teach Yourself C++ in One Hour a Day (8th Edition).pdf(21天学通C++ 第8版 英文版)

    Sams Teach Yourself C++ in One Hour a Day ' In just one hour a day, you'll have all the skills you need to begin programming in C++. With this complete tutorial, you'll quickly master the basics and ...

    python-Day04.rar

    1. **函数(Functions)**:Python中的函数是可重用的代码块,可以接受参数并返回值。学习如何定义函数,如`def`关键字的使用,参数传递(位置参数、关键字参数、默认参数和可变参数),以及返回值的概念。 2. **...

    python-Day05.rar

    1. **函数(Functions)**:函数是Python编程的基础元素之一,用于封装可重复使用的代码段。在Day05中,可能会讲解如何定义函数,参数传递(位置参数、关键字参数、默认参数和可变参数),以及如何返回值。此外,...

    Python.Crash.Course.15196386.epub

    Chapter 3: Python Language Structure Chapter 4: Python Variables Chapter 5: Python Operators Chapter 6: User Input Chapter 7: Strings in Python Chapter 8: Boolean Logic Chapter 9: Loops, Tuples, and ...

    S7A驱动720版本

    Build 224 New Functions: Solved problems: - The setup program created a wrong e-doc link in Windows start menu. Now the proper link to S7A.CHM will be installed - From the S7A power tool it wasn...

    PHP.7.Real.World.Application.Development

    Chapter 3: Working with PHP Functions Chapter 4: Working with PHP Object-Oriented Programming Chapter 5: Interacting with a Database Chapter 6: Building Scalable Websites Chapter 7: Accessing Web ...

    day03-js高级作业.zip

    【标题解析】: "day03-js高级作业.zip" 这个标题暗示了这是一个与JavaScript高级编程相关的学习资料,可能是某课程的第三天作业。"zip"格式表明它是一个压缩包,里面可能包含一系列的文件,如代码、文档或者测试案例...

    Datastage Parallel Job Functions.doc

    - `DateFromJulianDay`:从儒略日(Julian Day)转换为日期。 - `DaysSinceFromDate`:计算两个日期之间的天数差。 - `HoursFromTime`:从时间中提取小时部分。 - `JulianDayFromDate`:将日期转换为儒略日。 -...

    an-apple-a-day:Aave Health Factor Simulator

    设置我们使用Yarn作为包管理器。 要安装依赖项,只需在根目录中执行以下操作: yarn install燃烧起来;... Lambda函数在http://localhost:8911上运行,并且还代理到http://localhost:8910/.redwood/functions/* 。

Global site tag (gtag.js) - Google Analytics