C series for 1st sem students: fun fun functions

Posted on Posted in Blogs, Technology






 

 

Today we are going to discuss functions in a C program, (which is FUN !!!). In the first blog of this series we’ve already encountered a function, called main function. We have also discussed how functions came into being in computer programming. If you’ve missed that blog, check here.

The rule to write a function, also know as syntax, are:

  1. The function should have a name, a return type and may or may not have parameter(s).
  2. The function may not return any value to the executing program, still a return type of void must be mentioned in the function declaration.
  3. Every C program must have a main function.
  4. The function is called by its name, providing the arguments(if present), as parameters, within parenthesis. If the function returns a value, then the function should be called as assigning a value to a variable of same type.
  5. The function must be declared or defined before calling it.

 

Side Note: Programming vocabulary.

parameter is a variable in a function definition. When a function is called, the arguments are the data you pass into the function’s parametersParameter is variable in the declaration of function. Argument is the actual value of this variable that gets passed to function.

To understand the above rules let us first consider a simple and commonly used function, printf. As seen in the above screenshot we are calling the printf function inside the main function, in line number 4.

Here we see only the function call, but not the actual function definition. Let us first analyze the function call. The function name is printf and in the parenthesis we are providing a string of characters as an argument. Additionally, we are not assigning it as a value to any variable. That means it may have a return type of void. But we cannot be certain, because C allows us to not store the return value of a function if we don’t want to. This behavior is a result of ‘legacy code’, because early C compilers accepted functions declared without a return type.

But since the code works, the function declaration/definition must be present above the line calling it. The only line of code above the main function is an include statement. Include statement are used to include another file in the current file. So the definition of the printf function must be present in the included file stdio.h or any file included inside it. We can traverse to this file in our code editor(VS Code) by simply clicking the word ‘printf’ while holding the control key.

printf function jinsoft

In the screenshot shown here, the yellow box shows the function definition of printf. As we can see in line number 413, it has a return type of int. It returns the number of characters it actually prints, which is an integer. In the parameter list we can see a character pointer __format, which is a template string, followed by three dots that indicate optional parameters. You need not worry about what is a pointer or template string, or the actual code inside this function. What I want to stress here is the syntax of writing a function and the fact that main and printf are just functions like any other you are going to write in your own programs.

Speaking of your own functions, let us write a very simple function to drive the point home. In the following program, we are writing the function addus to add two numbers. It sounds silly; why would you use a function to add two numbers when we have a ‘+’ operator already. But remember, this is just for illustration purpose. Coding should be fun, not dumb.

First we define the addus function before the main function. Its return type is int and it has two parameters, firstnum and secondnum. It returns the result of adding these two numbers.

In the main function we define three variables, a, b and c. a and b has values 10 and 20 assigned to them. The function call is assigned as the value of c, and takes the variables a and b as its arguments. This means c will be assigned the return value of the addus function when the program executes. Then the function printf is called which prints the value of c.

We will discuss more on printf and scanf functions and how they work in a subsequent blog.

This was just a dumb example, but the real FUN of functions comes into play when you build a very complex program by dividing it into smaller functions that do parts of the operations and can be reused in different parts of the program. These utility functions can also be packed together in a file(like stdio.h) and distributed among other developers, to reduce the coding effort of everyone. For example, you’ll never have to write the exact code to calculate the square root of a number. It is present as a function in another file, ‘math.h’.

 

Using functions in computer programming is so beneficial that a separate programming paradigm called Functional Programming was invented. This is in contrast to the procedural approach in C language. We will discuss functional programming in a later blog.

In this blog I have made reference to the history of computing. It is very important to know the history of development of programming to better understand the core concepts. I have a written a blog about this here. You’ll also have a better understanding about functions in C when you study subroutines in the subject microprocessor.

 

If you liked this article please comment and show your support and interest so that I’ll be motivated to continue this effort. Like our facebook page if you haven’t already. And if you have any questions please comment. I’ll try to reply all.

Comments

comments

3 thoughts on “C series for 1st sem students: fun fun functions

Comments are closed.