哈佛大学CS50 课程笔记--参数,返回值

当水落到水箱里,它就给计算机的逻辑部件发送一个信号,计算机立即通知内存条,为下雨天找出相应的指令,内存条轮流的激活电子电路,关闭窗子,打开门,希望猫进来,自动电话开始行动。

 

Arrays:

Command line:

当你写好一个程序,电脑会默认寻找一个叫做main函数,main是默认运行的,

Hierarchal decompositions:addition

Define a function:one,the name of the function;I am not only saying this function takes an input,I also have to give that input a name.you have to be ever so explicit as to what data type your actually passing in.two,the arguments or parameters,and three,is the return value.You have to tell your compiler what kind of value to expect,and this is going to have ripple effects.Int main,lets ignore what is inside the parentheses for now,Int X gets two;so that declares four bytes of memory and stores inside of them the number 2.X is now placeholder.here is so called function call.you have used get int,get string,or the like perhaps already.here,I am calliing my own function,increment.I an passing an X,and yet I am also assigning the return value to X.So just intuitively what is going to be the effect of this one line of code?So to do X plus.this is why I am disclaming that this isnot all that interesting yet.I am passing as input the value in X,which is two.I have my includes,atandard IO's,so I can use printf,but then I had this thing called a prototype.I just reran make or GCC does it for me,when you compile,notice there is a number of key help--the number of helpful tidbits here.Implicit declaratin of function increment.

C compiler:C or the compiler's for C are kind of dumb,whereby ,they only know about what they have already seen.I have two solutions here;one,what would be sort of the obvious sort of hackish or solve it fast solution?Just move it to the top;Delete those lines of code and move them up to the top and problem solved.now downside of this,well,I have just now violated my preference for having main up top,maybe that is forgivable.but very soon will you find that if you are writing programs with lots and lots of funnctions,and some might called these,and these might call these,you can comme up very soon,with scenarios where you can not order them top to bottom,because everything is comingled,and there is a lot of cross talk among all of your functions.Thankfully C does provide a solution for this,whereby,I can provide a hint,essentially,to the compiler.It is a little redundant,admittedly,but just its prototype,as I say in the comment here.The prototype is just its return value,its name,its arguments,if any.But a semicolon thereafter is sufficient.You donnot have to reimplement the function.henceforth,when you say,to declare a function,I am declaring a function here but down here am I quote unquote defining it or implementing it.That is the semantic distinction.

Function name:It's just a little block of memory in RAM.that represents four bytes,32 bits and the Number Two is currently in it. The fact that I have called it X,is really just a programming--It's a feature of the programming language that I,the human,can now refer to it.Not by some very arcane numeric address inside my computer's memory,but by a symbol,by a name.I don't have to use that name for every copy of X,when you call a functioo,what actually happens is function gets its own chunk of memory that identical  in size to the data type that it expects.And I this case,an int,which means,this function increment also gets from the computer its own four bytes.whatever your passing in,for instance X,and if this is A,this thing gets copied into A;so at that moment in time of calling increment,I actually have two copies of the same value in memory but they refer to different names.

Question:You can use this for nefarious purposes.not so much with iint,because ints are bounded in size.They are 32 bits or thay are four bytes.but once we get the string today,in arrays then absolutely.this is how a lot a lot, a lot of software and servers are still compromised today.so putting that back prototype allows me to compile this,because I am saying proactively to the compiler hey,you are going to see a function called increment.it turns an int.it takes an int;deal with it when you get to it and then once you look through the whole file,should you find --ultimately find the actual code that defines or impliments this thing.where I am defining apparently--declaring a function called cube.

Function:when you define a function,you have,as we have seen these curly braces ,that define all of the code that is associated with that function;if I have two values--let me go on up here--if I have got two values,let's call it int X gets one,int Y getts two,semicolon at the end.so now I want to actually swap these values.You might think--and this board is --and you know what,now we have int X gets one,oh,this is much faster--and now my goal in life is to swap these things.X get assigned the value of Y,but then Y gets the value of X;so both of them now equal?

Swap:function is called swap,takes one int called A,followed by another B,as we have seen with print F,if you have multiple arguments.void just mean this function doesnot return anything.Its purpose in life is to do something ,to have what we call a side effect,but it doesnot actually return a value bacause functions donot have to have values.

Environment variables:we may see over time but elsewhere in memory are other things,text is where the zero ones that compose your file are actually stored.so when you compile a program called A dot out,or skittles or wherever,you double click that program,or in our command line environment,the program is loaded into memory just like microsoft word or whatever would be on your own computer.Conceptually,it's put at the top of my chunk of RAM,below it,goes initialized and uninitialized data--this is a fancy way of saying global variables come next--

Command line:anytime you run a program at the command line,like nano,you have almost always been putting one or more words.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值