Understanding C programming functions


Functions in C Programming are a block of statements enclosed with curly brackets that performs a specified task. C programming functions come in two main forms; those which return something and those which do not return anything, for example, c programming main function(int main), is a function which returns an integer, and since this is our main function, it must return 0, to show that programme ended successfully

Understanding C Programming functions into Details

As we have seen in the introductory part of this article, C programming functions are represented in two forms; functions which return something and functions which do not return anything. We are going to learn these two types of functions into details and understand how we can apply each them in C Programming projects. One thing to note, all c programming functions must be defined above int main() function and below #include stdio.h statements, the reason being, c programming is procedural and everything must presented procedurally, that is, since the execution of C Program starts at the main function (int main()) , then everything declared, must be done above it. If you define function below int main, you will get function not declared error!



1. C Programming functions with return type

C Programming functions with return types are functions which must return something. Function can return int, string, float, double, characters and so on. A function with return type is represented in form of return_type function_name, for example, int add(). Please have a look at the program below:

Also Read: How to create a simple ATM Machine in C Programming

#include <stdio.h>

int printNumberFive(){
	// this function with return type, which is going to return 
	// an intenger
	
	return 5;
}


int main(){

	// Let's call our function, in the main method
	printf("Your number is: %d\n\n", printNumberFive()); 
			
	return 0; 
}


Above was a smimple return type function in C programming, there are also return type functions in C programming with parameters. Parameters can also be known as function arguments. An example of return type function with argument can be represented as: return_type function_name(arg1, arg2, arg3...). To understand C Programming functions with arguments or parameters, please have a look at the example below. Feel free to copy paste this code in your code blocks and test it.

#include <stdio.h>

float calculatePrice(float priceONe, float priceTwo){
	
	return (priceONe + priceTwo);
	
}

int main(){
	
	float priceA = 300, priceB = 600; 
	
	printf("Your total price is: %f\n\n", calculatePrice(priceA, priceB)); 
		
	return 0;
}


Please note a function can return any data type, including compound data types like arrays.

2. C Programming VOID functions

C programming void functions are functions wich do not return any data type, they actually don’t return anything at all. Void functions in C Programming are of two types, functions with parameters or arguments and functions without arguments, we are going to see their examples in a short while. C Programming Void functions can be presented inform of: void function_name(). Also void functions with arguments can be presented in form of: void function_name(arg1, arg2, arg3....). Please have a look at the examples below to understand exactly how to apply void functions in C Programming.



(a). Example of C Programming function without paramenter
#include <stdio.h>

void sayHello(){
	printf("Hellow function\n\n");
}

int main(){
	
	// call function sayHello()
	// this how to call it
	sayHello();
	
	return 0;
		
}

(b). Example of C Programming function with paramenters

Below snipet shows an example of C programming void function with parameters(arguments)

#include <stdio.h>

void printTwoNumbers(int a, int b){
	printf("Your two numbers are %d and %d\n\n", a, b);
}

int main(){
	
	int numberOne = 20, numberTwo = 79; 
	
	printTwoNumbers(numberOne, numberTwo);
	
	return 0;
}

Conclusion

That’s how to use functions in C Programming. In case, you got a question regarding this topic, feel free to ask in the commenting system below, I will be attending to your questions, as soon as possible.

C Programming functions video