# What is the difference between private func, public func, private func() : async, public func() : async in actor

**URL:** <https://forum.dfinity.org/t/what-is-the-difference-between-private-func-public-func-private-func-async-public-func-async-in-actor/5328>\
**Category:** Language Support\
**Tags:** Motoko\
**Created:** [June 18, 2021, 12:57pm UTC](https://forum.dfinity.org/t/what-is-the-difference-between-private-func-public-func-private-func-async-public-func-async-in-actor/5328 "2021-06-18T12:57:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![flyq](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/flyq/32/3637_2.png) [@flyq](https://forum.dfinity.org/u/flyq)\
**Post date:** [June 18, 2021, 12:57pm UTC](https://forum.dfinity.org/t/what-is-the-difference-between-private-func-public-func-private-func-async-public-func-async-in-actor/5328/1 "2021-06-18T12:57:51Z")

</div>

the [document](https://sdk.dfinity.org/docs/language-guide/language-manual.html#syntax-decls) said:

> - Visibility `private` restricts access to `<id>` to the enclosing object, module or actor.
> - Visibility `public` extends `private` with external access to `<id>` using the dot notation `<exp>.<id>` .

as I test in:

```mo
actor Main {
    var a = 1;
    var b = 1;
    var c = 1;

    public func add1() {
        c := a + b;
    };

    public func addx() : async () {
        c := a + b;
    };

    private func add2() : Nat {
        c := a + b;
        return c;
    };

    public func add3() : async Nat {
        c := a + b;
        return c;
    };

    private func add4() : async Nat {
        c := a + b;
        return c;
    };

    public func add5() : async Nat {
        add1();
        return c;
    };

    public func add6() : async Nat {
        ignore add3();
        return c;
    };

    public func add7() : async Nat {
        ignore await add3();
        return c;
    };

    public func reset() : async Nat {
        c := 1;
        return c;
    };

    public query func geta() : async Nat {
        return a;
    };

    public query func getb() : async Nat {
        return b;
    };

    public query func getc() : async Nat {
        return c;
    };
};

```

code above is compiled successful.

the candid file is:

> **candid**
>
> ```did
> service : {
> add1: () -> () oneway;
> add3: () -> (nat);
> add5: () -> (nat);
> add6: () -> (nat);
> add7: () -> (nat);
> addx: () -> ();
> geta: () -> (nat) query;
> getb: () -> (nat) query;
> getc: () -> (nat) query;
> reset: () -> (nat);
> }
> 
> ```

In the actor, are the following conclusions correct:

1. Although add1 does not have async, it is also “export” and publicly available, just like addx. But add1 and addx are not exactly the same, because there is oneway more in add1 in candid. Can someone explain what oneway means?
2. Although add4 has async, it is private and invisible to the outside wasm/canister. In this case, why not restrict private functions from being modified with async?
3. add5 calls add1 without using asynchronous
4. add6 calls add3 without using asynchronous, although add3 is decorated with async. When a function in the canister calls others functions, even if which is an asynchronous function, it can be called synchronously.
5. add7 calls add3 with using asynchronous

---

<div class="post-metadata">

**Author:** ![stephenandrews](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/stephenandrews/32/1803_2.png) [@stephenandrews](https://forum.dfinity.org/u/stephenandrews)\
**Post date:** [June 18, 2021, 1:12pm UTC](https://forum.dfinity.org/t/what-is-the-difference-between-private-func-public-func-private-func-async-public-func-async-in-actor/5328/2 "2021-06-18T13:12:02Z")

</div>

Based on some of my testing (I had similar questions too):

1. I think oneway is essentially a query call? But if it is not async it can not use await (like JS)
2. private functions can still call out to external canisters and therefore may need to await and must be async funcs
3. Yup, an async func can call a non async func.
4. Yup, you don’t have to await the return. async func’s return a “future” which has to be consumed using await (which holds the stack and returns the value eventually). If you ignore the “future” it’s essentially sync
5. Yup, As above, await forces it to wait for the future to resolve

Hope that helps 🙂

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [June 18, 2021, 2:20pm UTC](https://forum.dfinity.org/t/what-is-the-difference-between-private-func-public-func-private-func-async-public-func-async-in-actor/5328/3 "2021-06-18T14:20:17Z")

</div>

- _Although add1 does not have async, it is also “export” and publicly available, just like addx. But add1 and addx are not exactly the same, because there is oneway more in add1 in candid. Can someone explain what oneway means?_  
A shared function with return type `()` (not `async ()`) returns control immediately and cannot be awaited. It still executes asynchronously and has its state changes committed (so is _not_ a query). If it throws an error, that error cannot be caught by the caller. These oneway functions are useful for fire-and-forget scenarios. (Your example doesn’t have an explicit return type, but an omitted return type defaults to `()`. This is a known pitfall of the syntax and perhaps we should fix it).
- _Although add4 has async, it is private and invisible to the outside wasm/canister. In this case, why not restrict private functions from being modified with async?_  
Local async functions are independently useful for abstracting out code that sends messages and performs awaits.  
(An ordinary, non-async returning local function is not allowed to send messages or await.)
- _add5 calls add1 without using asynchronous_  
`add5` will not wait for `add1()` to complete before returning (because `add1` returns just `()` and is oneway, `add5` _cannot_ wait for `add1()` to complete).
- _add6 calls add3 without using asynchronous, although add3 is decorated with async. When a function in the canister calls others functions, even if which is an asynchronous function, it can be called synchronously._  
`add6` will not wait for `add3()` to complete. Instead `add3()` will execute and finish asynchronously to `add6`. This example is similar to `add5` (except that `add6` could choose to await the `async ()` result of `add3()`, while `add5` cannot await the `()` result of `add1()`.
- _add7 calls add3 with using asynchronous_  
`add7` waits for `add3()` to complete but discards the value before returning `c` or propagates the error thrown by `add3()`.
