DEV Community

loading...

Functions in C

mikkel250 profile image mikkel250 Updated on ・3 min read

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):

int main()
{
  // ...your code
  return 0;
}
Enter fullscreen mode Exit fullscreen mode

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.

// #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

Enter fullscreen mode Exit fullscreen mode

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.

int findLargest(int x, int y) {
  int result;

  if (x > y) 
  {
    result = x;
  } else 
  {
    result = y;
  }

  return result;
}

Enter fullscreen mode Exit fullscreen mode

However, if you do not wish to follow this convention, the return statement can 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.

int x = 20;
int y = 30;

int summed = Sum(x, y);
Enter fullscreen mode Exit fullscreen mode

Discussion (2)

pic
Editor guide
Collapse
faridkhan profile image
Farid-khan

Thank you sir!
Sir I want to create a program which will find the day of week for the given date using

function? Could you help me?

Collapse
mikkel250 profile image
mikkel250 Author

These are my notes from learning C myself. I'm not certain how you would go about doing that off the top of my head - I'd recommend looking on stack overflow instead.