NEWTON
Asked
6 months ago
246
views
3
Hello! This is Day 2 of the 17 days of the Cairo challenge. And I have no idea how to solve the playground exercise “Functions”. Perhaps you can help me?
// The following code is missing the function `add1`. // 1. Click on "Run" to see the error. // 2. Write the function `add1`, based on `add1_square`, so // that the program terminates successfully. // // Note that `add1` doesn't have to call other functions. // // You can learn more about functions [here](https://www.cairo-lang.org/docs/how_cairo_works/functions.html). // Write your code here. func add1_square(x: felt) -> (x: felt) { // Call `add1` and unpack the result into `z`. let (z) = add1(y=x); return (x=z * z); } func main() { // Call `add1_square` and save the result into `res`. let (res) = add1_square(x=12); // Verify the computation. assert res = (12 + 1) * (12 + 1); return (); }
Answers to this question are a part of the ✨ 17 days of Cairo Lang with Playground & Newton. ✨
Vote for your favorite answer - the best answer will win a $10 award. A new day – a new reward! During the next 17 days, our goal is to attract more developers to the Cairo language and to systematize the knowledge of Cairo lang. Read rules
Newton
asked
6 months ago
4
Accepted answer
// The following code is missing the function `add1`.
// 1. Click on "Run" to see the error.
// 2. Write the function `add1`, based on `add1_square`, so
// that the program terminates successfully.
func add1_square(x: felt) -> (x: felt) {
// Call `add1` and unpack the result into `z`.
let (z) = add1(y=x);
return (x=z * z);
}
func main() {
// Call `add1_square` and save the result into `res`.
let (res) = add1_square(x=12);
// Verify the computation.
assert res = (12 + 1) * (12 + 1);
return ();
}
func add1_square(x: felt) -> (x: felt) {
// Call `add1` and unpack the result into `z`.
let (z) = add1(y=x);
return (x=z * z);
}
func main() {
// Call `add1_square` and save the result into `res`.
let (res) = add1_square(x=12);
// Verify the computation.
assert res = (12 + 1) * (12 + 1);
return ();
}
func add1(y:felt) -> (z:felt) {
return (z=y + 1);
}
Number of steps: 10
Program hash: 0x03856cfae0d569a83c0e2c9b9524c1e8a42730ccd24ca449ed415923a4c76fb0
Let’s look at the flow of how this program works.
In main function
The main function calls the add1_square function with one parameter named x which holds value 12. The controls go to the function add1_square.
let (res) = add1_square(x=12);
In add1_square function
The add1_square accepts one argument x of value 12 and returns one argument.It calls add1 function and control goes to the add1.
func add1_square(x: felt) -> (x: felt){ //x=12
let (z) = add1(y=x); // y=12
return (x=z * z);}
In add1 function
add1 function accepts one argument y which is 12(see above) and returns one argument z. The z returns the value y+1 which is 13, from where it is called which is add1_square function.
In add1_square function
x value(z * z) returned to where it is called that is the main function. res holds the square of z which is 13*13.
let (z) = add1(y=x); //z=13
return (x=z * z);//x=13*13
At the end computation is verified which turns out to be true as res holds the value x which is 13*13.
assert res = (12 + 1) * (12 + 1); //13*13=13*13 (checking if two variables are same)
Cairo's Assert statements can be used for 2 different use cases:
Here, it's checking if two variables are the same as res is already defined.
You can also use serialize_word to print the res.
// Use the output builtin.
%builtins output
// Import the serialize_word() function.
from starkware.cairo.common.serialize import serialize_word
func add1_square(x: felt) -> (x: felt) {
// Call `add1` and unpack the result into `z`.
let (z) = add1(y=x);
return (x=z * z);
}
func main{output_ptr: felt*}() {
// Call `add1_square` and save the result into `res`.
let (res) = add1_square(x=12);
serialize_word(res);
// Verify the computation.
assert res = (12 + 1) * (12 + 1);
return ();
}
func add1(y:felt) -> (z:felt) {
return (z=y + 1);
}
Program output:
169
Number of steps: 16
Program hash: 0x076e558dcac2872eb0c875a77c1f7be6401f606d62abbe327c93a36652228ab9
Ishita Rastogi
answered
6 months ago
0
You can pretty much solve this challenge by copying the syntax of the previous.
Copy this function:
func add1_square(x: felt) -> (x: felt) {
// Call `add1` and unpack the result into `z`.
let (z) = add1(y=x);
return (x=z * z);
}
On thing I think that could be confusing is that you need to declare a variable name for function parameters and return values. But basically it can be the same! (here x & x).
Good developers copy, great developers steal.
So here is our theft:
// Write your code here.
func add1(y: felt) -> (y: felt) {
return (y=y+1);
}
Note that you need to call the variable y because higher up in the program it's called as such. (It's when add1(y=x)
is called).
victorforissier.eth
answered
5 months ago
1
It is required to add add1
function before add1_square
:
func add1(y: felt) -> (y: felt) {
return (y=y + 1);
}
By looking at the usage of add1
in add1_square
, we see that the required argument should be called y
and should be of type felt
. Also, since the add1
is simple, the increment is done within the return statement without sacrificing readability.
Number of steps: 10
Program hash: 0x00866f10be8849f5d7e293e35c35ae1b178820a025edf7da20a8b64e3e7601a8
0xB883...B7956a
answered
6 months ago
1
func add1(y: felt) -> (z: felt) {
let z = y + 1;
return (z=z);
}
ac
answered
6 months ago
0
We need to add this function:
func add1(y: felt) -> (z: felt) {
return (z = y + 1);
}
0xac3d...5d2902
answered
6 months ago
0
func add1(y: felt) -> (z: felt) {
return (z = y + 1);
}
func add1_square(x: felt) -> (x: felt) {
// Call `add1` and unpack the result into `z`.
let (z) = add1(y=x);
return (x=z * z);
}
func main() {
// Call `add1_square` and save the result into `res`.
let (res) = add1_square(x=12);
// Verify the computation.
assert res = (12 + 1) * (12 + 1);
return ();
}
Asten
answered
6 months ago
0
// The following code is missing the function add1
.
// 1. Click on "Run" to see the error.
// 2. Write the function add1
, based on add1_square
, so
// that the program terminates successfully.
//
// Note that add1
doesn't have to call other functions.
//
// You can learn more about functions here.
// Write your code here.
func add1_square(x: felt) -> (x: felt) {
// Call add1
and unpack the result into z
.
let (z) = add1(y=x);
return (x=z * z);
}
func main() {
// Call add1_square
and save the result into res
.
let (res) = add1_square(x=12);
// Verify the computation.
assert res = (12 + 1) * (12 + 1);
return ();
}
func add1(y : felt) -> (z:felt){ return (z = y + 1); }
0xf2c1...A516b0
answered
6 months ago
-1
We can see by the name of the function (add1
), the assert
in the main
function and the signature add1(y=x)
in add1_square
that we need to return y + 1
.
Therefore, an implementation that would satisfy the assert in the main function would be:
func add1(y: felt) -> (y: felt) {
return (y=y + 1);
}
barrant
answered
6 months ago
1
repository challenge https://github.com/chococrypto/17daysOfCairo
answer:
func add1(y:felt) -> (z:felt) {
return (z=y + 1);
}
func add1_square(x: felt) -> (x: felt) {
// Call `add1` and unpack the result into `z`.
let (z) = add1(y=x);
return (x=z * z);
}
func main() {
// Call `add1_square` and save the result into `res`.
let (res) = add1_square(x=12);
// Verify the computation.
assert res = (12 + 1) * (12 + 1);
return ();
}
chocolaite
answered
6 months ago
1
The goal is to implement :
f : x -> (x+1) * (x+1)
The square function is already implemented, so we just add to implement the add1 function which is :
f : x -> x+1
Here is the implementation :
func add1(y : felt) -> (z : felt):
return (z=y + 1)
end
As asked, we unpacked the result into z.
0xd0Ff...340534
answered
6 months ago
2
Solution:
func add1(y: felt) -> (z: felt) {
return (z = y + 1);
}
return (z=y+1);
is a form of syntactic sugar, instead of writing the function like so:
func add1(y: felt) -> (z: felt) {
[ap] = y + 1, ap++; // z
ret;
}
Ivan
answered
6 months ago
1
Easy way to implement the function add1 is to look at the current function signature in the exercice:
add1(y=x)
The function you need to implement will have the name add1
and take a named input argument called y
. From the rest of the exercice, you can deduce that the function will need to add one to the input parameter and return it (you can see this from assert res = (12 + 1) * (12 + 1);
).
From this we can build the following function:
func add1(y : felt) -> (res : felt){
return(res=y+1);
}
which simply adds one to the input parameter and returns it.
answered
6 months ago
1
function add1_square
receives x
as input and returns
(x+1)^2
, we need function add1
increase input by one before perform squaring.
func add1(y:felt) -> (output: felt){
return(output=y+1);
}
//
answered
6 months ago
How to use get_fp_and_pc in Cairo Lang?
How to submit a StarkNet contract?
Cairo Lang / StarkNet: What are Revoked references? What is alloc_locals?
What is the difference between tempvar/let in Cairo Lang? How to use allow_locals and local?
How to divide felt in Cairo Lang using unsigned_div_rem?
How to make conditional expressions if..then..else in Cairo lang?
How to make recursive function in Cairo Lang?
What do you think about current StarkNet Alpha and future Beta? What will Beta mean? When should it be beta?
Cairo Testing Error
How can I connect a StarkNet account using starknetjs?
Is there any other way to do 2**n without using pow.cairo?
Is there a unsigned_div_rem supporting dividing by 2**128 ?
Would anyone have a link or know how fast blocks would be generated on StarkNet after Regenesis ?
My ABIs will be same right? In UI I am assuming I wont have to make any changes