# Function prototype declaration

**URL:** <https://community.unix.com/t/function-prototype-declaration/335754>\
**Category:** Shell Programming and Scripting\
**Created:** [October 22, 2013, 5:28am UTC](https://community.unix.com/t/function-prototype-declaration/335754 "2013-10-22T05:28:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Balasankar](https://community.unix.com/letter_avatar/balasankar/32/5_5575768a8748004e209b776fc1b2916d.png) [@Balasankar](https://community.unix.com/u/Balasankar)\
**Post date:** [October 22, 2013, 5:28am UTC](https://community.unix.com/t/function-prototype-declaration/335754/1 "2013-10-22T05:28:01Z")

</div>

Hi All,

I have the script as below

```nohighlight
#!bin/bash
let k=9
if [$k -eq 9]
then
echo "Start"
Hello
echo "End"
else
echo "failed"
fi
function Hello() {
echo "hello !!!!"
}

```

I got the below error :  
Start  
Testtt.sh: line 6: Hello: command not found  
End

Could any one help me how can I define the function after the main process. If I put the function definition before the main process it would work but I need the program structure as it is. If you could help to delcare any prototype to intimate the shell the I am going to use the function in later part of the program (as we declare prototype in C++).

Thanks in advance!!!

---

<div class="post-metadata">

**Author:** ![Akshay\_Hegde](https://community.unix.com/user_avatar/community.unix.com/akshay_hegde/32/2771_2.png) [@Akshay\_Hegde](https://community.unix.com/u/Akshay_Hegde)\
**Post date:** [October 22, 2013, 5:36am UTC](https://community.unix.com/t/function-prototype-declaration/335754/2 "2013-10-22T05:36:52Z")

</div>

> [@balasankar](#):
>
> Hi All,
> 
> I have the script as below
> 
> ```plaintext
> #!bin/bash
> let k=9
> if [$k -eq 9]
> then
> echo "Start"
> Hello
> echo "End"
> else
> echo "failed"
> fi
> function Hello() {
> echo "hello !!!!"
> }
> 
> ```
> 
> I got the below error :  
> Start  
> Testtt.sh: line 6: Hello: command not found  
> End
> 
> Could any one help me how can I define the function after the main process. If I put the function definition before the main process it would work but I need the program structure as it is. If you could help to delcare any prototype to intimate the shell the I am going to use the function in later part of the program (as we declare prototype in C++).
> 
> Thanks in advance!!!

Function should be defined before calling. as you might aware commands are executed sequentially.

modify like this

```plaintext
#!bin/bash

function Hello(){
        echo "hello "
            }

let k=9
if [$k -eq 9]
then
    echo "Start"
    Hello
    echo "End"
else
    echo "failed"
fi

```

---

<div class="post-metadata">

**Author:** ![Balasankar](https://community.unix.com/letter_avatar/balasankar/32/5_5575768a8748004e209b776fc1b2916d.png) [@Balasankar](https://community.unix.com/u/Balasankar)\
**Post date:** [October 22, 2013, 5:44am UTC](https://community.unix.com/t/function-prototype-declaration/335754/3 "2013-10-22T05:44:20Z")

</div>

Akshay thanks for quick reply.

Isn't possibe to decare prototype kind of statement as we do in C++ ?

Because the code I have posted should be as it is. I can not put the functions before calling.  
Can we let the program know that we are going to use Hello function before funtion command executed.

Thanks!!!

---

<div class="post-metadata">

**Author:** ![alister](https://community.unix.com/letter_avatar/alister/32/5_5575768a8748004e209b776fc1b2916d.png) [@alister](https://community.unix.com/u/alister)\
**Post date:** [October 22, 2013, 7:42am UTC](https://community.unix.com/t/function-prototype-declaration/335754/4 "2013-10-22T07:42:52Z")

</div>

> [@balasankar](#):
>
> Because the code I have posted should be as it is. I can not put the functions before calling.

Why not?

> [@balasankar](#):
>
> Can we let the program know that we are going to use Hello function before funtion command executed.

No. sh parses and executes each command as it's read.

Regards,  
Alister

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [October 22, 2013, 12:40pm UTC](https://community.unix.com/t/function-prototype-declaration/335754/5 "2013-10-22T12:40:37Z")

</div>

> [@balasankar](#):
>
> Isn't possibe to decare prototype kind of statement as we do in C++ ?

Nope.

C++ can do that because it's a compiled language. It can assume that "okay, there's a function named xyzzy, I can find its contents later". When the program gets linked together into an exe, it has to actually go find all these libraries to do so.

Shell language runs immediately with no compilation step. If those functions don't already exist, they don't exist. (A few shells might have special features here. These features do not exist in general.)

Even C++ can't manage if you don't tell it what the functions actually are though! At the very least you have to #include \<something\> before you use them, for it to even assume they exist. You can do something like #include in shell. Put your function, or a bunch of functions, in another file then do this:

```plaintext
. /path/to/script-name

```

Note the space between the dot and everything else, that's essential.

script-name will be run in your current shell, effectively loading the functions in it.
