TuringClub
Published in

TuringClub

Let’s begin to Function

Laws of Programming

Before you go ahead and learn programming concepts, as a programmer, you need to follow few universally accepted and basic code principles when you are coding .

  • KISS : Keep It Simple Stupid
    As a program grows in size, the complexity of the code tends to increase. This would give you a hard time debugging, as debugging complex code is a gruesome task. Nobody loves to maintain complex code. This principle states that you should always keep your code simple.
  • DRY : Don’t Repeat Yourself
    Repeated code is a common mistake among programmers. This principle states that a piece of code should be implemented in just one place in the source code. You can create a common function or abstract your code to avoid any repetition in your code.

Now, that you’re aware of these rules, you’ll clearly understand why and how functions help you follow these principles. So, lets function our brains to understand “functions” 😉.

FUNCTIONS

Functions are small routines or software commands that do specific tasks.

There are basically 2 types of functions :
1. In-built function
These are also called “Standard Library Functions”. Each language has its own set of built-in functions and they are standard. You can’t modify or overwrite them. They are already present to perform a specific task.
example :
strcpy(), getch(), strcmp(), strlen()

NOTE : main() is not a predefined or inbuilt function. It is a user-defined function with a predefined function prototype/declaration. The user writes its functionality, but its declaration has certain limits.

2.User-Defined Functions
Now, you might be wondering, what if you want to create your own function that performs a task that you want it to. That’s where these functions come to action. As it is clear from the name itself, these group of functions are defined by the user aka you. You’ll have full control over the name, task or code of the function you wish to create.
Again these user-defined functions can be of 4 different types, which you’ll know about it later.

Rules to write a function

Okay! Now, to create/declare a function of your own, you need to follow certain rules aka syntax so its understood by the computer. It is always recommended to declare a function before its use so that we don’t see any surprises when the program is run. The following figure shows the syntax :

syntax for user-defined functions
  • return-type : the data-type of the value returned by the function. If it returns nothing
  • function-name : whatever you / programmer wants to name it
  • parameters : values passed or sent to this function. These are also called arguments
  • function-body: the code the performs the task

Remember I was going to talk about types of functions? Well, here it is.
There are 4 types or 4 ways you can create user-defined functions :
1. function with no parameters and no return
2.function +parameters but no return value
3.function + parameters + return value
4.function +return value but no arguments

These will practically demonstrated in the video.

How to use the functions you created?

Okay. Say now you have created a function like below.

int sum(int number1, int number2){
int sum;
sum=number1 + number2;
return sum;
}

The way to use functions is to CALL them. All you have to do is write the function name and pass the parameters if necessary .If your function is returning a value, then assign the function call to a variable , so the returned value gets stored in the variable otherwise it’ll throw an error.

void main(){
int a = 89, b = 100;
c = sum(a,b);
printf("sum is %d", &c);
getch();
}

What happens when you decide not to use a function?

Say, you’re implementing arrays and the code for creation, insertion, deletion and modification of arrays is in one main().This makes it difficult to trace the program flow, creates confusion and possibly bugs in it. This makes the program look unclean where everything is dumped in one main(). A best programmer always follows the KISS and DRY principles to make a cleaner code. The same array program could be made cleaner and easier to understand by creating functions for all the above mentioned array operations.
AND NO ! Don’t dump all your code in main() like the meme says. Don’t follow the meme!

CONCLUSION

Now you know that functions help in avoiding repetition of code, makes it easier for us to modify the code, makes the program easier to read, divides a complex program into chunks of smaller and cleaner ones and the list goes on. In conclusion, it’s difficult to function as a programmer without “functions”.

HAPPY CODING!💜

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store