Functions in C
Tags: c, functions, prototypes, beginners
Published: 2019-12-15 06:22:15

---
title: Functions in C
published: true
description: The basics of declaring functions and using function prototypes
tags: c, functions, prototypes, beginners
series: Basics of the C programming language
---
#Introduction

I'm learning C as a way to also learn CS fundamentals and posting the notes I take so I retain it better, have a reference for later and hopefully my learning process can help others. 
I already know how to program in interpreted languages (mainly JavaScript, but some Ruby and Python as well), so the perspective here assumes some basic programming knowledge already. If you are a complete beginner to programming in general, this may not be the best way to learn, but I did try to explain things as simply as possible because IMO if you can explain things simply, it means you understand them. 

The order of this series is just the order in which I'm learning them, so feel free to jump around - it is not intended to be strictly chronological -- each topic is meant to be more or less self-contained on the subject. 
Enjoy!

##Functions

Functions are declared with a keyword type first, which determines what it will return, e.g. the main function, which is present in every C program, is usually declared as type int and returns an integer (0 by convention): 
```c
int main()
{
  // ...your code
  return 0;
}
```

You can also declare the type void, which will not return anything.

#Function Prototypes

Declaring function prototypes is a way to 'hoist' up the functions that are located further down in the actual source code and allow them to be called at any point in the program. 
They are written in a similar way to function declarations, but the code is not actually included in them, just the type, name, and arguments, and ended with a semicolon. E.g.
```c
// #include and #defines directives

// function prototypes
double Average(double_data_values[], size_t count);
double Sum(double *x, size_t n);
size_t GetData(double*, size_t);

int main(void) 
{
  // code in main() could then call the three functions listed above even if they're defined below

  Sum(arg1, arg2);
  Average(arg3, arg4);
  GetData(arg5, arg6);

}

// Definitions of the actual functions
double Average(double_data_values[], size_t count) 
{
  // the actual logic of the Average function
}

// ...rest of your code

```
#Function parameters

Some caveats when passing parameters (aka arguments) to functions: 

- Array sizes should be declared along with the argument name. The function has no way of telling how big the array 'should' be otherwise, and this can help prevent errors (e.g. out of bounds in iteration).

#The return statement

Return only once
While not strictly necessary, it makes code more readable if there is only one return statement at the bottom. If there is logic that needs to be performed in other parts of the function, store the result as a variable, and then return it at the bottom of the function.
```c
int findLargest(int x, int y) {
  int result;
  
  if (x > y) 
  {
    result = x;
  } else 
  {
    result = y;
  }

  return result;
}

```
However, if you do not wish to follow this convention, the return statement <i>can</i> be declared anywhere, as many times as you like. This will return the value and exit. Depending on the location of the return statement it will exit the nearest set of curly brackets -- either: 
- The block it is inside (e.g. a control flow statement like the if statement above), or 
- the entire function if the return statement is inside the main body of the function.

#Returning data

- If the function has statements inside the body but does not return a value, it must be return type void. 

- The compiler will try to convert the data to the correct type if it does not match the type declared in the function header, and return an error if this is not possible (i.e. it will convert a float to an int but return an error for a string).

#Invoking functions

The arguments should be passed in the same order they are declared in the header, using the same type. 

Functions can be used as the right side of an assignment statement, and the return value will be stored in the variable, e.g.
```c
int x = 20;
int y = 30;

int summed = Sum(x, y);
```